#P11519. [CCO 2024] Telephone Plans

[CCO 2024] Telephone Plans

Problem Description

The "Dormi’s Fone Service" is now the only telephone service provider in CCOland. There are NN houses in CCOland, numbered from 11 to NN. Each telephone line connects two distinct houses such that all the telephone lines that ever exist form a forest.

There is an issue where the phone lines are faulty, and each phone line only exists for a single interval of time. Two houses can call each other at a certain time if there is a path of phone lines that starts at one of the houses and ends in the other house at that time.

We would like to process QQ queries of the following forms:

  • 1 x y: Add a phone line between houses xx and yy. It is guaranteed that a phone line between houses xx and yy was never added before.
  • 2 x y: Remove the phone line between houses xx and yy. It is guaranteed that a phone line currently exists between houses xx and yy.
  • 3 t: Compute the number of pairs of different houses that can call each other at some time between the current query and tt queries ago. To be more clear, let GqG_q be the state of CCOland after the qq-th query, where G0G_0 is the state of CCOland before any queries. If this is the ss-th query, then count the number of pairs of houses that are connected in at least one of Gs−t,Gs−t+1,…,GsG_{s-t},G_{s-t+1},\dots,G_s.

Also, some test cases may be encrypted. For the test cases that are encrypted, the arguments x, y, or t are given as the bitwise xor of the true argument and the answer to the last query of type 3 (if there have been no queries of type 3, then the arguments are unchanged).

Input Format

The first line of input will contain E (E∈{0,1})E~(E \in \{0,1\}). E=0E = 0 denotes that the input is not encrypted, while E=1E = 1 denotes that the input is encrypted.

The second line contains two space-separated integers NN and QQ, representing the number of houses in CCOland and the number of queries, respectively.

The next QQ lines contain queries as specified above (queries are encrypted or not depending on EE.

For the qq-th query (1≤q≤N)(1 \le q \le N), it is guaranteed that (after decrypting if E=1E = 1) 1≤x,y≤N1 \le x,y \le N for type 1 and 2 queries and 0≤t≤q0 \le t \le q for type 3 queries.

Marks Awarded Bounds on NN Bounds on QQ Encrypted?
3 marks 1≤N≤301 \leq N \leq 30 1≤Q≤1501 \leq Q \leq 150 E=0E=0
2 marks E=1E=1
4 marks 1≤N≤20001 \leq N \leq 2000 1≤Q≤60001 \leq Q \leq 6000 E=0E=0
2 marks E=1E=1
4 marks 1≤N≤1000001 \leq N \leq 100000 1≤Q≤3000001 \leq Q \leq 300000 E=0E=0
5 marks E=1E=1
6 marks 1≤N≤5000001 \leq N \leq 500000 1≤Q≤15000001 \leq Q \leq 1500000

Output Format

For each query of type 3, output the answer to the query on a new line.

0
4 12
3 0
1 1 2
3 0
1 1 3
3 0
3 5
2 2 1
3 0
3 8
1 1 4
3 0
3 11
0
1
3
3
1
3
3
5
1
4 12
3 0
1 1 2
3 0
1 0 2
3 1
3 6
2 1 2
3 3
3 9
1 2 7
3 3
3 8
0
1
3
3
1
3
3
5

Hint

Explanation of Output for Sample Input 1

This test case is not encrypted.

For the 11st query, no pairs of different houses could have called each other.

For the 33rd query, only houses 11 and 22 could have called each other.

For the 55th query, {(1,2),(1,3),(2,3)}\{(1, 2), (1, 3), (2, 3)\} is the set of pairs that could have called each other. The 66th query is the same.

For the 88th query, only houses 11 and 33 could have called each other.

For the 99th query, there is a point in time where {(1,2),(1,3),(2,3)}\{(1, 2), (1, 3), (2, 3)\} could have called each other.

For the 1111th query, the set of pairs that could have called each other is {(1,3),(1,4),(3,4)}\{(1, 3), (1, 4), (3, 4)\}.

For the 1212th query, the set of pairs that could have called each other at any previous time is {(1,2),(1,3),(1,4),(2,3),(3,4)}\{(1, 2), (1, 3), (1, 4), (2, 3), (3, 4)\}.

Explanation of Output for Sample Input 2

Encrypted version of sample 1.