#include "bits/stdc++.h"
using namespace std;
#define sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define rall(v) rbegin(v), rend(v)
const int MAX = 2e5 + 5;
int n, m, q, timerdfs, a[MAX], adjLCA[MAX], depth[MAX], pos[MAX], st[20][MAX];
vector<int> adj[MAX];
set<int> one[MAX], two[MAX];
int query(set<int>& S, int l, int r){
auto it = S.lower_bound(l);
if(it == S.end()) return -1;
if(*it <= r) return *it;
return -1;
}
void dfs(int u){
st[0][pos[u] = timerdfs++] = u;
for(int v : adj[u]){
adj[v].erase(find(all(adj[v]), u));
depth[v] = depth[u] + 1;
dfs(v);
st[0][timerdfs++] = u;
}
}
int f(int u, int v){
return depth[u] < depth[v] ? u : v;
}
void prepareRMQ(){
for(int i = 1; (1 << i) <= timerdfs; ++i){
for(int j = 0; j + (1 << i) <= timerdfs; ++j){
st[i][j] = f(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
}
}
}
int getLCA(int u, int v){
u = pos[u]; v = pos[v];
if(u > v) swap(u, v);
int k = 31 - __builtin_clz(v - u + 1);
return f(st[k][u], st[k][v - (1 << k) + 1]);
}
void deleteAdj(int i){
two[adjLCA[i]].erase(i);
}
void refine(int i){
adjLCA[i] = getLCA(a[i], a[i + 1]);
two[adjLCA[i]].insert(i);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen("task.inp", "r")){
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
}
cin >> n >> m >> q;
for(int i = 1; i < n; ++i){
int u, v;
cin >> u >> v;
adj[u].push_back(v); adj[v].push_back(u);
}
for(int i = 1; i <= m; ++i){
cin >> a[i];
}
dfs(1);
prepareRMQ();
for(int i = 1; i <= m; ++i){
one[a[i]].insert(i);
if(i < m) {
adjLCA[i] = getLCA(a[i], a[i + 1]);
two[adjLCA[i]].insert(i);
}
}
while(q--){
int type;
cin >> type;
if(type == 1){
int pos, val;
cin >> pos >> val;
one[a[pos]].erase(pos);
a[pos] = val;
one[a[pos]].insert(pos);
if(pos < m) deleteAdj(pos), refine(pos);
if(pos > 1) deleteAdj(pos - 1), refine(pos - 1);
} else{
int l, r, x;
cin >> l >> r >> x;
int q1 = query(one[x], l, r);
if(q1 != -1){
cout << q1 << ' ' << q1 << '\n';
// cout << "YES\n";
continue;
}
int q2 = query(two[x], l, r - 1);
if(q2 != -1){
cout << q2 << ' ' << q2 + 1 << '\n';
// cout << "YES\n";
continue;
} else{
// cout << "NO\n";
cout << -1 << ' ' << -1 << '\n';
}
}
}
return 0;
}
Compilation message
treearray.cpp: In function 'int main()':
treearray.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | freopen("task.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | freopen("task.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
n=5 |
2 |
Correct |
11 ms |
23900 KB |
n=100 |
3 |
Correct |
10 ms |
23900 KB |
n=100 |
4 |
Correct |
10 ms |
23900 KB |
n=100 |
5 |
Correct |
10 ms |
23900 KB |
n=100 |
6 |
Correct |
12 ms |
23900 KB |
n=100 |
7 |
Correct |
10 ms |
23900 KB |
n=100 |
8 |
Correct |
10 ms |
23896 KB |
n=100 |
9 |
Correct |
10 ms |
23896 KB |
n=100 |
10 |
Correct |
12 ms |
23900 KB |
n=100 |
11 |
Correct |
12 ms |
23896 KB |
n=100 |
12 |
Correct |
10 ms |
23900 KB |
n=100 |
13 |
Correct |
12 ms |
23896 KB |
n=100 |
14 |
Correct |
12 ms |
24156 KB |
n=100 |
15 |
Correct |
11 ms |
23900 KB |
n=100 |
16 |
Correct |
11 ms |
23896 KB |
n=100 |
17 |
Correct |
11 ms |
23900 KB |
n=100 |
18 |
Correct |
11 ms |
23896 KB |
n=100 |
19 |
Correct |
10 ms |
23900 KB |
n=100 |
20 |
Correct |
11 ms |
23900 KB |
n=100 |
21 |
Correct |
12 ms |
23876 KB |
n=100 |
22 |
Correct |
15 ms |
23900 KB |
n=100 |
23 |
Correct |
11 ms |
23876 KB |
n=100 |
24 |
Correct |
11 ms |
23900 KB |
n=100 |
25 |
Correct |
11 ms |
23896 KB |
n=100 |
26 |
Correct |
10 ms |
23996 KB |
n=12 |
27 |
Correct |
11 ms |
23896 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
n=5 |
2 |
Correct |
11 ms |
23900 KB |
n=100 |
3 |
Correct |
10 ms |
23900 KB |
n=100 |
4 |
Correct |
10 ms |
23900 KB |
n=100 |
5 |
Correct |
10 ms |
23900 KB |
n=100 |
6 |
Correct |
12 ms |
23900 KB |
n=100 |
7 |
Correct |
10 ms |
23900 KB |
n=100 |
8 |
Correct |
10 ms |
23896 KB |
n=100 |
9 |
Correct |
10 ms |
23896 KB |
n=100 |
10 |
Correct |
12 ms |
23900 KB |
n=100 |
11 |
Correct |
12 ms |
23896 KB |
n=100 |
12 |
Correct |
10 ms |
23900 KB |
n=100 |
13 |
Correct |
12 ms |
23896 KB |
n=100 |
14 |
Correct |
12 ms |
24156 KB |
n=100 |
15 |
Correct |
11 ms |
23900 KB |
n=100 |
16 |
Correct |
11 ms |
23896 KB |
n=100 |
17 |
Correct |
11 ms |
23900 KB |
n=100 |
18 |
Correct |
11 ms |
23896 KB |
n=100 |
19 |
Correct |
10 ms |
23900 KB |
n=100 |
20 |
Correct |
11 ms |
23900 KB |
n=100 |
21 |
Correct |
12 ms |
23876 KB |
n=100 |
22 |
Correct |
15 ms |
23900 KB |
n=100 |
23 |
Correct |
11 ms |
23876 KB |
n=100 |
24 |
Correct |
11 ms |
23900 KB |
n=100 |
25 |
Correct |
11 ms |
23896 KB |
n=100 |
26 |
Correct |
10 ms |
23996 KB |
n=12 |
27 |
Correct |
11 ms |
23896 KB |
n=100 |
28 |
Correct |
10 ms |
23896 KB |
n=500 |
29 |
Correct |
12 ms |
24156 KB |
n=500 |
30 |
Correct |
16 ms |
24152 KB |
n=500 |
31 |
Correct |
11 ms |
24152 KB |
n=500 |
32 |
Correct |
11 ms |
23900 KB |
n=500 |
33 |
Correct |
11 ms |
24156 KB |
n=500 |
34 |
Correct |
11 ms |
23900 KB |
n=500 |
35 |
Correct |
11 ms |
24152 KB |
n=500 |
36 |
Correct |
13 ms |
23900 KB |
n=500 |
37 |
Correct |
11 ms |
24116 KB |
n=500 |
38 |
Correct |
11 ms |
23900 KB |
n=500 |
39 |
Correct |
11 ms |
23900 KB |
n=500 |
40 |
Correct |
11 ms |
23896 KB |
n=500 |
41 |
Correct |
12 ms |
23900 KB |
n=500 |
42 |
Correct |
11 ms |
23900 KB |
n=500 |
43 |
Correct |
12 ms |
23896 KB |
n=500 |
44 |
Correct |
12 ms |
23956 KB |
n=500 |
45 |
Correct |
10 ms |
23900 KB |
n=500 |
46 |
Correct |
10 ms |
24156 KB |
n=500 |
47 |
Correct |
12 ms |
24152 KB |
n=500 |
48 |
Correct |
15 ms |
23900 KB |
n=500 |
49 |
Correct |
12 ms |
24156 KB |
n=500 |
50 |
Correct |
11 ms |
23900 KB |
n=500 |
51 |
Correct |
15 ms |
24156 KB |
n=500 |
52 |
Correct |
15 ms |
24156 KB |
n=500 |
53 |
Correct |
13 ms |
24156 KB |
n=500 |
54 |
Correct |
12 ms |
24152 KB |
n=500 |
55 |
Correct |
11 ms |
23896 KB |
n=278 |
56 |
Correct |
11 ms |
24152 KB |
n=500 |
57 |
Correct |
11 ms |
24156 KB |
n=500 |
58 |
Correct |
14 ms |
23900 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
n=5 |
2 |
Correct |
11 ms |
23900 KB |
n=100 |
3 |
Correct |
10 ms |
23900 KB |
n=100 |
4 |
Correct |
10 ms |
23900 KB |
n=100 |
5 |
Correct |
10 ms |
23900 KB |
n=100 |
6 |
Correct |
12 ms |
23900 KB |
n=100 |
7 |
Correct |
10 ms |
23900 KB |
n=100 |
8 |
Correct |
10 ms |
23896 KB |
n=100 |
9 |
Correct |
10 ms |
23896 KB |
n=100 |
10 |
Correct |
12 ms |
23900 KB |
n=100 |
11 |
Correct |
12 ms |
23896 KB |
n=100 |
12 |
Correct |
10 ms |
23900 KB |
n=100 |
13 |
Correct |
12 ms |
23896 KB |
n=100 |
14 |
Correct |
12 ms |
24156 KB |
n=100 |
15 |
Correct |
11 ms |
23900 KB |
n=100 |
16 |
Correct |
11 ms |
23896 KB |
n=100 |
17 |
Correct |
11 ms |
23900 KB |
n=100 |
18 |
Correct |
11 ms |
23896 KB |
n=100 |
19 |
Correct |
10 ms |
23900 KB |
n=100 |
20 |
Correct |
11 ms |
23900 KB |
n=100 |
21 |
Correct |
12 ms |
23876 KB |
n=100 |
22 |
Correct |
15 ms |
23900 KB |
n=100 |
23 |
Correct |
11 ms |
23876 KB |
n=100 |
24 |
Correct |
11 ms |
23900 KB |
n=100 |
25 |
Correct |
11 ms |
23896 KB |
n=100 |
26 |
Correct |
10 ms |
23996 KB |
n=12 |
27 |
Correct |
11 ms |
23896 KB |
n=100 |
28 |
Correct |
10 ms |
23896 KB |
n=500 |
29 |
Correct |
12 ms |
24156 KB |
n=500 |
30 |
Correct |
16 ms |
24152 KB |
n=500 |
31 |
Correct |
11 ms |
24152 KB |
n=500 |
32 |
Correct |
11 ms |
23900 KB |
n=500 |
33 |
Correct |
11 ms |
24156 KB |
n=500 |
34 |
Correct |
11 ms |
23900 KB |
n=500 |
35 |
Correct |
11 ms |
24152 KB |
n=500 |
36 |
Correct |
13 ms |
23900 KB |
n=500 |
37 |
Correct |
11 ms |
24116 KB |
n=500 |
38 |
Correct |
11 ms |
23900 KB |
n=500 |
39 |
Correct |
11 ms |
23900 KB |
n=500 |
40 |
Correct |
11 ms |
23896 KB |
n=500 |
41 |
Correct |
12 ms |
23900 KB |
n=500 |
42 |
Correct |
11 ms |
23900 KB |
n=500 |
43 |
Correct |
12 ms |
23896 KB |
n=500 |
44 |
Correct |
12 ms |
23956 KB |
n=500 |
45 |
Correct |
10 ms |
23900 KB |
n=500 |
46 |
Correct |
10 ms |
24156 KB |
n=500 |
47 |
Correct |
12 ms |
24152 KB |
n=500 |
48 |
Correct |
15 ms |
23900 KB |
n=500 |
49 |
Correct |
12 ms |
24156 KB |
n=500 |
50 |
Correct |
11 ms |
23900 KB |
n=500 |
51 |
Correct |
15 ms |
24156 KB |
n=500 |
52 |
Correct |
15 ms |
24156 KB |
n=500 |
53 |
Correct |
13 ms |
24156 KB |
n=500 |
54 |
Correct |
12 ms |
24152 KB |
n=500 |
55 |
Correct |
11 ms |
23896 KB |
n=278 |
56 |
Correct |
11 ms |
24152 KB |
n=500 |
57 |
Correct |
11 ms |
24156 KB |
n=500 |
58 |
Correct |
14 ms |
23900 KB |
n=500 |
59 |
Correct |
14 ms |
24408 KB |
n=2000 |
60 |
Correct |
13 ms |
24664 KB |
n=2000 |
61 |
Correct |
16 ms |
24412 KB |
n=2000 |
62 |
Correct |
13 ms |
24388 KB |
n=2000 |
63 |
Correct |
14 ms |
24412 KB |
n=2000 |
64 |
Correct |
13 ms |
24412 KB |
n=2000 |
65 |
Correct |
13 ms |
24412 KB |
n=2000 |
66 |
Correct |
12 ms |
24408 KB |
n=2000 |
67 |
Correct |
12 ms |
24316 KB |
n=2000 |
68 |
Correct |
13 ms |
24412 KB |
n=2000 |
69 |
Correct |
12 ms |
24412 KB |
n=2000 |
70 |
Correct |
12 ms |
24412 KB |
n=2000 |
71 |
Correct |
17 ms |
24492 KB |
n=2000 |
72 |
Correct |
11 ms |
24412 KB |
n=2000 |
73 |
Correct |
11 ms |
24408 KB |
n=2000 |
74 |
Correct |
12 ms |
24408 KB |
n=1844 |
75 |
Correct |
12 ms |
24412 KB |
n=2000 |
76 |
Correct |
16 ms |
24412 KB |
n=2000 |
77 |
Correct |
11 ms |
24412 KB |
n=2000 |
78 |
Correct |
13 ms |
24412 KB |
n=2000 |
79 |
Correct |
12 ms |
24408 KB |
n=2000 |
80 |
Correct |
14 ms |
24668 KB |
n=2000 |
81 |
Correct |
12 ms |
24412 KB |
n=2000 |
82 |
Correct |
12 ms |
24412 KB |
n=2000 |
83 |
Correct |
12 ms |
24408 KB |
n=2000 |
84 |
Correct |
12 ms |
24408 KB |
n=2000 |
85 |
Correct |
13 ms |
24408 KB |
n=2000 |
86 |
Correct |
19 ms |
24404 KB |
n=2000 |
87 |
Correct |
12 ms |
24412 KB |
n=2000 |
88 |
Correct |
14 ms |
24668 KB |
n=2000 |
89 |
Correct |
12 ms |
24920 KB |
n=2000 |
90 |
Correct |
13 ms |
24508 KB |
n=2000 |
91 |
Correct |
12 ms |
24408 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
n=5 |
2 |
Correct |
11 ms |
23900 KB |
n=100 |
3 |
Correct |
10 ms |
23900 KB |
n=100 |
4 |
Correct |
10 ms |
23900 KB |
n=100 |
5 |
Correct |
10 ms |
23900 KB |
n=100 |
6 |
Correct |
12 ms |
23900 KB |
n=100 |
7 |
Correct |
10 ms |
23900 KB |
n=100 |
8 |
Correct |
10 ms |
23896 KB |
n=100 |
9 |
Correct |
10 ms |
23896 KB |
n=100 |
10 |
Correct |
12 ms |
23900 KB |
n=100 |
11 |
Correct |
12 ms |
23896 KB |
n=100 |
12 |
Correct |
10 ms |
23900 KB |
n=100 |
13 |
Correct |
12 ms |
23896 KB |
n=100 |
14 |
Correct |
12 ms |
24156 KB |
n=100 |
15 |
Correct |
11 ms |
23900 KB |
n=100 |
16 |
Correct |
11 ms |
23896 KB |
n=100 |
17 |
Correct |
11 ms |
23900 KB |
n=100 |
18 |
Correct |
11 ms |
23896 KB |
n=100 |
19 |
Correct |
10 ms |
23900 KB |
n=100 |
20 |
Correct |
11 ms |
23900 KB |
n=100 |
21 |
Correct |
12 ms |
23876 KB |
n=100 |
22 |
Correct |
15 ms |
23900 KB |
n=100 |
23 |
Correct |
11 ms |
23876 KB |
n=100 |
24 |
Correct |
11 ms |
23900 KB |
n=100 |
25 |
Correct |
11 ms |
23896 KB |
n=100 |
26 |
Correct |
10 ms |
23996 KB |
n=12 |
27 |
Correct |
11 ms |
23896 KB |
n=100 |
28 |
Correct |
10 ms |
23896 KB |
n=500 |
29 |
Correct |
12 ms |
24156 KB |
n=500 |
30 |
Correct |
16 ms |
24152 KB |
n=500 |
31 |
Correct |
11 ms |
24152 KB |
n=500 |
32 |
Correct |
11 ms |
23900 KB |
n=500 |
33 |
Correct |
11 ms |
24156 KB |
n=500 |
34 |
Correct |
11 ms |
23900 KB |
n=500 |
35 |
Correct |
11 ms |
24152 KB |
n=500 |
36 |
Correct |
13 ms |
23900 KB |
n=500 |
37 |
Correct |
11 ms |
24116 KB |
n=500 |
38 |
Correct |
11 ms |
23900 KB |
n=500 |
39 |
Correct |
11 ms |
23900 KB |
n=500 |
40 |
Correct |
11 ms |
23896 KB |
n=500 |
41 |
Correct |
12 ms |
23900 KB |
n=500 |
42 |
Correct |
11 ms |
23900 KB |
n=500 |
43 |
Correct |
12 ms |
23896 KB |
n=500 |
44 |
Correct |
12 ms |
23956 KB |
n=500 |
45 |
Correct |
10 ms |
23900 KB |
n=500 |
46 |
Correct |
10 ms |
24156 KB |
n=500 |
47 |
Correct |
12 ms |
24152 KB |
n=500 |
48 |
Correct |
15 ms |
23900 KB |
n=500 |
49 |
Correct |
12 ms |
24156 KB |
n=500 |
50 |
Correct |
11 ms |
23900 KB |
n=500 |
51 |
Correct |
15 ms |
24156 KB |
n=500 |
52 |
Correct |
15 ms |
24156 KB |
n=500 |
53 |
Correct |
13 ms |
24156 KB |
n=500 |
54 |
Correct |
12 ms |
24152 KB |
n=500 |
55 |
Correct |
11 ms |
23896 KB |
n=278 |
56 |
Correct |
11 ms |
24152 KB |
n=500 |
57 |
Correct |
11 ms |
24156 KB |
n=500 |
58 |
Correct |
14 ms |
23900 KB |
n=500 |
59 |
Correct |
14 ms |
24408 KB |
n=2000 |
60 |
Correct |
13 ms |
24664 KB |
n=2000 |
61 |
Correct |
16 ms |
24412 KB |
n=2000 |
62 |
Correct |
13 ms |
24388 KB |
n=2000 |
63 |
Correct |
14 ms |
24412 KB |
n=2000 |
64 |
Correct |
13 ms |
24412 KB |
n=2000 |
65 |
Correct |
13 ms |
24412 KB |
n=2000 |
66 |
Correct |
12 ms |
24408 KB |
n=2000 |
67 |
Correct |
12 ms |
24316 KB |
n=2000 |
68 |
Correct |
13 ms |
24412 KB |
n=2000 |
69 |
Correct |
12 ms |
24412 KB |
n=2000 |
70 |
Correct |
12 ms |
24412 KB |
n=2000 |
71 |
Correct |
17 ms |
24492 KB |
n=2000 |
72 |
Correct |
11 ms |
24412 KB |
n=2000 |
73 |
Correct |
11 ms |
24408 KB |
n=2000 |
74 |
Correct |
12 ms |
24408 KB |
n=1844 |
75 |
Correct |
12 ms |
24412 KB |
n=2000 |
76 |
Correct |
16 ms |
24412 KB |
n=2000 |
77 |
Correct |
11 ms |
24412 KB |
n=2000 |
78 |
Correct |
13 ms |
24412 KB |
n=2000 |
79 |
Correct |
12 ms |
24408 KB |
n=2000 |
80 |
Correct |
14 ms |
24668 KB |
n=2000 |
81 |
Correct |
12 ms |
24412 KB |
n=2000 |
82 |
Correct |
12 ms |
24412 KB |
n=2000 |
83 |
Correct |
12 ms |
24408 KB |
n=2000 |
84 |
Correct |
12 ms |
24408 KB |
n=2000 |
85 |
Correct |
13 ms |
24408 KB |
n=2000 |
86 |
Correct |
19 ms |
24404 KB |
n=2000 |
87 |
Correct |
12 ms |
24412 KB |
n=2000 |
88 |
Correct |
14 ms |
24668 KB |
n=2000 |
89 |
Correct |
12 ms |
24920 KB |
n=2000 |
90 |
Correct |
13 ms |
24508 KB |
n=2000 |
91 |
Correct |
12 ms |
24408 KB |
n=2000 |
92 |
Incorrect |
381 ms |
75344 KB |
Wrong range. |
93 |
Halted |
0 ms |
0 KB |
- |