#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pii pair<ll, ll>
#define read_input freopen("in.txt","r", stdin)
#define print_output freopen("out.txt","w", stdout)
typedef long long ll;
typedef long double ld;
using namespace std;
const int maxn = 2e5+10, inf = 1e9+7;
set<int> S[maxn], T[maxn];
vector<int> adj[maxn];
int parent[20][maxn], level[maxn], a[maxn];
void dfs(int u, int par, int lev = 0) {
level[u] = lev;
parent[0][u] = par;
for(auto it : adj[u])
if(it != par) dfs(it, u, lev+1);
}
int lca(int u, int v) {
if(level[u] < level[v]) swap(u, v);
int i, k;
if(level[u] != level[v]) {
for(i = 19; i >= 0; i--) {
if(parent[i][u] != -1 && level[parent[i][u]] >= level[v]) u = parent[i][u];
}
}
if(u == v) return u;
for(i = 19; i >= 0; i--) {
if(parent[i][u] != -1 && parent[i][u] != parent[i][v]) {
u = parent[i][u];
v = parent[i][v];
}
} return parent[0][u];
}
int main()
{
int n, m, q;
cin >> n >> m >> q;
for(int i = 1, x, y; i < n; i++) {
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
}
memset(parent, -1, sizeof parent);
dfs(1, -1);
for(int i = 1; 1 << i < n; i++)
for(int j = 1; j <= n; j++)
if(parent[i-1][j] != -1) parent[i][j] = parent[i-1][parent[i-1][j]];
for(int i = 1; i <= m; i++) {
cin >> a[i];
S[a[i]].insert(i);
if(i > 1) T[lca(a[i], a[i-1])].insert(i);
}
for(int i = 1; i <= n; i++) S[i].insert(inf), T[i].insert(inf);
while(q--) {
int ch;
cin >> ch;
if(ch == 1) {
int pos, v;
cin >> pos >> v;
if(pos > 1) T[lca(a[pos], a[pos-1])].erase(pos);
if(pos < m) T[lca(a[pos], a[pos+1])].erase(pos+1);
S[a[pos]].erase(pos);
a[pos] = v;
S[a[pos]].insert(pos);
if(pos > 1) T[lca(a[pos], a[pos-1])].insert(pos);
if(pos < m) T[lca(a[pos], a[pos+1])].insert(pos+1);
}
else {
int l, r, v;
cin >> l >> r >> v;
int d = *S[v].lower_bound(l);
int e = *T[v].lower_bound(l+1);
if(d <= r) cout << d << ' ' << d << endl;
else if(e <= r) cout << e-1 << ' ' << e << endl;
else cout << -1 << ' ' << -1 << endl;
}
}
return 0;
}
Compilation message
treearray.cpp: In function 'int lca(int, int)':
treearray.cpp:26:12: warning: unused variable 'k' [-Wunused-variable]
int i, k;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
39544 KB |
n=5 |
2 |
Correct |
41 ms |
39552 KB |
n=100 |
3 |
Correct |
40 ms |
39416 KB |
n=100 |
4 |
Correct |
39 ms |
39516 KB |
n=100 |
5 |
Correct |
41 ms |
39420 KB |
n=100 |
6 |
Correct |
37 ms |
39464 KB |
n=100 |
7 |
Correct |
39 ms |
39524 KB |
n=100 |
8 |
Correct |
48 ms |
39416 KB |
n=100 |
9 |
Correct |
34 ms |
39552 KB |
n=100 |
10 |
Correct |
40 ms |
39416 KB |
n=100 |
11 |
Correct |
42 ms |
39544 KB |
n=100 |
12 |
Correct |
37 ms |
39416 KB |
n=100 |
13 |
Correct |
39 ms |
39544 KB |
n=100 |
14 |
Correct |
43 ms |
39628 KB |
n=100 |
15 |
Correct |
38 ms |
39480 KB |
n=100 |
16 |
Correct |
41 ms |
39568 KB |
n=100 |
17 |
Correct |
41 ms |
39416 KB |
n=100 |
18 |
Correct |
41 ms |
39416 KB |
n=100 |
19 |
Correct |
35 ms |
39552 KB |
n=100 |
20 |
Correct |
39 ms |
39416 KB |
n=100 |
21 |
Correct |
35 ms |
39544 KB |
n=100 |
22 |
Correct |
39 ms |
39516 KB |
n=100 |
23 |
Correct |
38 ms |
39544 KB |
n=100 |
24 |
Correct |
32 ms |
39544 KB |
n=100 |
25 |
Correct |
42 ms |
39544 KB |
n=100 |
26 |
Correct |
41 ms |
39416 KB |
n=12 |
27 |
Correct |
41 ms |
39544 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
39544 KB |
n=5 |
2 |
Correct |
41 ms |
39552 KB |
n=100 |
3 |
Correct |
40 ms |
39416 KB |
n=100 |
4 |
Correct |
39 ms |
39516 KB |
n=100 |
5 |
Correct |
41 ms |
39420 KB |
n=100 |
6 |
Correct |
37 ms |
39464 KB |
n=100 |
7 |
Correct |
39 ms |
39524 KB |
n=100 |
8 |
Correct |
48 ms |
39416 KB |
n=100 |
9 |
Correct |
34 ms |
39552 KB |
n=100 |
10 |
Correct |
40 ms |
39416 KB |
n=100 |
11 |
Correct |
42 ms |
39544 KB |
n=100 |
12 |
Correct |
37 ms |
39416 KB |
n=100 |
13 |
Correct |
39 ms |
39544 KB |
n=100 |
14 |
Correct |
43 ms |
39628 KB |
n=100 |
15 |
Correct |
38 ms |
39480 KB |
n=100 |
16 |
Correct |
41 ms |
39568 KB |
n=100 |
17 |
Correct |
41 ms |
39416 KB |
n=100 |
18 |
Correct |
41 ms |
39416 KB |
n=100 |
19 |
Correct |
35 ms |
39552 KB |
n=100 |
20 |
Correct |
39 ms |
39416 KB |
n=100 |
21 |
Correct |
35 ms |
39544 KB |
n=100 |
22 |
Correct |
39 ms |
39516 KB |
n=100 |
23 |
Correct |
38 ms |
39544 KB |
n=100 |
24 |
Correct |
32 ms |
39544 KB |
n=100 |
25 |
Correct |
42 ms |
39544 KB |
n=100 |
26 |
Correct |
41 ms |
39416 KB |
n=12 |
27 |
Correct |
41 ms |
39544 KB |
n=100 |
28 |
Correct |
43 ms |
39580 KB |
n=500 |
29 |
Correct |
39 ms |
39672 KB |
n=500 |
30 |
Correct |
40 ms |
39544 KB |
n=500 |
31 |
Correct |
44 ms |
39672 KB |
n=500 |
32 |
Correct |
40 ms |
39680 KB |
n=500 |
33 |
Correct |
45 ms |
39644 KB |
n=500 |
34 |
Correct |
38 ms |
39588 KB |
n=500 |
35 |
Correct |
43 ms |
39672 KB |
n=500 |
36 |
Correct |
42 ms |
39548 KB |
n=500 |
37 |
Correct |
39 ms |
39548 KB |
n=500 |
38 |
Correct |
43 ms |
39544 KB |
n=500 |
39 |
Correct |
48 ms |
39544 KB |
n=500 |
40 |
Correct |
50 ms |
39544 KB |
n=500 |
41 |
Correct |
45 ms |
39672 KB |
n=500 |
42 |
Correct |
47 ms |
39672 KB |
n=500 |
43 |
Correct |
40 ms |
39544 KB |
n=500 |
44 |
Correct |
39 ms |
39544 KB |
n=500 |
45 |
Correct |
43 ms |
39672 KB |
n=500 |
46 |
Correct |
41 ms |
39672 KB |
n=500 |
47 |
Correct |
41 ms |
39672 KB |
n=500 |
48 |
Correct |
37 ms |
39552 KB |
n=500 |
49 |
Correct |
41 ms |
39544 KB |
n=500 |
50 |
Correct |
48 ms |
39544 KB |
n=500 |
51 |
Correct |
40 ms |
39548 KB |
n=500 |
52 |
Correct |
42 ms |
39544 KB |
n=500 |
53 |
Correct |
42 ms |
39672 KB |
n=500 |
54 |
Correct |
41 ms |
39672 KB |
n=500 |
55 |
Correct |
35 ms |
39544 KB |
n=278 |
56 |
Correct |
38 ms |
39672 KB |
n=500 |
57 |
Correct |
39 ms |
39680 KB |
n=500 |
58 |
Correct |
39 ms |
39552 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
39544 KB |
n=5 |
2 |
Correct |
41 ms |
39552 KB |
n=100 |
3 |
Correct |
40 ms |
39416 KB |
n=100 |
4 |
Correct |
39 ms |
39516 KB |
n=100 |
5 |
Correct |
41 ms |
39420 KB |
n=100 |
6 |
Correct |
37 ms |
39464 KB |
n=100 |
7 |
Correct |
39 ms |
39524 KB |
n=100 |
8 |
Correct |
48 ms |
39416 KB |
n=100 |
9 |
Correct |
34 ms |
39552 KB |
n=100 |
10 |
Correct |
40 ms |
39416 KB |
n=100 |
11 |
Correct |
42 ms |
39544 KB |
n=100 |
12 |
Correct |
37 ms |
39416 KB |
n=100 |
13 |
Correct |
39 ms |
39544 KB |
n=100 |
14 |
Correct |
43 ms |
39628 KB |
n=100 |
15 |
Correct |
38 ms |
39480 KB |
n=100 |
16 |
Correct |
41 ms |
39568 KB |
n=100 |
17 |
Correct |
41 ms |
39416 KB |
n=100 |
18 |
Correct |
41 ms |
39416 KB |
n=100 |
19 |
Correct |
35 ms |
39552 KB |
n=100 |
20 |
Correct |
39 ms |
39416 KB |
n=100 |
21 |
Correct |
35 ms |
39544 KB |
n=100 |
22 |
Correct |
39 ms |
39516 KB |
n=100 |
23 |
Correct |
38 ms |
39544 KB |
n=100 |
24 |
Correct |
32 ms |
39544 KB |
n=100 |
25 |
Correct |
42 ms |
39544 KB |
n=100 |
26 |
Correct |
41 ms |
39416 KB |
n=12 |
27 |
Correct |
41 ms |
39544 KB |
n=100 |
28 |
Correct |
43 ms |
39580 KB |
n=500 |
29 |
Correct |
39 ms |
39672 KB |
n=500 |
30 |
Correct |
40 ms |
39544 KB |
n=500 |
31 |
Correct |
44 ms |
39672 KB |
n=500 |
32 |
Correct |
40 ms |
39680 KB |
n=500 |
33 |
Correct |
45 ms |
39644 KB |
n=500 |
34 |
Correct |
38 ms |
39588 KB |
n=500 |
35 |
Correct |
43 ms |
39672 KB |
n=500 |
36 |
Correct |
42 ms |
39548 KB |
n=500 |
37 |
Correct |
39 ms |
39548 KB |
n=500 |
38 |
Correct |
43 ms |
39544 KB |
n=500 |
39 |
Correct |
48 ms |
39544 KB |
n=500 |
40 |
Correct |
50 ms |
39544 KB |
n=500 |
41 |
Correct |
45 ms |
39672 KB |
n=500 |
42 |
Correct |
47 ms |
39672 KB |
n=500 |
43 |
Correct |
40 ms |
39544 KB |
n=500 |
44 |
Correct |
39 ms |
39544 KB |
n=500 |
45 |
Correct |
43 ms |
39672 KB |
n=500 |
46 |
Correct |
41 ms |
39672 KB |
n=500 |
47 |
Correct |
41 ms |
39672 KB |
n=500 |
48 |
Correct |
37 ms |
39552 KB |
n=500 |
49 |
Correct |
41 ms |
39544 KB |
n=500 |
50 |
Correct |
48 ms |
39544 KB |
n=500 |
51 |
Correct |
40 ms |
39548 KB |
n=500 |
52 |
Correct |
42 ms |
39544 KB |
n=500 |
53 |
Correct |
42 ms |
39672 KB |
n=500 |
54 |
Correct |
41 ms |
39672 KB |
n=500 |
55 |
Correct |
35 ms |
39544 KB |
n=278 |
56 |
Correct |
38 ms |
39672 KB |
n=500 |
57 |
Correct |
39 ms |
39680 KB |
n=500 |
58 |
Correct |
39 ms |
39552 KB |
n=500 |
59 |
Correct |
50 ms |
40008 KB |
n=2000 |
60 |
Correct |
52 ms |
40016 KB |
n=2000 |
61 |
Correct |
43 ms |
40060 KB |
n=2000 |
62 |
Correct |
50 ms |
40008 KB |
n=2000 |
63 |
Correct |
43 ms |
40064 KB |
n=2000 |
64 |
Correct |
59 ms |
40056 KB |
n=2000 |
65 |
Correct |
53 ms |
40056 KB |
n=2000 |
66 |
Correct |
43 ms |
40064 KB |
n=2000 |
67 |
Correct |
48 ms |
40056 KB |
n=2000 |
68 |
Correct |
46 ms |
40028 KB |
n=2000 |
69 |
Correct |
52 ms |
39936 KB |
n=2000 |
70 |
Correct |
50 ms |
40064 KB |
n=2000 |
71 |
Correct |
50 ms |
40056 KB |
n=2000 |
72 |
Correct |
55 ms |
40056 KB |
n=2000 |
73 |
Correct |
42 ms |
40060 KB |
n=2000 |
74 |
Correct |
42 ms |
39928 KB |
n=1844 |
75 |
Correct |
49 ms |
39928 KB |
n=2000 |
76 |
Correct |
81 ms |
39984 KB |
n=2000 |
77 |
Correct |
50 ms |
40044 KB |
n=2000 |
78 |
Correct |
48 ms |
39952 KB |
n=2000 |
79 |
Correct |
59 ms |
39928 KB |
n=2000 |
80 |
Correct |
47 ms |
40064 KB |
n=2000 |
81 |
Correct |
50 ms |
40056 KB |
n=2000 |
82 |
Correct |
46 ms |
39928 KB |
n=2000 |
83 |
Correct |
53 ms |
40056 KB |
n=2000 |
84 |
Correct |
50 ms |
40056 KB |
n=2000 |
85 |
Correct |
44 ms |
40056 KB |
n=2000 |
86 |
Correct |
51 ms |
40056 KB |
n=2000 |
87 |
Correct |
47 ms |
39928 KB |
n=2000 |
88 |
Correct |
58 ms |
40056 KB |
n=2000 |
89 |
Correct |
47 ms |
40056 KB |
n=2000 |
90 |
Correct |
53 ms |
40096 KB |
n=2000 |
91 |
Correct |
48 ms |
39928 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
39544 KB |
n=5 |
2 |
Correct |
41 ms |
39552 KB |
n=100 |
3 |
Correct |
40 ms |
39416 KB |
n=100 |
4 |
Correct |
39 ms |
39516 KB |
n=100 |
5 |
Correct |
41 ms |
39420 KB |
n=100 |
6 |
Correct |
37 ms |
39464 KB |
n=100 |
7 |
Correct |
39 ms |
39524 KB |
n=100 |
8 |
Correct |
48 ms |
39416 KB |
n=100 |
9 |
Correct |
34 ms |
39552 KB |
n=100 |
10 |
Correct |
40 ms |
39416 KB |
n=100 |
11 |
Correct |
42 ms |
39544 KB |
n=100 |
12 |
Correct |
37 ms |
39416 KB |
n=100 |
13 |
Correct |
39 ms |
39544 KB |
n=100 |
14 |
Correct |
43 ms |
39628 KB |
n=100 |
15 |
Correct |
38 ms |
39480 KB |
n=100 |
16 |
Correct |
41 ms |
39568 KB |
n=100 |
17 |
Correct |
41 ms |
39416 KB |
n=100 |
18 |
Correct |
41 ms |
39416 KB |
n=100 |
19 |
Correct |
35 ms |
39552 KB |
n=100 |
20 |
Correct |
39 ms |
39416 KB |
n=100 |
21 |
Correct |
35 ms |
39544 KB |
n=100 |
22 |
Correct |
39 ms |
39516 KB |
n=100 |
23 |
Correct |
38 ms |
39544 KB |
n=100 |
24 |
Correct |
32 ms |
39544 KB |
n=100 |
25 |
Correct |
42 ms |
39544 KB |
n=100 |
26 |
Correct |
41 ms |
39416 KB |
n=12 |
27 |
Correct |
41 ms |
39544 KB |
n=100 |
28 |
Correct |
43 ms |
39580 KB |
n=500 |
29 |
Correct |
39 ms |
39672 KB |
n=500 |
30 |
Correct |
40 ms |
39544 KB |
n=500 |
31 |
Correct |
44 ms |
39672 KB |
n=500 |
32 |
Correct |
40 ms |
39680 KB |
n=500 |
33 |
Correct |
45 ms |
39644 KB |
n=500 |
34 |
Correct |
38 ms |
39588 KB |
n=500 |
35 |
Correct |
43 ms |
39672 KB |
n=500 |
36 |
Correct |
42 ms |
39548 KB |
n=500 |
37 |
Correct |
39 ms |
39548 KB |
n=500 |
38 |
Correct |
43 ms |
39544 KB |
n=500 |
39 |
Correct |
48 ms |
39544 KB |
n=500 |
40 |
Correct |
50 ms |
39544 KB |
n=500 |
41 |
Correct |
45 ms |
39672 KB |
n=500 |
42 |
Correct |
47 ms |
39672 KB |
n=500 |
43 |
Correct |
40 ms |
39544 KB |
n=500 |
44 |
Correct |
39 ms |
39544 KB |
n=500 |
45 |
Correct |
43 ms |
39672 KB |
n=500 |
46 |
Correct |
41 ms |
39672 KB |
n=500 |
47 |
Correct |
41 ms |
39672 KB |
n=500 |
48 |
Correct |
37 ms |
39552 KB |
n=500 |
49 |
Correct |
41 ms |
39544 KB |
n=500 |
50 |
Correct |
48 ms |
39544 KB |
n=500 |
51 |
Correct |
40 ms |
39548 KB |
n=500 |
52 |
Correct |
42 ms |
39544 KB |
n=500 |
53 |
Correct |
42 ms |
39672 KB |
n=500 |
54 |
Correct |
41 ms |
39672 KB |
n=500 |
55 |
Correct |
35 ms |
39544 KB |
n=278 |
56 |
Correct |
38 ms |
39672 KB |
n=500 |
57 |
Correct |
39 ms |
39680 KB |
n=500 |
58 |
Correct |
39 ms |
39552 KB |
n=500 |
59 |
Correct |
50 ms |
40008 KB |
n=2000 |
60 |
Correct |
52 ms |
40016 KB |
n=2000 |
61 |
Correct |
43 ms |
40060 KB |
n=2000 |
62 |
Correct |
50 ms |
40008 KB |
n=2000 |
63 |
Correct |
43 ms |
40064 KB |
n=2000 |
64 |
Correct |
59 ms |
40056 KB |
n=2000 |
65 |
Correct |
53 ms |
40056 KB |
n=2000 |
66 |
Correct |
43 ms |
40064 KB |
n=2000 |
67 |
Correct |
48 ms |
40056 KB |
n=2000 |
68 |
Correct |
46 ms |
40028 KB |
n=2000 |
69 |
Correct |
52 ms |
39936 KB |
n=2000 |
70 |
Correct |
50 ms |
40064 KB |
n=2000 |
71 |
Correct |
50 ms |
40056 KB |
n=2000 |
72 |
Correct |
55 ms |
40056 KB |
n=2000 |
73 |
Correct |
42 ms |
40060 KB |
n=2000 |
74 |
Correct |
42 ms |
39928 KB |
n=1844 |
75 |
Correct |
49 ms |
39928 KB |
n=2000 |
76 |
Correct |
81 ms |
39984 KB |
n=2000 |
77 |
Correct |
50 ms |
40044 KB |
n=2000 |
78 |
Correct |
48 ms |
39952 KB |
n=2000 |
79 |
Correct |
59 ms |
39928 KB |
n=2000 |
80 |
Correct |
47 ms |
40064 KB |
n=2000 |
81 |
Correct |
50 ms |
40056 KB |
n=2000 |
82 |
Correct |
46 ms |
39928 KB |
n=2000 |
83 |
Correct |
53 ms |
40056 KB |
n=2000 |
84 |
Correct |
50 ms |
40056 KB |
n=2000 |
85 |
Correct |
44 ms |
40056 KB |
n=2000 |
86 |
Correct |
51 ms |
40056 KB |
n=2000 |
87 |
Correct |
47 ms |
39928 KB |
n=2000 |
88 |
Correct |
58 ms |
40056 KB |
n=2000 |
89 |
Correct |
47 ms |
40056 KB |
n=2000 |
90 |
Correct |
53 ms |
40096 KB |
n=2000 |
91 |
Correct |
48 ms |
39928 KB |
n=2000 |
92 |
Correct |
1781 ms |
93580 KB |
n=200000 |
93 |
Correct |
3475 ms |
98400 KB |
n=200000 |
94 |
Correct |
3715 ms |
101840 KB |
n=200000 |
95 |
Correct |
1860 ms |
93088 KB |
n=200000 |
96 |
Correct |
1929 ms |
93364 KB |
n=200000 |
97 |
Correct |
3842 ms |
97548 KB |
n=200000 |
98 |
Correct |
2096 ms |
93444 KB |
n=200000 |
99 |
Correct |
2256 ms |
93492 KB |
n=200000 |
100 |
Correct |
1916 ms |
93312 KB |
n=200000 |
101 |
Correct |
3388 ms |
102984 KB |
n=200000 |
102 |
Correct |
1642 ms |
94540 KB |
n=200000 |
103 |
Correct |
1525 ms |
94452 KB |
n=200000 |
104 |
Correct |
1634 ms |
94536 KB |
n=200000 |
105 |
Correct |
1498 ms |
94840 KB |
n=200000 |
106 |
Correct |
1531 ms |
94844 KB |
n=200000 |
107 |
Correct |
1404 ms |
94832 KB |
n=200000 |
108 |
Correct |
2431 ms |
93404 KB |
n=200000 |
109 |
Correct |
2188 ms |
93528 KB |
n=200000 |
110 |
Correct |
2245 ms |
93312 KB |
n=200000 |
111 |
Correct |
1832 ms |
92876 KB |
n=200000 |
112 |
Correct |
3470 ms |
101916 KB |
n=200000 |
113 |
Correct |
3598 ms |
97548 KB |
n=200000 |
114 |
Correct |
2001 ms |
92816 KB |
n=200000 |
115 |
Correct |
3817 ms |
95532 KB |
n=200000 |
116 |
Correct |
1786 ms |
93436 KB |
n=200000 |
117 |
Correct |
3567 ms |
102372 KB |
n=200000 |
118 |
Correct |
3883 ms |
96500 KB |
n=200000 |
119 |
Correct |
1996 ms |
93524 KB |
n=200000 |
120 |
Correct |
3364 ms |
102036 KB |
n=200000 |
121 |
Correct |
3351 ms |
102032 KB |
n=200000 |
122 |
Correct |
3390 ms |
102368 KB |
n=200000 |
123 |
Correct |
1704 ms |
94716 KB |
n=200000 |
124 |
Correct |
800 ms |
54804 KB |
n=25264 |