#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define F first
#define S second
using namespace std;
const int N = 2000 + 10;
const int mod = 1e9 + 7;
const int inf = 1e9;
const int LOG = 17;
vector<int>g[N];
int up[N][LOG],in[N],out[N],timer = 0,a[N],st[N * 4];
void dfs(int x,int par) {
in[x] = ++timer;
up[x][0] = par;
for(int j = 1; j < LOG; j++) up[x][j] = up[up[x][j - 1]][j - 1];
for(auto X : g[x]) {
if(X != par) {
dfs(X,x);
}
}
out[x] = timer;
}
bool inn(int u,int v) {
return in[u] <= in[v] && out[u] >= out[v];
}
int lca(int u,int v) {
if(u == inf) return v;
if(v == inf) return u;
if(inn(u,v)) return u;
if(inn(v,u)) return v;
for(int j = LOG - 1; j >= 0; j--) {
if(up[u][j] > 0 && !inn(up[u][j],v)) u = up[u][j];
}
return up[u][0];
}
void build(int node,int tl,int tr) {
if(tl == tr) {
st[node] = a[tl];
return;
}
int mid = (tl + tr) / 2;
build(node * 2,tl,mid);
build(node * 2 + 1,mid + 1,tr);
st[node] = lca(st[node * 2],st[node * 2 + 1]);
}
void modify(int node,int tl,int tr,int i,int v) {
if(tl > i || tr < i) return;
if(tl == tr) {
st[node] = v;
return;
}
int mid = (tl + tr) / 2;
modify(node * 2,tl,mid,i,v);
modify(node * 2 + 1,mid + 1,tr,i,v);
st[node] = lca(st[node * 2],st[node * 2 + 1]);
}
int get(int node,int tl,int tr,int l,int r) {
if(tl > r || tr < l) return inf;
if(tl >= l && tr <= r) return st[node];
int mid = (tl + tr) / 2;
return lca(get(node * 2,tl,mid,l,r),get(node * 2 + 1,mid + 1,tr,l,r));
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m,q;
cin >> n >> m >> q;
for(int i = 1; i <= n - 1; i++) {
int u,v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs(1,0);
for(int i = 1; i <= m; i++) cin >> a[i];
build(1,1,m);
while(q--) {
int type;
cin >> type;
if(type == 1) {
int i,v;
cin >> i >> v;
modify(1,1,m,i,v);
}else {
int l,r,v;
cin >> l >> r >> v;
int L = -1,R = -1;
for(int i = l; i <= r; i++) {
int tl = i,tr = r,ans = -1;
while(tl <= tr) {
int mid = (tl + tr) / 2;
int lc = get(1,1,m,i,mid);
if(inn(v,lc)) {
ans = mid;
tl = mid + 1;
}else {
tr = mid - 1;
}
}
if(ans == -1) continue;
if(get(1,1,m,i,ans) == v) {
L = i;
R = ans;
break;
}
}
cout << L << " " << R << "\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
n=5 |
2 |
Correct |
1 ms |
348 KB |
n=100 |
3 |
Correct |
1 ms |
348 KB |
n=100 |
4 |
Correct |
1 ms |
348 KB |
n=100 |
5 |
Correct |
1 ms |
348 KB |
n=100 |
6 |
Correct |
2 ms |
348 KB |
n=100 |
7 |
Correct |
2 ms |
348 KB |
n=100 |
8 |
Correct |
1 ms |
352 KB |
n=100 |
9 |
Correct |
1 ms |
348 KB |
n=100 |
10 |
Correct |
2 ms |
348 KB |
n=100 |
11 |
Correct |
1 ms |
348 KB |
n=100 |
12 |
Correct |
1 ms |
348 KB |
n=100 |
13 |
Correct |
1 ms |
512 KB |
n=100 |
14 |
Correct |
1 ms |
348 KB |
n=100 |
15 |
Correct |
0 ms |
348 KB |
n=100 |
16 |
Correct |
1 ms |
344 KB |
n=100 |
17 |
Correct |
1 ms |
348 KB |
n=100 |
18 |
Correct |
1 ms |
348 KB |
n=100 |
19 |
Correct |
1 ms |
348 KB |
n=100 |
20 |
Correct |
1 ms |
348 KB |
n=100 |
21 |
Correct |
1 ms |
344 KB |
n=100 |
22 |
Correct |
0 ms |
348 KB |
n=100 |
23 |
Correct |
2 ms |
348 KB |
n=100 |
24 |
Correct |
2 ms |
348 KB |
n=100 |
25 |
Correct |
1 ms |
348 KB |
n=100 |
26 |
Correct |
1 ms |
348 KB |
n=12 |
27 |
Correct |
2 ms |
348 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
n=5 |
2 |
Correct |
1 ms |
348 KB |
n=100 |
3 |
Correct |
1 ms |
348 KB |
n=100 |
4 |
Correct |
1 ms |
348 KB |
n=100 |
5 |
Correct |
1 ms |
348 KB |
n=100 |
6 |
Correct |
2 ms |
348 KB |
n=100 |
7 |
Correct |
2 ms |
348 KB |
n=100 |
8 |
Correct |
1 ms |
352 KB |
n=100 |
9 |
Correct |
1 ms |
348 KB |
n=100 |
10 |
Correct |
2 ms |
348 KB |
n=100 |
11 |
Correct |
1 ms |
348 KB |
n=100 |
12 |
Correct |
1 ms |
348 KB |
n=100 |
13 |
Correct |
1 ms |
512 KB |
n=100 |
14 |
Correct |
1 ms |
348 KB |
n=100 |
15 |
Correct |
0 ms |
348 KB |
n=100 |
16 |
Correct |
1 ms |
344 KB |
n=100 |
17 |
Correct |
1 ms |
348 KB |
n=100 |
18 |
Correct |
1 ms |
348 KB |
n=100 |
19 |
Correct |
1 ms |
348 KB |
n=100 |
20 |
Correct |
1 ms |
348 KB |
n=100 |
21 |
Correct |
1 ms |
344 KB |
n=100 |
22 |
Correct |
0 ms |
348 KB |
n=100 |
23 |
Correct |
2 ms |
348 KB |
n=100 |
24 |
Correct |
2 ms |
348 KB |
n=100 |
25 |
Correct |
1 ms |
348 KB |
n=100 |
26 |
Correct |
1 ms |
348 KB |
n=12 |
27 |
Correct |
2 ms |
348 KB |
n=100 |
28 |
Correct |
1 ms |
600 KB |
n=500 |
29 |
Correct |
19 ms |
600 KB |
n=500 |
30 |
Correct |
16 ms |
656 KB |
n=500 |
31 |
Correct |
22 ms |
604 KB |
n=500 |
32 |
Correct |
1 ms |
600 KB |
n=500 |
33 |
Correct |
14 ms |
604 KB |
n=500 |
34 |
Correct |
2 ms |
604 KB |
n=500 |
35 |
Correct |
16 ms |
604 KB |
n=500 |
36 |
Correct |
110 ms |
600 KB |
n=500 |
37 |
Correct |
111 ms |
604 KB |
n=500 |
38 |
Correct |
109 ms |
600 KB |
n=500 |
39 |
Correct |
38 ms |
604 KB |
n=500 |
40 |
Correct |
35 ms |
604 KB |
n=500 |
41 |
Correct |
35 ms |
600 KB |
n=500 |
42 |
Correct |
62 ms |
628 KB |
n=500 |
43 |
Correct |
55 ms |
604 KB |
n=500 |
44 |
Correct |
53 ms |
604 KB |
n=500 |
45 |
Correct |
1 ms |
600 KB |
n=500 |
46 |
Correct |
18 ms |
604 KB |
n=500 |
47 |
Correct |
15 ms |
600 KB |
n=500 |
48 |
Correct |
1 ms |
600 KB |
n=500 |
49 |
Correct |
14 ms |
604 KB |
n=500 |
50 |
Correct |
22 ms |
604 KB |
n=500 |
51 |
Correct |
25 ms |
604 KB |
n=500 |
52 |
Correct |
32 ms |
604 KB |
n=500 |
53 |
Correct |
25 ms |
600 KB |
n=500 |
54 |
Correct |
24 ms |
604 KB |
n=500 |
55 |
Correct |
3 ms |
604 KB |
n=278 |
56 |
Correct |
108 ms |
640 KB |
n=500 |
57 |
Correct |
115 ms |
604 KB |
n=500 |
58 |
Correct |
61 ms |
604 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
n=5 |
2 |
Correct |
1 ms |
348 KB |
n=100 |
3 |
Correct |
1 ms |
348 KB |
n=100 |
4 |
Correct |
1 ms |
348 KB |
n=100 |
5 |
Correct |
1 ms |
348 KB |
n=100 |
6 |
Correct |
2 ms |
348 KB |
n=100 |
7 |
Correct |
2 ms |
348 KB |
n=100 |
8 |
Correct |
1 ms |
352 KB |
n=100 |
9 |
Correct |
1 ms |
348 KB |
n=100 |
10 |
Correct |
2 ms |
348 KB |
n=100 |
11 |
Correct |
1 ms |
348 KB |
n=100 |
12 |
Correct |
1 ms |
348 KB |
n=100 |
13 |
Correct |
1 ms |
512 KB |
n=100 |
14 |
Correct |
1 ms |
348 KB |
n=100 |
15 |
Correct |
0 ms |
348 KB |
n=100 |
16 |
Correct |
1 ms |
344 KB |
n=100 |
17 |
Correct |
1 ms |
348 KB |
n=100 |
18 |
Correct |
1 ms |
348 KB |
n=100 |
19 |
Correct |
1 ms |
348 KB |
n=100 |
20 |
Correct |
1 ms |
348 KB |
n=100 |
21 |
Correct |
1 ms |
344 KB |
n=100 |
22 |
Correct |
0 ms |
348 KB |
n=100 |
23 |
Correct |
2 ms |
348 KB |
n=100 |
24 |
Correct |
2 ms |
348 KB |
n=100 |
25 |
Correct |
1 ms |
348 KB |
n=100 |
26 |
Correct |
1 ms |
348 KB |
n=12 |
27 |
Correct |
2 ms |
348 KB |
n=100 |
28 |
Correct |
1 ms |
600 KB |
n=500 |
29 |
Correct |
19 ms |
600 KB |
n=500 |
30 |
Correct |
16 ms |
656 KB |
n=500 |
31 |
Correct |
22 ms |
604 KB |
n=500 |
32 |
Correct |
1 ms |
600 KB |
n=500 |
33 |
Correct |
14 ms |
604 KB |
n=500 |
34 |
Correct |
2 ms |
604 KB |
n=500 |
35 |
Correct |
16 ms |
604 KB |
n=500 |
36 |
Correct |
110 ms |
600 KB |
n=500 |
37 |
Correct |
111 ms |
604 KB |
n=500 |
38 |
Correct |
109 ms |
600 KB |
n=500 |
39 |
Correct |
38 ms |
604 KB |
n=500 |
40 |
Correct |
35 ms |
604 KB |
n=500 |
41 |
Correct |
35 ms |
600 KB |
n=500 |
42 |
Correct |
62 ms |
628 KB |
n=500 |
43 |
Correct |
55 ms |
604 KB |
n=500 |
44 |
Correct |
53 ms |
604 KB |
n=500 |
45 |
Correct |
1 ms |
600 KB |
n=500 |
46 |
Correct |
18 ms |
604 KB |
n=500 |
47 |
Correct |
15 ms |
600 KB |
n=500 |
48 |
Correct |
1 ms |
600 KB |
n=500 |
49 |
Correct |
14 ms |
604 KB |
n=500 |
50 |
Correct |
22 ms |
604 KB |
n=500 |
51 |
Correct |
25 ms |
604 KB |
n=500 |
52 |
Correct |
32 ms |
604 KB |
n=500 |
53 |
Correct |
25 ms |
600 KB |
n=500 |
54 |
Correct |
24 ms |
604 KB |
n=500 |
55 |
Correct |
3 ms |
604 KB |
n=278 |
56 |
Correct |
108 ms |
640 KB |
n=500 |
57 |
Correct |
115 ms |
604 KB |
n=500 |
58 |
Correct |
61 ms |
604 KB |
n=500 |
59 |
Correct |
6 ms |
860 KB |
n=2000 |
60 |
Correct |
414 ms |
860 KB |
n=2000 |
61 |
Correct |
394 ms |
1012 KB |
n=2000 |
62 |
Correct |
259 ms |
856 KB |
n=2000 |
63 |
Correct |
9 ms |
856 KB |
n=2000 |
64 |
Correct |
351 ms |
860 KB |
n=2000 |
65 |
Correct |
4 ms |
860 KB |
n=2000 |
66 |
Correct |
444 ms |
860 KB |
n=2000 |
67 |
Correct |
20 ms |
856 KB |
n=2000 |
68 |
Correct |
363 ms |
860 KB |
n=2000 |
69 |
Correct |
2628 ms |
860 KB |
n=2000 |
70 |
Correct |
2649 ms |
964 KB |
n=2000 |
71 |
Correct |
2638 ms |
972 KB |
n=2000 |
72 |
Correct |
922 ms |
968 KB |
n=2000 |
73 |
Correct |
941 ms |
964 KB |
n=2000 |
74 |
Correct |
4 ms |
860 KB |
n=1844 |
75 |
Correct |
957 ms |
972 KB |
n=2000 |
76 |
Correct |
1279 ms |
856 KB |
n=2000 |
77 |
Correct |
1374 ms |
956 KB |
n=2000 |
78 |
Correct |
1454 ms |
856 KB |
n=2000 |
79 |
Correct |
3 ms |
856 KB |
n=2000 |
80 |
Correct |
360 ms |
856 KB |
n=2000 |
81 |
Correct |
312 ms |
856 KB |
n=2000 |
82 |
Correct |
4 ms |
856 KB |
n=2000 |
83 |
Correct |
359 ms |
1020 KB |
n=2000 |
84 |
Correct |
570 ms |
856 KB |
n=2000 |
85 |
Correct |
584 ms |
856 KB |
n=2000 |
86 |
Correct |
653 ms |
984 KB |
n=2000 |
87 |
Correct |
605 ms |
964 KB |
n=2000 |
88 |
Correct |
2680 ms |
860 KB |
n=2000 |
89 |
Correct |
2816 ms |
1040 KB |
n=2000 |
90 |
Correct |
428 ms |
860 KB |
n=2000 |
91 |
Correct |
1664 ms |
856 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
n=5 |
2 |
Correct |
1 ms |
348 KB |
n=100 |
3 |
Correct |
1 ms |
348 KB |
n=100 |
4 |
Correct |
1 ms |
348 KB |
n=100 |
5 |
Correct |
1 ms |
348 KB |
n=100 |
6 |
Correct |
2 ms |
348 KB |
n=100 |
7 |
Correct |
2 ms |
348 KB |
n=100 |
8 |
Correct |
1 ms |
352 KB |
n=100 |
9 |
Correct |
1 ms |
348 KB |
n=100 |
10 |
Correct |
2 ms |
348 KB |
n=100 |
11 |
Correct |
1 ms |
348 KB |
n=100 |
12 |
Correct |
1 ms |
348 KB |
n=100 |
13 |
Correct |
1 ms |
512 KB |
n=100 |
14 |
Correct |
1 ms |
348 KB |
n=100 |
15 |
Correct |
0 ms |
348 KB |
n=100 |
16 |
Correct |
1 ms |
344 KB |
n=100 |
17 |
Correct |
1 ms |
348 KB |
n=100 |
18 |
Correct |
1 ms |
348 KB |
n=100 |
19 |
Correct |
1 ms |
348 KB |
n=100 |
20 |
Correct |
1 ms |
348 KB |
n=100 |
21 |
Correct |
1 ms |
344 KB |
n=100 |
22 |
Correct |
0 ms |
348 KB |
n=100 |
23 |
Correct |
2 ms |
348 KB |
n=100 |
24 |
Correct |
2 ms |
348 KB |
n=100 |
25 |
Correct |
1 ms |
348 KB |
n=100 |
26 |
Correct |
1 ms |
348 KB |
n=12 |
27 |
Correct |
2 ms |
348 KB |
n=100 |
28 |
Correct |
1 ms |
600 KB |
n=500 |
29 |
Correct |
19 ms |
600 KB |
n=500 |
30 |
Correct |
16 ms |
656 KB |
n=500 |
31 |
Correct |
22 ms |
604 KB |
n=500 |
32 |
Correct |
1 ms |
600 KB |
n=500 |
33 |
Correct |
14 ms |
604 KB |
n=500 |
34 |
Correct |
2 ms |
604 KB |
n=500 |
35 |
Correct |
16 ms |
604 KB |
n=500 |
36 |
Correct |
110 ms |
600 KB |
n=500 |
37 |
Correct |
111 ms |
604 KB |
n=500 |
38 |
Correct |
109 ms |
600 KB |
n=500 |
39 |
Correct |
38 ms |
604 KB |
n=500 |
40 |
Correct |
35 ms |
604 KB |
n=500 |
41 |
Correct |
35 ms |
600 KB |
n=500 |
42 |
Correct |
62 ms |
628 KB |
n=500 |
43 |
Correct |
55 ms |
604 KB |
n=500 |
44 |
Correct |
53 ms |
604 KB |
n=500 |
45 |
Correct |
1 ms |
600 KB |
n=500 |
46 |
Correct |
18 ms |
604 KB |
n=500 |
47 |
Correct |
15 ms |
600 KB |
n=500 |
48 |
Correct |
1 ms |
600 KB |
n=500 |
49 |
Correct |
14 ms |
604 KB |
n=500 |
50 |
Correct |
22 ms |
604 KB |
n=500 |
51 |
Correct |
25 ms |
604 KB |
n=500 |
52 |
Correct |
32 ms |
604 KB |
n=500 |
53 |
Correct |
25 ms |
600 KB |
n=500 |
54 |
Correct |
24 ms |
604 KB |
n=500 |
55 |
Correct |
3 ms |
604 KB |
n=278 |
56 |
Correct |
108 ms |
640 KB |
n=500 |
57 |
Correct |
115 ms |
604 KB |
n=500 |
58 |
Correct |
61 ms |
604 KB |
n=500 |
59 |
Correct |
6 ms |
860 KB |
n=2000 |
60 |
Correct |
414 ms |
860 KB |
n=2000 |
61 |
Correct |
394 ms |
1012 KB |
n=2000 |
62 |
Correct |
259 ms |
856 KB |
n=2000 |
63 |
Correct |
9 ms |
856 KB |
n=2000 |
64 |
Correct |
351 ms |
860 KB |
n=2000 |
65 |
Correct |
4 ms |
860 KB |
n=2000 |
66 |
Correct |
444 ms |
860 KB |
n=2000 |
67 |
Correct |
20 ms |
856 KB |
n=2000 |
68 |
Correct |
363 ms |
860 KB |
n=2000 |
69 |
Correct |
2628 ms |
860 KB |
n=2000 |
70 |
Correct |
2649 ms |
964 KB |
n=2000 |
71 |
Correct |
2638 ms |
972 KB |
n=2000 |
72 |
Correct |
922 ms |
968 KB |
n=2000 |
73 |
Correct |
941 ms |
964 KB |
n=2000 |
74 |
Correct |
4 ms |
860 KB |
n=1844 |
75 |
Correct |
957 ms |
972 KB |
n=2000 |
76 |
Correct |
1279 ms |
856 KB |
n=2000 |
77 |
Correct |
1374 ms |
956 KB |
n=2000 |
78 |
Correct |
1454 ms |
856 KB |
n=2000 |
79 |
Correct |
3 ms |
856 KB |
n=2000 |
80 |
Correct |
360 ms |
856 KB |
n=2000 |
81 |
Correct |
312 ms |
856 KB |
n=2000 |
82 |
Correct |
4 ms |
856 KB |
n=2000 |
83 |
Correct |
359 ms |
1020 KB |
n=2000 |
84 |
Correct |
570 ms |
856 KB |
n=2000 |
85 |
Correct |
584 ms |
856 KB |
n=2000 |
86 |
Correct |
653 ms |
984 KB |
n=2000 |
87 |
Correct |
605 ms |
964 KB |
n=2000 |
88 |
Correct |
2680 ms |
860 KB |
n=2000 |
89 |
Correct |
2816 ms |
1040 KB |
n=2000 |
90 |
Correct |
428 ms |
860 KB |
n=2000 |
91 |
Correct |
1664 ms |
856 KB |
n=2000 |
92 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
93 |
Halted |
0 ms |
0 KB |
- |