QOJ.ac

QOJ

時間限制: 10 s 記憶體限制: 1024 MB 總分: 100

#18825. 트리와 쿼리 22

统计

$N$ 個の頂点からなる木がある。頂点には $1$ から $N$ までの番号が付いている。頂点 $i$ には数字 $a_i$ が書かれている。初期状態では $a_i = i$ に設定されている。

$f(u, v)$ を、$u$ から $v$ への経路 $p_0 = u, p_1, p_2, \ldots ,p_t = v$ 上の数字 $a_{p_0}, a_{p_1}, \ldots, a_{p_t}$ を順に並べた数列とする。

次のようなクエリを処理しなければならない。

  • u v: $a_u$ と $a_v$ を交換する。その後、$f(u, w)$ を最大化する $w$ を出力する。数列は辞書順で比較する。

クエリはオンラインで与えられることに注意せよ。詳細は入力の項を参照せよ。

入力

最初の行に木のサイズ $N$ が与えられる。($2 \le N \le 200{,}000$)

次の $N-1$ 行に整数 $u, v$ が与えられる。これは頂点 $u$ と $v$ を結ぶ辺が存在することを意味する。($1 \le u, v \le N$)

次の行にクエリの個数 $Q$ が与えられる。($1 \le Q \le 200{,}000$)

次の $Q$ 行に整数 $x, y$ が与えられる。($1 \le x, y \le n$, $x \ne y$)

整数 $x, y$ から $u, v$ を得るには次のようにする。直前のクエリの答えを $last$ とする。(最初のクエリの場合は $last = 0$)。このとき $(u, v) = (((x + N - 1 + last) \text{ mod } N) + 1, ((y + N - 1 + last) \text{ mod } N) + 1)$ が成り立つ。

出力

$Q$ 行にわたって問題の答えを出力せよ。

入出力例

入力 1

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

出力 1

1
3
3
3

入力 2

10
9 8
10 3
2 3
10 9
1 10
6 4
4 1
1 5
6 7
15
8 10
2 8
9 8
8 2
9 1
2 8
1 4
4 1
6 2
6 9
7 8
4 2
4 3
10 2
1 9

出力 2

2
7
5
8
5
5
5
8
7
8
2
7
5
7
5

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.