#include <bits/stdc++.h>
using i64 = long long;
#ifdef DEBUG
#include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
#define debug(...) void(23)
#endif
constexpr int max_N = int(2E5) + 5;
constexpr int LG = 20;
int N, M, Q;
std::vector<int> adj[max_N];
int par[LG][max_N], tin[max_N], tout[max_N], dep[max_N], timer;
void dfs(int v) {
tin[v] = timer++;
for (auto u : adj[v]) {
adj[u].erase(std::find(adj[u].begin(), adj[u].end(), v));
par[0][u] = v;
dep[u] = dep[v] + 1;
dfs(u);
}
tout[v] = timer;
}
bool is_in_subtree_of(int u, int v) {
return tin[v] <= tin[u] && tout[u] <= tout[v];
}
int lca(int a, int b) {
if (is_in_subtree_of(a, b)) {
return b;
} else if (is_in_subtree_of(b, a)) {
return a;
}
for (int lg = LG - 1; lg >= 0; --lg) {
if (!is_in_subtree_of(b, par[lg][a])) {
a = par[lg][a];
}
}
return par[0][a];
}
int A[max_N];
std::set<int> s[max_N], t[max_N];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> N >> M >> Q;
for (int i = 1; i < N; ++i) {
int X, Y;
std::cin >> X >> Y;
adj[X].emplace_back(Y);
adj[Y].emplace_back(X);
}
for (int i = 0; i < M; ++i) {
std::cin >> A[i];
}
par[0][1] = 1;
dfs(1);
for (int lg = 0; lg + 1 < LG; ++lg) {
for (int i = 1; i <= N; ++i) {
par[lg + 1][i] = par[lg][par[lg][i]];
}
}
for (int i = 0; i < M; ++i) {
s[A[i]].emplace(i);
}
for (int i = 0; i + 1 < M; ++i) {
t[lca(A[i], A[i + 1])].emplace(i);
}
while (Q--) {
int TP;
std::cin >> TP;
if (TP == 1) {
int P, X;
std::cin >> P >> X;
--P;
s[A[P]].erase(P);
if (0 <= P - 1) {
t[lca(A[P - 1], A[P])].erase(P - 1);
}
if (P + 1 < M) {
t[lca(A[P], A[P + 1])].erase(P);
}
A[P] = X;
s[A[P]].emplace(P);
if (0 <= P - 1) {
t[lca(A[P - 1], A[P])].emplace(P - 1);
}
if (P + 1 < M) {
t[lca(A[P], A[P + 1])].emplace(P);
}
} else {
int L, R, X;
std::cin >> L >> R >> X;
--L, --R;
auto it = s[X].lower_bound(L);
if (it != s[X].end() && *it <= R) {
std::cout << *it + 1 << ' ' << *it + 1 << '\n';
} else {
it = t[X].lower_bound(L);
if (it != t[X].end() && *it + 1 <= R) {
std::cout << *it + 1 << ' ' << *it + 2 << '\n';
} else {
std::cout << "-1 -1\n";
}
}
}
debug();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24400 KB |
n=5 |
2 |
Correct |
14 ms |
24516 KB |
n=100 |
3 |
Correct |
11 ms |
24400 KB |
n=100 |
4 |
Correct |
12 ms |
24400 KB |
n=100 |
5 |
Correct |
12 ms |
24400 KB |
n=100 |
6 |
Correct |
11 ms |
24400 KB |
n=100 |
7 |
Correct |
11 ms |
24400 KB |
n=100 |
8 |
Correct |
9 ms |
24400 KB |
n=100 |
9 |
Correct |
9 ms |
24400 KB |
n=100 |
10 |
Correct |
10 ms |
24568 KB |
n=100 |
11 |
Correct |
11 ms |
24400 KB |
n=100 |
12 |
Correct |
12 ms |
24400 KB |
n=100 |
13 |
Correct |
12 ms |
24400 KB |
n=100 |
14 |
Correct |
11 ms |
24400 KB |
n=100 |
15 |
Correct |
10 ms |
24400 KB |
n=100 |
16 |
Correct |
11 ms |
24400 KB |
n=100 |
17 |
Correct |
11 ms |
24400 KB |
n=100 |
18 |
Correct |
12 ms |
24400 KB |
n=100 |
19 |
Correct |
12 ms |
24568 KB |
n=100 |
20 |
Correct |
12 ms |
24400 KB |
n=100 |
21 |
Correct |
11 ms |
24400 KB |
n=100 |
22 |
Correct |
10 ms |
24400 KB |
n=100 |
23 |
Correct |
12 ms |
24400 KB |
n=100 |
24 |
Correct |
13 ms |
24572 KB |
n=100 |
25 |
Correct |
11 ms |
24400 KB |
n=100 |
26 |
Correct |
13 ms |
24400 KB |
n=12 |
27 |
Correct |
12 ms |
24568 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24400 KB |
n=5 |
2 |
Correct |
14 ms |
24516 KB |
n=100 |
3 |
Correct |
11 ms |
24400 KB |
n=100 |
4 |
Correct |
12 ms |
24400 KB |
n=100 |
5 |
Correct |
12 ms |
24400 KB |
n=100 |
6 |
Correct |
11 ms |
24400 KB |
n=100 |
7 |
Correct |
11 ms |
24400 KB |
n=100 |
8 |
Correct |
9 ms |
24400 KB |
n=100 |
9 |
Correct |
9 ms |
24400 KB |
n=100 |
10 |
Correct |
10 ms |
24568 KB |
n=100 |
11 |
Correct |
11 ms |
24400 KB |
n=100 |
12 |
Correct |
12 ms |
24400 KB |
n=100 |
13 |
Correct |
12 ms |
24400 KB |
n=100 |
14 |
Correct |
11 ms |
24400 KB |
n=100 |
15 |
Correct |
10 ms |
24400 KB |
n=100 |
16 |
Correct |
11 ms |
24400 KB |
n=100 |
17 |
Correct |
11 ms |
24400 KB |
n=100 |
18 |
Correct |
12 ms |
24400 KB |
n=100 |
19 |
Correct |
12 ms |
24568 KB |
n=100 |
20 |
Correct |
12 ms |
24400 KB |
n=100 |
21 |
Correct |
11 ms |
24400 KB |
n=100 |
22 |
Correct |
10 ms |
24400 KB |
n=100 |
23 |
Correct |
12 ms |
24400 KB |
n=100 |
24 |
Correct |
13 ms |
24572 KB |
n=100 |
25 |
Correct |
11 ms |
24400 KB |
n=100 |
26 |
Correct |
13 ms |
24400 KB |
n=12 |
27 |
Correct |
12 ms |
24568 KB |
n=100 |
28 |
Correct |
15 ms |
24496 KB |
n=500 |
29 |
Correct |
12 ms |
24656 KB |
n=500 |
30 |
Correct |
12 ms |
24656 KB |
n=500 |
31 |
Correct |
12 ms |
24656 KB |
n=500 |
32 |
Correct |
14 ms |
24400 KB |
n=500 |
33 |
Correct |
11 ms |
24656 KB |
n=500 |
34 |
Correct |
11 ms |
24656 KB |
n=500 |
35 |
Correct |
12 ms |
24656 KB |
n=500 |
36 |
Correct |
10 ms |
24824 KB |
n=500 |
37 |
Correct |
12 ms |
24400 KB |
n=500 |
38 |
Correct |
13 ms |
24400 KB |
n=500 |
39 |
Correct |
10 ms |
24400 KB |
n=500 |
40 |
Correct |
11 ms |
24568 KB |
n=500 |
41 |
Correct |
12 ms |
24656 KB |
n=500 |
42 |
Correct |
14 ms |
24400 KB |
n=500 |
43 |
Correct |
13 ms |
24400 KB |
n=500 |
44 |
Correct |
18 ms |
24604 KB |
n=500 |
45 |
Correct |
12 ms |
24400 KB |
n=500 |
46 |
Correct |
13 ms |
24656 KB |
n=500 |
47 |
Correct |
13 ms |
24656 KB |
n=500 |
48 |
Correct |
11 ms |
24656 KB |
n=500 |
49 |
Correct |
13 ms |
24656 KB |
n=500 |
50 |
Correct |
12 ms |
24400 KB |
n=500 |
51 |
Correct |
12 ms |
24656 KB |
n=500 |
52 |
Correct |
10 ms |
24656 KB |
n=500 |
53 |
Correct |
13 ms |
24656 KB |
n=500 |
54 |
Correct |
13 ms |
24656 KB |
n=500 |
55 |
Correct |
13 ms |
24400 KB |
n=278 |
56 |
Correct |
13 ms |
24668 KB |
n=500 |
57 |
Correct |
12 ms |
24656 KB |
n=500 |
58 |
Correct |
12 ms |
24824 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24400 KB |
n=5 |
2 |
Correct |
14 ms |
24516 KB |
n=100 |
3 |
Correct |
11 ms |
24400 KB |
n=100 |
4 |
Correct |
12 ms |
24400 KB |
n=100 |
5 |
Correct |
12 ms |
24400 KB |
n=100 |
6 |
Correct |
11 ms |
24400 KB |
n=100 |
7 |
Correct |
11 ms |
24400 KB |
n=100 |
8 |
Correct |
9 ms |
24400 KB |
n=100 |
9 |
Correct |
9 ms |
24400 KB |
n=100 |
10 |
Correct |
10 ms |
24568 KB |
n=100 |
11 |
Correct |
11 ms |
24400 KB |
n=100 |
12 |
Correct |
12 ms |
24400 KB |
n=100 |
13 |
Correct |
12 ms |
24400 KB |
n=100 |
14 |
Correct |
11 ms |
24400 KB |
n=100 |
15 |
Correct |
10 ms |
24400 KB |
n=100 |
16 |
Correct |
11 ms |
24400 KB |
n=100 |
17 |
Correct |
11 ms |
24400 KB |
n=100 |
18 |
Correct |
12 ms |
24400 KB |
n=100 |
19 |
Correct |
12 ms |
24568 KB |
n=100 |
20 |
Correct |
12 ms |
24400 KB |
n=100 |
21 |
Correct |
11 ms |
24400 KB |
n=100 |
22 |
Correct |
10 ms |
24400 KB |
n=100 |
23 |
Correct |
12 ms |
24400 KB |
n=100 |
24 |
Correct |
13 ms |
24572 KB |
n=100 |
25 |
Correct |
11 ms |
24400 KB |
n=100 |
26 |
Correct |
13 ms |
24400 KB |
n=12 |
27 |
Correct |
12 ms |
24568 KB |
n=100 |
28 |
Correct |
15 ms |
24496 KB |
n=500 |
29 |
Correct |
12 ms |
24656 KB |
n=500 |
30 |
Correct |
12 ms |
24656 KB |
n=500 |
31 |
Correct |
12 ms |
24656 KB |
n=500 |
32 |
Correct |
14 ms |
24400 KB |
n=500 |
33 |
Correct |
11 ms |
24656 KB |
n=500 |
34 |
Correct |
11 ms |
24656 KB |
n=500 |
35 |
Correct |
12 ms |
24656 KB |
n=500 |
36 |
Correct |
10 ms |
24824 KB |
n=500 |
37 |
Correct |
12 ms |
24400 KB |
n=500 |
38 |
Correct |
13 ms |
24400 KB |
n=500 |
39 |
Correct |
10 ms |
24400 KB |
n=500 |
40 |
Correct |
11 ms |
24568 KB |
n=500 |
41 |
Correct |
12 ms |
24656 KB |
n=500 |
42 |
Correct |
14 ms |
24400 KB |
n=500 |
43 |
Correct |
13 ms |
24400 KB |
n=500 |
44 |
Correct |
18 ms |
24604 KB |
n=500 |
45 |
Correct |
12 ms |
24400 KB |
n=500 |
46 |
Correct |
13 ms |
24656 KB |
n=500 |
47 |
Correct |
13 ms |
24656 KB |
n=500 |
48 |
Correct |
11 ms |
24656 KB |
n=500 |
49 |
Correct |
13 ms |
24656 KB |
n=500 |
50 |
Correct |
12 ms |
24400 KB |
n=500 |
51 |
Correct |
12 ms |
24656 KB |
n=500 |
52 |
Correct |
10 ms |
24656 KB |
n=500 |
53 |
Correct |
13 ms |
24656 KB |
n=500 |
54 |
Correct |
13 ms |
24656 KB |
n=500 |
55 |
Correct |
13 ms |
24400 KB |
n=278 |
56 |
Correct |
13 ms |
24668 KB |
n=500 |
57 |
Correct |
12 ms |
24656 KB |
n=500 |
58 |
Correct |
12 ms |
24824 KB |
n=500 |
59 |
Correct |
14 ms |
24912 KB |
n=2000 |
60 |
Correct |
17 ms |
24912 KB |
n=2000 |
61 |
Correct |
14 ms |
24912 KB |
n=2000 |
62 |
Correct |
15 ms |
24912 KB |
n=2000 |
63 |
Correct |
15 ms |
24912 KB |
n=2000 |
64 |
Correct |
12 ms |
24912 KB |
n=2000 |
65 |
Correct |
15 ms |
25080 KB |
n=2000 |
66 |
Correct |
14 ms |
24912 KB |
n=2000 |
67 |
Correct |
12 ms |
24912 KB |
n=2000 |
68 |
Correct |
16 ms |
24912 KB |
n=2000 |
69 |
Correct |
15 ms |
24912 KB |
n=2000 |
70 |
Correct |
12 ms |
24912 KB |
n=2000 |
71 |
Correct |
13 ms |
24912 KB |
n=2000 |
72 |
Correct |
12 ms |
24912 KB |
n=2000 |
73 |
Correct |
12 ms |
24912 KB |
n=2000 |
74 |
Correct |
15 ms |
24912 KB |
n=1844 |
75 |
Correct |
16 ms |
24912 KB |
n=2000 |
76 |
Correct |
16 ms |
24912 KB |
n=2000 |
77 |
Correct |
15 ms |
24912 KB |
n=2000 |
78 |
Correct |
16 ms |
24912 KB |
n=2000 |
79 |
Correct |
15 ms |
25080 KB |
n=2000 |
80 |
Correct |
16 ms |
24912 KB |
n=2000 |
81 |
Correct |
13 ms |
24912 KB |
n=2000 |
82 |
Correct |
13 ms |
24912 KB |
n=2000 |
83 |
Correct |
12 ms |
24912 KB |
n=2000 |
84 |
Correct |
15 ms |
24912 KB |
n=2000 |
85 |
Correct |
15 ms |
24912 KB |
n=2000 |
86 |
Correct |
16 ms |
24912 KB |
n=2000 |
87 |
Correct |
14 ms |
24912 KB |
n=2000 |
88 |
Correct |
14 ms |
24912 KB |
n=2000 |
89 |
Correct |
12 ms |
24912 KB |
n=2000 |
90 |
Correct |
12 ms |
24912 KB |
n=2000 |
91 |
Correct |
14 ms |
25080 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24400 KB |
n=5 |
2 |
Correct |
14 ms |
24516 KB |
n=100 |
3 |
Correct |
11 ms |
24400 KB |
n=100 |
4 |
Correct |
12 ms |
24400 KB |
n=100 |
5 |
Correct |
12 ms |
24400 KB |
n=100 |
6 |
Correct |
11 ms |
24400 KB |
n=100 |
7 |
Correct |
11 ms |
24400 KB |
n=100 |
8 |
Correct |
9 ms |
24400 KB |
n=100 |
9 |
Correct |
9 ms |
24400 KB |
n=100 |
10 |
Correct |
10 ms |
24568 KB |
n=100 |
11 |
Correct |
11 ms |
24400 KB |
n=100 |
12 |
Correct |
12 ms |
24400 KB |
n=100 |
13 |
Correct |
12 ms |
24400 KB |
n=100 |
14 |
Correct |
11 ms |
24400 KB |
n=100 |
15 |
Correct |
10 ms |
24400 KB |
n=100 |
16 |
Correct |
11 ms |
24400 KB |
n=100 |
17 |
Correct |
11 ms |
24400 KB |
n=100 |
18 |
Correct |
12 ms |
24400 KB |
n=100 |
19 |
Correct |
12 ms |
24568 KB |
n=100 |
20 |
Correct |
12 ms |
24400 KB |
n=100 |
21 |
Correct |
11 ms |
24400 KB |
n=100 |
22 |
Correct |
10 ms |
24400 KB |
n=100 |
23 |
Correct |
12 ms |
24400 KB |
n=100 |
24 |
Correct |
13 ms |
24572 KB |
n=100 |
25 |
Correct |
11 ms |
24400 KB |
n=100 |
26 |
Correct |
13 ms |
24400 KB |
n=12 |
27 |
Correct |
12 ms |
24568 KB |
n=100 |
28 |
Correct |
15 ms |
24496 KB |
n=500 |
29 |
Correct |
12 ms |
24656 KB |
n=500 |
30 |
Correct |
12 ms |
24656 KB |
n=500 |
31 |
Correct |
12 ms |
24656 KB |
n=500 |
32 |
Correct |
14 ms |
24400 KB |
n=500 |
33 |
Correct |
11 ms |
24656 KB |
n=500 |
34 |
Correct |
11 ms |
24656 KB |
n=500 |
35 |
Correct |
12 ms |
24656 KB |
n=500 |
36 |
Correct |
10 ms |
24824 KB |
n=500 |
37 |
Correct |
12 ms |
24400 KB |
n=500 |
38 |
Correct |
13 ms |
24400 KB |
n=500 |
39 |
Correct |
10 ms |
24400 KB |
n=500 |
40 |
Correct |
11 ms |
24568 KB |
n=500 |
41 |
Correct |
12 ms |
24656 KB |
n=500 |
42 |
Correct |
14 ms |
24400 KB |
n=500 |
43 |
Correct |
13 ms |
24400 KB |
n=500 |
44 |
Correct |
18 ms |
24604 KB |
n=500 |
45 |
Correct |
12 ms |
24400 KB |
n=500 |
46 |
Correct |
13 ms |
24656 KB |
n=500 |
47 |
Correct |
13 ms |
24656 KB |
n=500 |
48 |
Correct |
11 ms |
24656 KB |
n=500 |
49 |
Correct |
13 ms |
24656 KB |
n=500 |
50 |
Correct |
12 ms |
24400 KB |
n=500 |
51 |
Correct |
12 ms |
24656 KB |
n=500 |
52 |
Correct |
10 ms |
24656 KB |
n=500 |
53 |
Correct |
13 ms |
24656 KB |
n=500 |
54 |
Correct |
13 ms |
24656 KB |
n=500 |
55 |
Correct |
13 ms |
24400 KB |
n=278 |
56 |
Correct |
13 ms |
24668 KB |
n=500 |
57 |
Correct |
12 ms |
24656 KB |
n=500 |
58 |
Correct |
12 ms |
24824 KB |
n=500 |
59 |
Correct |
14 ms |
24912 KB |
n=2000 |
60 |
Correct |
17 ms |
24912 KB |
n=2000 |
61 |
Correct |
14 ms |
24912 KB |
n=2000 |
62 |
Correct |
15 ms |
24912 KB |
n=2000 |
63 |
Correct |
15 ms |
24912 KB |
n=2000 |
64 |
Correct |
12 ms |
24912 KB |
n=2000 |
65 |
Correct |
15 ms |
25080 KB |
n=2000 |
66 |
Correct |
14 ms |
24912 KB |
n=2000 |
67 |
Correct |
12 ms |
24912 KB |
n=2000 |
68 |
Correct |
16 ms |
24912 KB |
n=2000 |
69 |
Correct |
15 ms |
24912 KB |
n=2000 |
70 |
Correct |
12 ms |
24912 KB |
n=2000 |
71 |
Correct |
13 ms |
24912 KB |
n=2000 |
72 |
Correct |
12 ms |
24912 KB |
n=2000 |
73 |
Correct |
12 ms |
24912 KB |
n=2000 |
74 |
Correct |
15 ms |
24912 KB |
n=1844 |
75 |
Correct |
16 ms |
24912 KB |
n=2000 |
76 |
Correct |
16 ms |
24912 KB |
n=2000 |
77 |
Correct |
15 ms |
24912 KB |
n=2000 |
78 |
Correct |
16 ms |
24912 KB |
n=2000 |
79 |
Correct |
15 ms |
25080 KB |
n=2000 |
80 |
Correct |
16 ms |
24912 KB |
n=2000 |
81 |
Correct |
13 ms |
24912 KB |
n=2000 |
82 |
Correct |
13 ms |
24912 KB |
n=2000 |
83 |
Correct |
12 ms |
24912 KB |
n=2000 |
84 |
Correct |
15 ms |
24912 KB |
n=2000 |
85 |
Correct |
15 ms |
24912 KB |
n=2000 |
86 |
Correct |
16 ms |
24912 KB |
n=2000 |
87 |
Correct |
14 ms |
24912 KB |
n=2000 |
88 |
Correct |
14 ms |
24912 KB |
n=2000 |
89 |
Correct |
12 ms |
24912 KB |
n=2000 |
90 |
Correct |
12 ms |
24912 KB |
n=2000 |
91 |
Correct |
14 ms |
25080 KB |
n=2000 |
92 |
Correct |
561 ms |
69708 KB |
n=200000 |
93 |
Correct |
567 ms |
76684 KB |
n=200000 |
94 |
Correct |
386 ms |
81992 KB |
n=200000 |
95 |
Correct |
550 ms |
69564 KB |
n=200000 |
96 |
Correct |
548 ms |
69564 KB |
n=200000 |
97 |
Correct |
717 ms |
74996 KB |
n=200000 |
98 |
Correct |
619 ms |
69520 KB |
n=200000 |
99 |
Correct |
795 ms |
68960 KB |
n=200000 |
100 |
Correct |
729 ms |
69584 KB |
n=200000 |
101 |
Correct |
411 ms |
84132 KB |
n=200000 |
102 |
Correct |
331 ms |
70456 KB |
n=200000 |
103 |
Correct |
305 ms |
70512 KB |
n=200000 |
104 |
Correct |
307 ms |
70472 KB |
n=200000 |
105 |
Correct |
325 ms |
70216 KB |
n=200000 |
106 |
Correct |
318 ms |
70216 KB |
n=200000 |
107 |
Correct |
335 ms |
70216 KB |
n=200000 |
108 |
Correct |
597 ms |
69200 KB |
n=200000 |
109 |
Correct |
634 ms |
69124 KB |
n=200000 |
110 |
Correct |
677 ms |
69280 KB |
n=200000 |
111 |
Correct |
647 ms |
69476 KB |
n=200000 |
112 |
Correct |
413 ms |
82528 KB |
n=200000 |
113 |
Correct |
671 ms |
74896 KB |
n=200000 |
114 |
Correct |
671 ms |
69412 KB |
n=200000 |
115 |
Correct |
863 ms |
71276 KB |
n=200000 |
116 |
Correct |
525 ms |
69292 KB |
n=200000 |
117 |
Correct |
346 ms |
83060 KB |
n=200000 |
118 |
Correct |
720 ms |
73032 KB |
n=200000 |
119 |
Correct |
587 ms |
69248 KB |
n=200000 |
120 |
Correct |
267 ms |
84000 KB |
n=200000 |
121 |
Correct |
277 ms |
83784 KB |
n=200000 |
122 |
Correct |
297 ms |
84404 KB |
n=200000 |
123 |
Correct |
321 ms |
69960 KB |
n=200000 |
124 |
Correct |
139 ms |
38216 KB |
n=25264 |