QOJ.ac

QOJ

Limite de temps : 10 s Limite de mémoire : 1024 MB Points totaux : 100

#18825. 트리와 쿼리 22

Statistiques

Có một cây gồm $N$ đỉnh. Các đỉnh được đánh số từ $1$ đến $N$. Trên đỉnh $i$ có ghi số $a_i$. Ban đầu, $a_i = i$ được thiết lập.

Đặt $f(u, v)$ là dãy số gồm các số $a_{p_0}, a_{p_1}, \ldots, a_{p_t}$ được viết theo thứ tự trên đường đi $p_0 = u, p_1, p_2, \ldots ,p_t = v$ từ $u$ đến $v$.

Bạn phải thực hiện các truy vấn sau:

  • u v: Đổi chỗ $a_u$ và $a_v$. Sau đó, in ra $w$ làm cho $f(u, w)$ lớn nhất. Các dãy số được so sánh theo thứ tự từ điển.

Lưu ý rằng các truy vấn được đưa ra trực tuyến. Chi tiết tham khảo phần dữ liệu vào.

Dữ liệu vào

Dòng đầu tiên chứa kích thước của cây $N$ ($2 \le N \le 200\,000$).

$N-1$ dòng tiếp theo, mỗi dòng chứa hai số nguyên $u, v$, cho biết có một cạnh nối hai đỉnh $u$ và $v$ ($1 \le u, v \le N$).

Dòng tiếp theo chứa số lượng truy vấn $Q$ ($1 \le Q \le 200\,000$).

$Q$ dòng tiếp theo, mỗi dòng chứa hai số nguyên $x, y$ ($1 \le x, y \le N$, $x \ne y$).

Để lấy $u, v$ từ $x, y$, làm như sau: Gọi $last$ là đáp án của truy vấn trước đó (nếu là truy vấn đầu tiên thì $last = 0$). Khi đó $(u, v) = (((x + N - 1 + last) \text{ mod } N) + 1, ((y + N - 1 + last) \text{ mod } N) + 1)$.

Dữ liệu ra

In ra $Q$ dòng, mỗi dòng là đáp án của truy vấn tương ứng.

Ví dụ

Dữ liệu vào 1

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

Dữ liệu ra 1

1
3
3
3

Dữ liệu vào 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

Dữ liệu ra 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.