#P9358. [ICPC2022 Xi'an R] Bridge

    ID: 8550 Type: RemoteJudge 2000ms 512MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>平衡树2022O2优化Link-Cut Tree,LCTICPC

[ICPC2022 Xi'an R] Bridge

题目描述

There are nn countries numbered from 11 to nn in Erathia. Each country can be regarded as a chain with m+1m+1 nodes numbered from 11 to m+1m+1. Initially, node (a,b)(a, b) is connected with node (a,b+1)(a, b + 1) by a street where node (a,b)(a, b) denotes the bb-th node of the aa-th country. There are no bridges between any two countries at first.

You need to process qq queries of the following two types.

  • 1 a b1\ a\ b (1a<n,1bm1\leq a < n, 1\leq b\leq m). Build a bridge between node (a,b)(a, b) and node (a+1,b)(a + 1, b). It is guranteed that at any time, each node is connected with at most one bridge.
  • 2 a2\ a (1an1\leq a\leq n). A hero will walk through Erathia. This hero starts from (a,1)(a, 1). If the hero is at (x,y)(x, y) and there is a unvisited bridge connected to him, he passes it, or he goes to (x,y+1)(x, y + 1). Once he arrived the (m+1)(m+1)-th node of any conutry, he stops. Please note that 'unvisited bridge' is independently judged for each query.

Your task is to print which country the hero is in at last for the second kind of query. It can be proved that the hero's route is always unique under these constraints.

输入格式

The first line contains three integers nn, mm and qq (1n,m,q1051 \leq n, m, q \leq 10 ^ 5).

Each of the following qq lines represents a query with format described above.

输出格式

For each query of type 22, output a line with an integer representing the answer.

题目大意

题目描述

Erathia 大陆上有 nn 个国家,从 11nn 编号。每个国家可以看成由 m+1m + 1 个结点组成的链,结点从 11m+1m + 1 编号。结点 (a,b)(a, b)(a,b+1)(a, b + 1) 由一条街道连接,其中 (a,b)(a, b) 表示国家 aa 的第 bb 个结点。一开始,国家之间没有桥。

你需要处理 qq 个操作:

  • 1 a b1\ a\ b1a<n1\leq a < n1bm1\leq b\leq m):在 (a,b)(a, b)(a+1,b)(a + 1, b) 之间建造一座桥。保证每个结点最多和一座桥相连
  • 2 a2\ a1an1\leq a\leq n):一名英雄走过 Erathia 大陆。他从 (a,1)(a, 1) 出发。如果这名英雄当前在结点 (x,y)(x, y) 且有一座未被访问过的桥与之连接,那么他会走过这个桥到达桥的另一端,否则他会走到 (x,y+1)(x, y + 1)。一旦他到达某个国家的第 m+1m + 1 个结点,他就会停下来。注意两个询问之间的 “未被访问过的桥” 是独立的。

你的任务是对每个操作 22 求出英雄最终所在的国家。

1n,m,q1051\leq n, m, q\leq 10 ^ 5

输入格式

第一行三个整数 n,m,qn, m, q

接下来 qq 行,每行若干个整数表示一组询问。格式见题目描述。

输出格式

对于每个操作 22,输出一行一个整数表示答案。

3 4 13
2 2
1 1 3
2 1
2 2
2 3
1 2 4
2 1
2 2
2 3
1 2 1
2 1
2 2
2 3

2
2
1
3
3
1
2
3
2
1

提示

Source: The 2022 ICPC Asia Xi'an Regional Contest Problem A.

Author: MonkeyKing.