#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 998244353;
const ll LOGN = 20;
const ll MAXN = 2e5 + 100;
vector<vector<int>> graph;
vector<int> A;
int depth[MAXN], up[LOGN][MAXN];
set<int> single[MAXN], two[MAXN];
void dfs(int node, int parent) {
for (auto u : graph[node]) {
if (u == parent)
continue;
depth[u] = depth[node] + 1;
up[0][u] = node;
for (int i = 1; i < LOGN; i++)
up[i][u] = up[i-1][up[i-1][u]];
dfs(u, node);
}
}
int find(int node, int k) {
for (int i = 0; i < LOGN; i++) {
if ((1<<i) & k)
node = up[i][node];
}
return node;
}
int lca(int a, int b) {
if (depth[b] > depth[a])
swap(a, b);
a = find(a, depth[a] - depth[b]);
if (a == b)
return a;
for (int i = LOGN - 1; i >= 0; i--) {
if (up[i][a] != up[i][b]) {
a = up[i][a];
b = up[i][b];
}
}
return up[0][a];
}
int main() {
fast
int n, m, q, a, b;
cin >> n >> m >> q;
graph = vector<vector<int>>(n+1, vector<int>());
A = vector<int>(n+1);
for (int i = 1; i < n; i++) {
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
for (int i = 0; i < LOGN; i++)
up[i][1] = 1;
dfs(1, 1);
for (int i = 1; i <= m; i++) {
cin >> A[i];
single[A[i]].insert(i);
}
for (int i = 2; i <= m; i++)
two[lca(A[i], A[i-1])].insert(i-1);
while (q--) {
int type;
cin >> type;
if (type == 1) {
int pos, v;
cin >> pos >> v;
single[A[pos]].erase(pos);
if (pos > 1)
two[lca(A[pos], A[pos-1])].erase(pos-1);
if (pos < m)
two[lca(A[pos], A[pos+1])].erase(pos);
A[pos] = v;
single[A[pos]].insert(pos);
if (pos > 1)
two[lca(A[pos], A[pos-1])].insert(pos-1);
if (pos < m)
two[lca(A[pos], A[pos+1])].insert(pos);
} else {
int x, y, low;
cin >> x >> y >> low;
auto it = single[low].lower_bound(x);
auto it2 = two[low].lower_bound(x);
if (it != single[low].end() && *it <= y)
cout << *it << " " << *it << "\n";
else if (it2 != two[low].end() && *it2 + 1 <= y)
cout << *it2 << " " << *it2 + 1 << "\n";
else
cout << "-1 -1\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
35672 KB |
n=5 |
2 |
Correct |
6 ms |
35416 KB |
n=100 |
3 |
Correct |
6 ms |
35488 KB |
n=100 |
4 |
Correct |
6 ms |
35488 KB |
n=100 |
5 |
Correct |
6 ms |
35416 KB |
n=100 |
6 |
Correct |
6 ms |
35420 KB |
n=100 |
7 |
Correct |
6 ms |
35708 KB |
n=100 |
8 |
Correct |
6 ms |
35416 KB |
n=100 |
9 |
Correct |
7 ms |
35420 KB |
n=100 |
10 |
Correct |
6 ms |
35420 KB |
n=100 |
11 |
Correct |
6 ms |
35520 KB |
n=100 |
12 |
Correct |
6 ms |
35420 KB |
n=100 |
13 |
Correct |
6 ms |
35528 KB |
n=100 |
14 |
Correct |
6 ms |
35420 KB |
n=100 |
15 |
Correct |
6 ms |
35420 KB |
n=100 |
16 |
Correct |
6 ms |
35420 KB |
n=100 |
17 |
Correct |
6 ms |
35420 KB |
n=100 |
18 |
Correct |
6 ms |
35420 KB |
n=100 |
19 |
Correct |
8 ms |
35928 KB |
n=100 |
20 |
Correct |
6 ms |
35420 KB |
n=100 |
21 |
Correct |
6 ms |
35420 KB |
n=100 |
22 |
Correct |
7 ms |
35420 KB |
n=100 |
23 |
Correct |
6 ms |
35420 KB |
n=100 |
24 |
Correct |
7 ms |
35420 KB |
n=100 |
25 |
Correct |
6 ms |
35416 KB |
n=100 |
26 |
Runtime error |
32 ms |
71696 KB |
Execution killed with signal 6 |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
35672 KB |
n=5 |
2 |
Correct |
6 ms |
35416 KB |
n=100 |
3 |
Correct |
6 ms |
35488 KB |
n=100 |
4 |
Correct |
6 ms |
35488 KB |
n=100 |
5 |
Correct |
6 ms |
35416 KB |
n=100 |
6 |
Correct |
6 ms |
35420 KB |
n=100 |
7 |
Correct |
6 ms |
35708 KB |
n=100 |
8 |
Correct |
6 ms |
35416 KB |
n=100 |
9 |
Correct |
7 ms |
35420 KB |
n=100 |
10 |
Correct |
6 ms |
35420 KB |
n=100 |
11 |
Correct |
6 ms |
35520 KB |
n=100 |
12 |
Correct |
6 ms |
35420 KB |
n=100 |
13 |
Correct |
6 ms |
35528 KB |
n=100 |
14 |
Correct |
6 ms |
35420 KB |
n=100 |
15 |
Correct |
6 ms |
35420 KB |
n=100 |
16 |
Correct |
6 ms |
35420 KB |
n=100 |
17 |
Correct |
6 ms |
35420 KB |
n=100 |
18 |
Correct |
6 ms |
35420 KB |
n=100 |
19 |
Correct |
8 ms |
35928 KB |
n=100 |
20 |
Correct |
6 ms |
35420 KB |
n=100 |
21 |
Correct |
6 ms |
35420 KB |
n=100 |
22 |
Correct |
7 ms |
35420 KB |
n=100 |
23 |
Correct |
6 ms |
35420 KB |
n=100 |
24 |
Correct |
7 ms |
35420 KB |
n=100 |
25 |
Correct |
6 ms |
35416 KB |
n=100 |
26 |
Runtime error |
32 ms |
71696 KB |
Execution killed with signal 6 |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
35672 KB |
n=5 |
2 |
Correct |
6 ms |
35416 KB |
n=100 |
3 |
Correct |
6 ms |
35488 KB |
n=100 |
4 |
Correct |
6 ms |
35488 KB |
n=100 |
5 |
Correct |
6 ms |
35416 KB |
n=100 |
6 |
Correct |
6 ms |
35420 KB |
n=100 |
7 |
Correct |
6 ms |
35708 KB |
n=100 |
8 |
Correct |
6 ms |
35416 KB |
n=100 |
9 |
Correct |
7 ms |
35420 KB |
n=100 |
10 |
Correct |
6 ms |
35420 KB |
n=100 |
11 |
Correct |
6 ms |
35520 KB |
n=100 |
12 |
Correct |
6 ms |
35420 KB |
n=100 |
13 |
Correct |
6 ms |
35528 KB |
n=100 |
14 |
Correct |
6 ms |
35420 KB |
n=100 |
15 |
Correct |
6 ms |
35420 KB |
n=100 |
16 |
Correct |
6 ms |
35420 KB |
n=100 |
17 |
Correct |
6 ms |
35420 KB |
n=100 |
18 |
Correct |
6 ms |
35420 KB |
n=100 |
19 |
Correct |
8 ms |
35928 KB |
n=100 |
20 |
Correct |
6 ms |
35420 KB |
n=100 |
21 |
Correct |
6 ms |
35420 KB |
n=100 |
22 |
Correct |
7 ms |
35420 KB |
n=100 |
23 |
Correct |
6 ms |
35420 KB |
n=100 |
24 |
Correct |
7 ms |
35420 KB |
n=100 |
25 |
Correct |
6 ms |
35416 KB |
n=100 |
26 |
Runtime error |
32 ms |
71696 KB |
Execution killed with signal 6 |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
35672 KB |
n=5 |
2 |
Correct |
6 ms |
35416 KB |
n=100 |
3 |
Correct |
6 ms |
35488 KB |
n=100 |
4 |
Correct |
6 ms |
35488 KB |
n=100 |
5 |
Correct |
6 ms |
35416 KB |
n=100 |
6 |
Correct |
6 ms |
35420 KB |
n=100 |
7 |
Correct |
6 ms |
35708 KB |
n=100 |
8 |
Correct |
6 ms |
35416 KB |
n=100 |
9 |
Correct |
7 ms |
35420 KB |
n=100 |
10 |
Correct |
6 ms |
35420 KB |
n=100 |
11 |
Correct |
6 ms |
35520 KB |
n=100 |
12 |
Correct |
6 ms |
35420 KB |
n=100 |
13 |
Correct |
6 ms |
35528 KB |
n=100 |
14 |
Correct |
6 ms |
35420 KB |
n=100 |
15 |
Correct |
6 ms |
35420 KB |
n=100 |
16 |
Correct |
6 ms |
35420 KB |
n=100 |
17 |
Correct |
6 ms |
35420 KB |
n=100 |
18 |
Correct |
6 ms |
35420 KB |
n=100 |
19 |
Correct |
8 ms |
35928 KB |
n=100 |
20 |
Correct |
6 ms |
35420 KB |
n=100 |
21 |
Correct |
6 ms |
35420 KB |
n=100 |
22 |
Correct |
7 ms |
35420 KB |
n=100 |
23 |
Correct |
6 ms |
35420 KB |
n=100 |
24 |
Correct |
7 ms |
35420 KB |
n=100 |
25 |
Correct |
6 ms |
35416 KB |
n=100 |
26 |
Runtime error |
32 ms |
71696 KB |
Execution killed with signal 6 |
27 |
Halted |
0 ms |
0 KB |
- |