Stack

Time limit: 4s Memory limit: 1024MB Input: Output:

You are given a sequence, ss, of size NN with elements from the set {0,1,2,,M1}\{0, 1, 2, \ldots, M - 1\}. We define the compression function of a sequence as follows:

function compress(s)
	stk ← []
	for i ← 0 ... len(s) − 1 do
		if s[i] = last element of stk then
			remove last element from stk
		else
			stk ← stk + s[i]
		end if
	end for
	return len(stk)
end function

Requirement

We denote by s[ij]s[i\dots j] the contiguous subsequence of ss obtained by removing the prefix s0,s1,si1s_0, s_1, \dots s_{i-1} and the suffix sj+1,sj+2,sN1s_{j+1}, s_{j+2}, \dots s_{N-1}.

Compute

0ij<Ncompress(s[ij])\sum_{0 \le i \le j < N} \operatorname{compress}(s[i \dots j])

Since this number can become very large, output the remainder of its division by 2642^{64}.

Implementation Details

You must implement the function

std::uint64_t sum_compressed_lengths(std::vector<int> s, int M);

which:

  • receives as parameters the sequence ss and MM, with the meaning given in the statement;
  • returns the required sum modulo 2642^{64}.

The judge will call the function exactly once per test.

Constraints and Notes

  • 1MN5 000 0001 \leq M \leq N \leq 5 \ 000 \ 000
  • 0si<M0 \leq s_i < M
# Score Constraints
1 2 M=1M = 1
2 3 1N2 0001 \leq N \leq 2 \ 000
3 11 M=2,1N500 000M = 2, 1 \leq N \leq 500 \ 000
4 13 1NM5 000 0001 \leq N \cdot M \leq 5 \ 000 \ 000
5 22 1N500 0001 \leq N \leq 500 \ 000
6 49 No additional restrictions.

Example

input

10 5
2 2 3 3 4 4 1 1 4 3

output

64

Log in or sign up to be able to send submissions!