397 传统题

[ABC350G] 在线森林共同邻居 (Mediator)

时间限制
3000ms
内存限制
256MiB
登顶 / 尝试
1/ 1
难度
8
    上传者 标签AtCoderABC350省选-启发式合并树论强制在线

[ABC350G] 在线森林共同邻居 (Mediator)

Score: 600600 points

Problem Statement

Beware the special input format and the smaller memory limit than usual.

There is an undirected graph with vertices 1,2,…,N1, 2, \dots, N, initially without edges.

You need to process the following QQ queries on this graph:

1 $u$ $v$

Type 11: Add an edge between vertices uu and vv.

Before adding the edge, uu and vv belong to different connected components (i.e., the graph always remains a forest).

2 $u$ $v$

Type 22: If there is a vertex adjacent to both uu and vv, report its vertex number; otherwise, report 00.

Given that the graph always remains a forest, the answer to this query is uniquely determined.

The queries are given in an encrypted form.

The original query is defined by three integers A,B,CA, B, C, and the encrypted query is given as three integers a,b,ca, b, c.

Let XkX_k be the answer to the kk-th type-22 query. Define Xk=0X_k = 0 for k=0k = 0.

Restore A,B,CA, B, C from the given a,b,ca, b, c as follows:

  • Let ll be the number of type-22 queries given before this query (not counting the query itself). Then, use the following: $A = 1 + (((a \times (1+X_l)) \mod 998244353) \mod 2)$

  • $B = 1 + (((b \times (1+X_l)) \mod 998244353) \mod N)$

  • $C = 1 + (((c \times (1+X_l)) \mod 998244353) \mod N)$

Constraints

  • All input values are integers.

  • 2≤N≤1052 \le N \le 10^5

  • 1≤Q≤1051 \le Q \le 10^5

  • 1≤u<v≤N1 \le u < v \le N

  • 0≤a,b,c<9982443530 \le a,b,c < 998244353

Input

The input is given from Standard Input in the following format:

$N$ $Q$
$\rm{Query}$$_1$
$\rm{Query}$$_2$
$\vdots$
$\rm{Query}$$_Q$

Output

Let kk be the number of type-22 queries. Print kk lines.

The ii-th line should contain the answer to the ii-th type-22 query.

Sample Input 1

6 12
143209915 123840720 97293110
89822758 207184717 689046893
67757230 270920641 26993265
952858464 221010240 871605818
730183361 147726243 445163345
963432357 295317852 195433903
120904472 106195318 615645575
817920568 27584394 770578609
38727673 250957656 506822697
139174867 566158852 412971999
205467538 606353836 855642999
159292205 319166257 51234344

Sample Output 1

0
2
0
2
6
0
1

After decrypting all queries, the input is as follows:

6 12
2 1 3
1 2 6
1 2 4
1 1 3
2 4 6
2 1 4
1 5 6
1 1 2
2 1 4
2 2 5
2 3 4
2 2 3

This input has a 66-vertex graph and 1212 queries.

  • The first query is 2 1 3. No vertex is adjacent to both vertex 11 and vertex 33, so report 00.

  • The second query is 1 2 6. Add an edge between vertices 22 and 66.

  • The third query is 1 2 4. Add an edge between vertices 22 and 44.

  • The fourth query is 1 1 3. Add an edge between vertices 11 and 33.

  • The fifth query is 2 4 6. The vertex adjacent to both vertices 44 and 66 is vertex 22.

  • The sixth query is 2 1 4. No vertex is adjacent to both vertices 11 and 44, so report 00.

  • The seventh query is 1 5 6. Add an edge between vertices 55 and 66.

  • The eighth query is 1 1 2. Add an edge between vertices 11 and 22.

  • The ninth query is 2 1 4. The vertex adjacent to both vertices 11 and 44 is vertex 22.

  • The tenth query is 2 2 5. The vertex adjacent to both vertices 22 and 55 is vertex 66.

  • The eleventh query is 2 3 4. No vertex is adjacent to both vertices 33 and 44, so report 00.

  • The twelfth query is 2 2 3. The vertex adjacent to both vertices 22 and 33 is vertex 11.

Sample Input 2

2 1
377373366 41280240 33617925

Sample Output 2


The output may be empty.