Supermerge

Time limit: 1.2s Memory limit: 512MB Input: Output:

Supermerge super works (joke lost in translation) like Tanaka would say, but it doesn't work because it has no legs (joke lost in translation). Recently, Andrei and Andrei discovered NN sets S1,S2,,SNS_1, S_2, \ldots, S_N, which can contain only natural numbers from 11 to VMAXVMAX. Initially, all sets are empty. On these sets, they observed the following operations occurring:

  • flip poz val - if the element valval exists in the set SpozS_{poz}, then we remove it from the set. Otherwise, we insert it.
  • supermerge st1 dr1 st2 dr2 - the following is applied:
procedure Supermerge
    for i ← st1 ... dr1 do
        for j ← st2 ... dr2 do
        	S[i], S[j] ← S[i] ∪ S[j]

Requirement

Since the operations only cause chaos in the heads of the two Andreis, they want to know how many elements are in each set, after each operation. To simplify the result, they only ask for the computation of the following sum: (S1+S2mod+S3mod2++SNmodN1) % 264(|S_1| + |S_2| \cdot \text{mod} + |S_3| \cdot \text{mod}^2 + \ldots + |S_N| \cdot \text{mod}^{N-1}) \ \% \ 2^{64}, where mod=109+7\text{mod} = 10^9 + 7.

Implementation Details

You will need to implement several functions. The first of them is:

void init(int N, int VMAX)

which receives as parameters:

  • NN, the total number of sets
  • VMAXVMAX, the maximum value an element of a set can take
std::uint64_t flip(int poz, int val)

which:

  • receives as parameters the index of the set and the value
  • returns the sum describing the sets
std::uint64_t supermerge(int st1, int dr1, int st2, int dr2)

which:

  • receives as parameters the intervals between which the supermerge operation takes place
  • returns the sum describing the sets

The init function will be called exactly once, and the flip and supermerge functions will be called a total of at most QQ times.

Constraints and Notes

  • 1Q1 000 0001 \leq Q \leq 1 \ 000 \ 000
  • 1NVMAX1 000 0001 \leq N \cdot VMAX \leq 1 \ 000 \ 000
  • For any operation of type flip, it is guaranteed that 1pozN1 \leq poz \leq N and 1valVMAX1 \leq val \leq VMAX
  • For any operation of type supermerge, it is guaranteed that 1st1dr1N1 \leq st1 \leq dr1 \leq N and 1st2dr2N1 \leq st2 \leq dr2 \leq N
  • By S[i], S[j] ← S[i] ∪ S[j] we denote that both sets receive the result of the union of the two sets simultaneously.
# Score Constraints
1 4 N,VMAX,Q100N, VMAX, Q \leq 100
2 10 N,VMAX,Q1 000N, VMAX, Q \leq 1 \ 000
3 11 VMAX30VMAX \leq 30
4 23 For any operation of type supermerge, it is guaranteed that st1=dr1st1 = dr1 and st2=dr2st2 = dr2
5 7 N100N \leq 100
6 19 NVMAX,Q200 000N \cdot VMAX, Q \leq 200 \ 000
7 17 For any operation of type supermerge, it is guaranteed that st1=st2st1 = st2 and dr1=dr2dr1 = dr2
8 9 No additional restrictions.

Examples

input

5 3 5
0 1 1
1 1 2 2 3
0 3 2
1 1 1 5 5
0 2 2

output

1
1000000015000000057
2000000029000000106
4173964637407773643
4173964638407773650

Explanation

The state of the sets, after each operation, is:

  1. S1={1}, S2=, S3=, S4=, S5=S_1 = \{1\}, \ S_2 = \emptyset, \ S_3 = \emptyset, \ S_4 = \emptyset, \ S_5 = \emptyset
  2. S1={1}, S2={1}, S3={1}, S4=, S5=S_1 = \{1\}, \ S_2 = \{1\}, \ S_3 = \{1\}, \ S_4 = \emptyset, \ S_5 = \emptyset
  3. S1={1}, S2={1}, S3={1,2}, S4=, S5=S_1 = \{1\}, \ S_2 = \{1\}, \ S_3 = \{1, 2\}, \ S_4 = \emptyset, \ S_5 = \emptyset
  4. S1={1}, S2={1}, S3={1,2}, S4=, S5={1}S_1 = \{1\}, \ S_2 = \{1\}, \ S_3 = \{1, 2\}, \ S_4 = \emptyset, \ S_5 = \{1\}
  5. S1={1}, S2={1,2}, S3={1,2}, S4=, S5={1}S_1 = \{1\}, \ S_2 = \{1, 2\}, \ S_3 = \{1, 2\}, \ S_4 = \emptyset, \ S_5 = \{1\}

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