#include <bits/stdc++.h>
#define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using namespace std;
using ll = long long;
const int MAXN = 2e5 + 5, K = 20;
int n, m, q, timer;
int a[MAXN], tin[2 * MAXN], tout[2 * MAXN], par[MAXN][K];
vector<int> g[MAXN];
void dfs(int u, int p) {
tin[u] = timer++;
for (int j = 1; j < K; j++)
par[u][j] = par[par[u][j - 1]][j - 1];
for (const auto &v : g[u]) {
if (v == p) continue;
par[v][0] = u;
dfs(v, u);
}
tout[u] = timer - 1;
}
bool isAnc(int u, int v) {
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int query(int u, int v) {
if (isAnc(u, v)) return u;
if (isAnc(v, u)) return v;
for (int j = K - 1; j >= 0; j--)
if (!isAnc(par[u][j], v))
u = par[u][j];
return par[u][0];
}
void solve() {
cin >> n >> m >> q;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
timer = 0;
dfs(0, -1);
set<pair<int, int>> individual;
vector<set<int>> lca(n + 1);
for (int i = 0; i < m; i++) {
cin >> a[i];
a[i]--;
individual.emplace(a[i], i);
}
for (int i = 0; i < m - 1; i++) {
lca[query(a[i], a[i + 1])].insert(i);
}
while (q--) {
int type;
cin >> type;
if (type == 1) {
int pos, val;
cin >> pos >> val;
pos--, val--;
individual.erase(make_pair(a[pos], pos));
if (pos > 0) lca[query(a[pos], a[pos - 1])].erase(pos - 1);
if (pos < m - 1) lca[query(a[pos], a[pos + 1])].erase(pos);
a[pos] = val;
individual.emplace(a[pos], pos);
if (pos > 0) lca[query(a[pos], a[pos - 1])].insert(pos - 1);
if (pos < m - 1) lca[query(a[pos], a[pos + 1])].insert(pos);
} else {
int l, r, val;
cin >> l >> r >> val;
l--, r--, val--;
auto single = individual.lower_bound(make_pair(val, l));
if (single != individual.end()) {
int idx = (*single).second;
if ((*single).first == val && l <= idx && idx <= r) {
cout << idx + 1 << " " << idx + 1 << "\n";
continue;
}
}
auto adjacent = lca[val].lower_bound(l);
if (adjacent == lca[val].end() || *adjacent >= r) {
cout << "-1 -1\n";
continue;
}
cout << *adjacent + 1 << " " << *adjacent + 2 << "\n";
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc = 1;
// cin >> tc;
while (tc--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
n=5 |
2 |
Correct |
4 ms |
5100 KB |
n=100 |
3 |
Correct |
4 ms |
5100 KB |
n=100 |
4 |
Correct |
3 ms |
5100 KB |
n=100 |
5 |
Correct |
3 ms |
5100 KB |
n=100 |
6 |
Correct |
5 ms |
5100 KB |
n=100 |
7 |
Correct |
4 ms |
5100 KB |
n=100 |
8 |
Correct |
3 ms |
5100 KB |
n=100 |
9 |
Correct |
3 ms |
5100 KB |
n=100 |
10 |
Correct |
4 ms |
5120 KB |
n=100 |
11 |
Correct |
5 ms |
5100 KB |
n=100 |
12 |
Correct |
4 ms |
5100 KB |
n=100 |
13 |
Correct |
5 ms |
5100 KB |
n=100 |
14 |
Correct |
4 ms |
5100 KB |
n=100 |
15 |
Correct |
4 ms |
5100 KB |
n=100 |
16 |
Correct |
4 ms |
5100 KB |
n=100 |
17 |
Correct |
4 ms |
5100 KB |
n=100 |
18 |
Correct |
4 ms |
5100 KB |
n=100 |
19 |
Correct |
4 ms |
5100 KB |
n=100 |
20 |
Correct |
5 ms |
5100 KB |
n=100 |
21 |
Correct |
4 ms |
5100 KB |
n=100 |
22 |
Correct |
4 ms |
5100 KB |
n=100 |
23 |
Correct |
4 ms |
5100 KB |
n=100 |
24 |
Correct |
5 ms |
5100 KB |
n=100 |
25 |
Correct |
4 ms |
5100 KB |
n=100 |
26 |
Correct |
4 ms |
5100 KB |
n=12 |
27 |
Correct |
4 ms |
5100 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
n=5 |
2 |
Correct |
4 ms |
5100 KB |
n=100 |
3 |
Correct |
4 ms |
5100 KB |
n=100 |
4 |
Correct |
3 ms |
5100 KB |
n=100 |
5 |
Correct |
3 ms |
5100 KB |
n=100 |
6 |
Correct |
5 ms |
5100 KB |
n=100 |
7 |
Correct |
4 ms |
5100 KB |
n=100 |
8 |
Correct |
3 ms |
5100 KB |
n=100 |
9 |
Correct |
3 ms |
5100 KB |
n=100 |
10 |
Correct |
4 ms |
5120 KB |
n=100 |
11 |
Correct |
5 ms |
5100 KB |
n=100 |
12 |
Correct |
4 ms |
5100 KB |
n=100 |
13 |
Correct |
5 ms |
5100 KB |
n=100 |
14 |
Correct |
4 ms |
5100 KB |
n=100 |
15 |
Correct |
4 ms |
5100 KB |
n=100 |
16 |
Correct |
4 ms |
5100 KB |
n=100 |
17 |
Correct |
4 ms |
5100 KB |
n=100 |
18 |
Correct |
4 ms |
5100 KB |
n=100 |
19 |
Correct |
4 ms |
5100 KB |
n=100 |
20 |
Correct |
5 ms |
5100 KB |
n=100 |
21 |
Correct |
4 ms |
5100 KB |
n=100 |
22 |
Correct |
4 ms |
5100 KB |
n=100 |
23 |
Correct |
4 ms |
5100 KB |
n=100 |
24 |
Correct |
5 ms |
5100 KB |
n=100 |
25 |
Correct |
4 ms |
5100 KB |
n=100 |
26 |
Correct |
4 ms |
5100 KB |
n=12 |
27 |
Correct |
4 ms |
5100 KB |
n=100 |
28 |
Correct |
5 ms |
5228 KB |
n=500 |
29 |
Correct |
5 ms |
5356 KB |
n=500 |
30 |
Correct |
5 ms |
5228 KB |
n=500 |
31 |
Correct |
5 ms |
5284 KB |
n=500 |
32 |
Correct |
5 ms |
5228 KB |
n=500 |
33 |
Correct |
5 ms |
5228 KB |
n=500 |
34 |
Correct |
5 ms |
5228 KB |
n=500 |
35 |
Correct |
5 ms |
5228 KB |
n=500 |
36 |
Correct |
5 ms |
5228 KB |
n=500 |
37 |
Correct |
4 ms |
5228 KB |
n=500 |
38 |
Correct |
4 ms |
5228 KB |
n=500 |
39 |
Correct |
5 ms |
5228 KB |
n=500 |
40 |
Correct |
5 ms |
5248 KB |
n=500 |
41 |
Correct |
4 ms |
5228 KB |
n=500 |
42 |
Correct |
5 ms |
5228 KB |
n=500 |
43 |
Correct |
5 ms |
5228 KB |
n=500 |
44 |
Correct |
5 ms |
5228 KB |
n=500 |
45 |
Correct |
5 ms |
5228 KB |
n=500 |
46 |
Correct |
5 ms |
5228 KB |
n=500 |
47 |
Correct |
5 ms |
5228 KB |
n=500 |
48 |
Correct |
5 ms |
5228 KB |
n=500 |
49 |
Correct |
4 ms |
5228 KB |
n=500 |
50 |
Correct |
5 ms |
5228 KB |
n=500 |
51 |
Correct |
5 ms |
5228 KB |
n=500 |
52 |
Correct |
5 ms |
5228 KB |
n=500 |
53 |
Correct |
5 ms |
5228 KB |
n=500 |
54 |
Correct |
5 ms |
5228 KB |
n=500 |
55 |
Correct |
4 ms |
5100 KB |
n=278 |
56 |
Correct |
4 ms |
5228 KB |
n=500 |
57 |
Correct |
5 ms |
5228 KB |
n=500 |
58 |
Correct |
5 ms |
5228 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
n=5 |
2 |
Correct |
4 ms |
5100 KB |
n=100 |
3 |
Correct |
4 ms |
5100 KB |
n=100 |
4 |
Correct |
3 ms |
5100 KB |
n=100 |
5 |
Correct |
3 ms |
5100 KB |
n=100 |
6 |
Correct |
5 ms |
5100 KB |
n=100 |
7 |
Correct |
4 ms |
5100 KB |
n=100 |
8 |
Correct |
3 ms |
5100 KB |
n=100 |
9 |
Correct |
3 ms |
5100 KB |
n=100 |
10 |
Correct |
4 ms |
5120 KB |
n=100 |
11 |
Correct |
5 ms |
5100 KB |
n=100 |
12 |
Correct |
4 ms |
5100 KB |
n=100 |
13 |
Correct |
5 ms |
5100 KB |
n=100 |
14 |
Correct |
4 ms |
5100 KB |
n=100 |
15 |
Correct |
4 ms |
5100 KB |
n=100 |
16 |
Correct |
4 ms |
5100 KB |
n=100 |
17 |
Correct |
4 ms |
5100 KB |
n=100 |
18 |
Correct |
4 ms |
5100 KB |
n=100 |
19 |
Correct |
4 ms |
5100 KB |
n=100 |
20 |
Correct |
5 ms |
5100 KB |
n=100 |
21 |
Correct |
4 ms |
5100 KB |
n=100 |
22 |
Correct |
4 ms |
5100 KB |
n=100 |
23 |
Correct |
4 ms |
5100 KB |
n=100 |
24 |
Correct |
5 ms |
5100 KB |
n=100 |
25 |
Correct |
4 ms |
5100 KB |
n=100 |
26 |
Correct |
4 ms |
5100 KB |
n=12 |
27 |
Correct |
4 ms |
5100 KB |
n=100 |
28 |
Correct |
5 ms |
5228 KB |
n=500 |
29 |
Correct |
5 ms |
5356 KB |
n=500 |
30 |
Correct |
5 ms |
5228 KB |
n=500 |
31 |
Correct |
5 ms |
5284 KB |
n=500 |
32 |
Correct |
5 ms |
5228 KB |
n=500 |
33 |
Correct |
5 ms |
5228 KB |
n=500 |
34 |
Correct |
5 ms |
5228 KB |
n=500 |
35 |
Correct |
5 ms |
5228 KB |
n=500 |
36 |
Correct |
5 ms |
5228 KB |
n=500 |
37 |
Correct |
4 ms |
5228 KB |
n=500 |
38 |
Correct |
4 ms |
5228 KB |
n=500 |
39 |
Correct |
5 ms |
5228 KB |
n=500 |
40 |
Correct |
5 ms |
5248 KB |
n=500 |
41 |
Correct |
4 ms |
5228 KB |
n=500 |
42 |
Correct |
5 ms |
5228 KB |
n=500 |
43 |
Correct |
5 ms |
5228 KB |
n=500 |
44 |
Correct |
5 ms |
5228 KB |
n=500 |
45 |
Correct |
5 ms |
5228 KB |
n=500 |
46 |
Correct |
5 ms |
5228 KB |
n=500 |
47 |
Correct |
5 ms |
5228 KB |
n=500 |
48 |
Correct |
5 ms |
5228 KB |
n=500 |
49 |
Correct |
4 ms |
5228 KB |
n=500 |
50 |
Correct |
5 ms |
5228 KB |
n=500 |
51 |
Correct |
5 ms |
5228 KB |
n=500 |
52 |
Correct |
5 ms |
5228 KB |
n=500 |
53 |
Correct |
5 ms |
5228 KB |
n=500 |
54 |
Correct |
5 ms |
5228 KB |
n=500 |
55 |
Correct |
4 ms |
5100 KB |
n=278 |
56 |
Correct |
4 ms |
5228 KB |
n=500 |
57 |
Correct |
5 ms |
5228 KB |
n=500 |
58 |
Correct |
5 ms |
5228 KB |
n=500 |
59 |
Correct |
9 ms |
5612 KB |
n=2000 |
60 |
Correct |
8 ms |
5740 KB |
n=2000 |
61 |
Correct |
8 ms |
5740 KB |
n=2000 |
62 |
Correct |
8 ms |
5740 KB |
n=2000 |
63 |
Correct |
9 ms |
5612 KB |
n=2000 |
64 |
Correct |
8 ms |
5740 KB |
n=2000 |
65 |
Correct |
10 ms |
5624 KB |
n=2000 |
66 |
Correct |
8 ms |
5760 KB |
n=2000 |
67 |
Correct |
9 ms |
5612 KB |
n=2000 |
68 |
Correct |
8 ms |
5740 KB |
n=2000 |
69 |
Correct |
7 ms |
5740 KB |
n=2000 |
70 |
Correct |
7 ms |
5612 KB |
n=2000 |
71 |
Correct |
7 ms |
5612 KB |
n=2000 |
72 |
Correct |
7 ms |
5612 KB |
n=2000 |
73 |
Correct |
7 ms |
5612 KB |
n=2000 |
74 |
Correct |
8 ms |
5612 KB |
n=1844 |
75 |
Correct |
7 ms |
5632 KB |
n=2000 |
76 |
Correct |
9 ms |
5612 KB |
n=2000 |
77 |
Correct |
9 ms |
5612 KB |
n=2000 |
78 |
Correct |
8 ms |
5612 KB |
n=2000 |
79 |
Correct |
8 ms |
5612 KB |
n=2000 |
80 |
Correct |
7 ms |
5740 KB |
n=2000 |
81 |
Correct |
8 ms |
5740 KB |
n=2000 |
82 |
Correct |
8 ms |
5612 KB |
n=2000 |
83 |
Correct |
8 ms |
5740 KB |
n=2000 |
84 |
Correct |
8 ms |
5612 KB |
n=2000 |
85 |
Correct |
8 ms |
5740 KB |
n=2000 |
86 |
Correct |
9 ms |
5632 KB |
n=2000 |
87 |
Correct |
9 ms |
5612 KB |
n=2000 |
88 |
Correct |
14 ms |
5740 KB |
n=2000 |
89 |
Correct |
7 ms |
5740 KB |
n=2000 |
90 |
Correct |
7 ms |
5740 KB |
n=2000 |
91 |
Correct |
9 ms |
5612 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
n=5 |
2 |
Correct |
4 ms |
5100 KB |
n=100 |
3 |
Correct |
4 ms |
5100 KB |
n=100 |
4 |
Correct |
3 ms |
5100 KB |
n=100 |
5 |
Correct |
3 ms |
5100 KB |
n=100 |
6 |
Correct |
5 ms |
5100 KB |
n=100 |
7 |
Correct |
4 ms |
5100 KB |
n=100 |
8 |
Correct |
3 ms |
5100 KB |
n=100 |
9 |
Correct |
3 ms |
5100 KB |
n=100 |
10 |
Correct |
4 ms |
5120 KB |
n=100 |
11 |
Correct |
5 ms |
5100 KB |
n=100 |
12 |
Correct |
4 ms |
5100 KB |
n=100 |
13 |
Correct |
5 ms |
5100 KB |
n=100 |
14 |
Correct |
4 ms |
5100 KB |
n=100 |
15 |
Correct |
4 ms |
5100 KB |
n=100 |
16 |
Correct |
4 ms |
5100 KB |
n=100 |
17 |
Correct |
4 ms |
5100 KB |
n=100 |
18 |
Correct |
4 ms |
5100 KB |
n=100 |
19 |
Correct |
4 ms |
5100 KB |
n=100 |
20 |
Correct |
5 ms |
5100 KB |
n=100 |
21 |
Correct |
4 ms |
5100 KB |
n=100 |
22 |
Correct |
4 ms |
5100 KB |
n=100 |
23 |
Correct |
4 ms |
5100 KB |
n=100 |
24 |
Correct |
5 ms |
5100 KB |
n=100 |
25 |
Correct |
4 ms |
5100 KB |
n=100 |
26 |
Correct |
4 ms |
5100 KB |
n=12 |
27 |
Correct |
4 ms |
5100 KB |
n=100 |
28 |
Correct |
5 ms |
5228 KB |
n=500 |
29 |
Correct |
5 ms |
5356 KB |
n=500 |
30 |
Correct |
5 ms |
5228 KB |
n=500 |
31 |
Correct |
5 ms |
5284 KB |
n=500 |
32 |
Correct |
5 ms |
5228 KB |
n=500 |
33 |
Correct |
5 ms |
5228 KB |
n=500 |
34 |
Correct |
5 ms |
5228 KB |
n=500 |
35 |
Correct |
5 ms |
5228 KB |
n=500 |
36 |
Correct |
5 ms |
5228 KB |
n=500 |
37 |
Correct |
4 ms |
5228 KB |
n=500 |
38 |
Correct |
4 ms |
5228 KB |
n=500 |
39 |
Correct |
5 ms |
5228 KB |
n=500 |
40 |
Correct |
5 ms |
5248 KB |
n=500 |
41 |
Correct |
4 ms |
5228 KB |
n=500 |
42 |
Correct |
5 ms |
5228 KB |
n=500 |
43 |
Correct |
5 ms |
5228 KB |
n=500 |
44 |
Correct |
5 ms |
5228 KB |
n=500 |
45 |
Correct |
5 ms |
5228 KB |
n=500 |
46 |
Correct |
5 ms |
5228 KB |
n=500 |
47 |
Correct |
5 ms |
5228 KB |
n=500 |
48 |
Correct |
5 ms |
5228 KB |
n=500 |
49 |
Correct |
4 ms |
5228 KB |
n=500 |
50 |
Correct |
5 ms |
5228 KB |
n=500 |
51 |
Correct |
5 ms |
5228 KB |
n=500 |
52 |
Correct |
5 ms |
5228 KB |
n=500 |
53 |
Correct |
5 ms |
5228 KB |
n=500 |
54 |
Correct |
5 ms |
5228 KB |
n=500 |
55 |
Correct |
4 ms |
5100 KB |
n=278 |
56 |
Correct |
4 ms |
5228 KB |
n=500 |
57 |
Correct |
5 ms |
5228 KB |
n=500 |
58 |
Correct |
5 ms |
5228 KB |
n=500 |
59 |
Correct |
9 ms |
5612 KB |
n=2000 |
60 |
Correct |
8 ms |
5740 KB |
n=2000 |
61 |
Correct |
8 ms |
5740 KB |
n=2000 |
62 |
Correct |
8 ms |
5740 KB |
n=2000 |
63 |
Correct |
9 ms |
5612 KB |
n=2000 |
64 |
Correct |
8 ms |
5740 KB |
n=2000 |
65 |
Correct |
10 ms |
5624 KB |
n=2000 |
66 |
Correct |
8 ms |
5760 KB |
n=2000 |
67 |
Correct |
9 ms |
5612 KB |
n=2000 |
68 |
Correct |
8 ms |
5740 KB |
n=2000 |
69 |
Correct |
7 ms |
5740 KB |
n=2000 |
70 |
Correct |
7 ms |
5612 KB |
n=2000 |
71 |
Correct |
7 ms |
5612 KB |
n=2000 |
72 |
Correct |
7 ms |
5612 KB |
n=2000 |
73 |
Correct |
7 ms |
5612 KB |
n=2000 |
74 |
Correct |
8 ms |
5612 KB |
n=1844 |
75 |
Correct |
7 ms |
5632 KB |
n=2000 |
76 |
Correct |
9 ms |
5612 KB |
n=2000 |
77 |
Correct |
9 ms |
5612 KB |
n=2000 |
78 |
Correct |
8 ms |
5612 KB |
n=2000 |
79 |
Correct |
8 ms |
5612 KB |
n=2000 |
80 |
Correct |
7 ms |
5740 KB |
n=2000 |
81 |
Correct |
8 ms |
5740 KB |
n=2000 |
82 |
Correct |
8 ms |
5612 KB |
n=2000 |
83 |
Correct |
8 ms |
5740 KB |
n=2000 |
84 |
Correct |
8 ms |
5612 KB |
n=2000 |
85 |
Correct |
8 ms |
5740 KB |
n=2000 |
86 |
Correct |
9 ms |
5632 KB |
n=2000 |
87 |
Correct |
9 ms |
5612 KB |
n=2000 |
88 |
Correct |
14 ms |
5740 KB |
n=2000 |
89 |
Correct |
7 ms |
5740 KB |
n=2000 |
90 |
Correct |
7 ms |
5740 KB |
n=2000 |
91 |
Correct |
9 ms |
5612 KB |
n=2000 |
92 |
Correct |
1135 ms |
66100 KB |
n=200000 |
93 |
Correct |
1271 ms |
72640 KB |
n=200000 |
94 |
Correct |
1024 ms |
77028 KB |
n=200000 |
95 |
Correct |
1116 ms |
65888 KB |
n=200000 |
96 |
Correct |
1041 ms |
66016 KB |
n=200000 |
97 |
Correct |
1382 ms |
71420 KB |
n=200000 |
98 |
Correct |
1047 ms |
66144 KB |
n=200000 |
99 |
Correct |
1243 ms |
66412 KB |
n=200000 |
100 |
Correct |
1094 ms |
66120 KB |
n=200000 |
101 |
Correct |
888 ms |
78692 KB |
n=200000 |
102 |
Correct |
578 ms |
67052 KB |
n=200000 |
103 |
Correct |
560 ms |
67052 KB |
n=200000 |
104 |
Correct |
681 ms |
67300 KB |
n=200000 |
105 |
Correct |
577 ms |
67692 KB |
n=200000 |
106 |
Correct |
568 ms |
67564 KB |
n=200000 |
107 |
Correct |
582 ms |
67564 KB |
n=200000 |
108 |
Correct |
1126 ms |
66028 KB |
n=200000 |
109 |
Correct |
1121 ms |
66028 KB |
n=200000 |
110 |
Correct |
1136 ms |
66028 KB |
n=200000 |
111 |
Correct |
959 ms |
65508 KB |
n=200000 |
112 |
Correct |
917 ms |
77348 KB |
n=200000 |
113 |
Correct |
1241 ms |
71244 KB |
n=200000 |
114 |
Correct |
956 ms |
65504 KB |
n=200000 |
115 |
Correct |
1511 ms |
68416 KB |
n=200000 |
116 |
Correct |
1047 ms |
66228 KB |
n=200000 |
117 |
Correct |
868 ms |
77804 KB |
n=200000 |
118 |
Correct |
1352 ms |
69868 KB |
n=200000 |
119 |
Correct |
1041 ms |
66260 KB |
n=200000 |
120 |
Correct |
826 ms |
77676 KB |
n=200000 |
121 |
Correct |
760 ms |
77676 KB |
n=200000 |
122 |
Correct |
779 ms |
77932 KB |
n=200000 |
123 |
Correct |
554 ms |
67180 KB |
n=200000 |
124 |
Correct |
234 ms |
21612 KB |
n=25264 |