PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000120. Truy vấn liên thông trực tuyến bắt buộc (Forced Online Queries Problem)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Truy vấn liên thông trực tuyến bắt buộc (Forced Online Queries Problem)

Forced Online Queries Problem

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an undirected graph with nn vertices numbered from 11 to nn. Initially, the graph has no edges. Process mm operations in their given order.

Let lastlast be the answer to the most recent type 2 connectivity query; initially last=0last=0. Each operation contains t,x,yt,x,y with x≠yx\ne y. Before applying the operation, decode the endpoints as

$$u=((x+last-1)\bmod n)+1,\qquad v=((y+last-1)\bmod n)+1.$$
  • 1 x y — Toggle an undirected edge: insert {u,v}\{u,v\} if it is absent, or remove it if it is present.
  • 2 x y — Connectivity query: its answer is 11 if there is a path from uu to vv using currently present edges, and 00 otherwise. Then set lastlast to that answer.

The graph may be disconnected and may contain cycles. The same edge can be toggled repeatedly. Every operation must use the current value of lastlast.

Input

  • The first line contains n,mn,m.
  • The following mm lines each contain t,x,yt,x,y, with t∈{1,2}t\in\{1,2\}, 1≤x,y≤n1\le x,y\le n, x≠yx\ne y.
  • At least one operation is of type 2.

Output

Print one string consisting of 0 and 1 characters with no spaces. Its ii-th character is the answer to the ii-th type 2 query, in chronological order. End the output with a newline.

Subtasks

General guarantees: 2≤n,m≤200 0002\le n,m\le200\,000; 1≤x,y≤n1\le x,y\le n, x≠yx\ne y; at least one type 2 query.

  • Subtask 1 (10 points): 2≤n≤352\le n\le 35, 2≤m≤1202\le m\le 120; all general guarantees remain in force.
  • Subtask 2 (20 points): 2≤n≤1,5002\le n\le 1,500, 2≤m≤5,0002\le m\le 5,000; all general guarantees remain in force.
  • Subtask 3 (30 points): 2≤n≤35,0002\le n\le 35,000, 2≤m≤35,0002\le m\le 35,000; all general guarantees remain in force.
  • Subtask 4 (40 points): 2≤n≤200,0002\le n\le 200,000, 2≤m≤200,0002\le m\le 200,000; all general guarantees remain in force (full original constraints).

Examples

Example 1

Input

5 9
1 1 2
1 1 3
2 3 2
1 2 4
2 3 4
1 2 4
2 3 4
1 1 3
2 4 3

Output

1010

Explanation

Initially last=0last=0. Edges (1,2)(1,2) and (1,3)(1,3) are inserted. The first query connects 33 and 22 through 11, so the answer is 1 and lastlast becomes 11. Subsequent operations shift both endpoints by one modulo 55. The four query answers are 1, 0, 1, 0, printed together as 1010.

Example 2

Input

3 9
1 1 2
1 2 3
1 3 1
2 1 3
1 3 2
2 2 3
1 1 2
2 1 2
2 1 2

Output

1101

Explanation

In the second example, the initial triangle connects all three vertices. The decoded toggle operations then lead to query answers 1, 1, 0, 1; concatenating them gives 1101.