Submission #167664

# Submission time Handle Problem Language Result Execution time Memory
167664 2019-12-09T10:31:12 Z shuvi_dola Birthday gift (IZhO18_treearray) C++14
100 / 100
1513 ms 81964 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector <int> g[N];
int a[N], dep[N], par[N][23];
set <int> posver[N], poslca[N];
int n, m, q;
void dfs(int s, int p) {
	par[s][0] = p;
	for(int i = 1; i <= 20; i++) {
		par[s][i] = par[par[s][i - 1]][i - 1];
	}
	for(auto u : g[s]) {
		if(u == p) continue;
		dep[u] = dep[s] + 1;
		dfs(u, s);
	}
}

int lca(int u, int v) {
	if(dep[u] < dep[v]) swap(u, v);
	for(int i = 20; i >= 0; i--) if(dep[u] - dep[v] >= (1 << i)) u = par[u][i];
	if(u == v) return u;
	for(int i = 20; i >= 0; i--)  if(par[u][i] != par[v][i]) u = par[u][i], v = par[v][i]; 
	return par[u][0];
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m >> q;
	for(int i = 1; i < n; i++) {
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	dfs(1, 0);
	for(int i = 1; i <= m; i++) {
		cin >> a[i];
		posver[a[i]].insert(i);
		if(i > 1) poslca[lca(a[i], a[i - 1])].insert(i);
	}

	while(q--) {
		int typ, x, y ,ver;
		cin >> typ >> x >> y;
		if(typ == 1) {
			posver[a[x]].erase(x);
			if(x > 1) poslca[lca(a[x], a[x - 1])].erase(x);
			if(x < m) poslca[lca(a[x], a[x + 1])].erase(x + 1);
			a[x] = y;
			posver[a[x]].insert(x);
			if(x > 1) poslca[lca(a[x], a[x - 1])].insert(x);
			if(x < m) poslca[lca(a[x], a[x + 1])].insert(x + 1);
		}

		else {
			cin >> ver;
			if(posver[ver].size() > 0) {
				auto L = posver[ver].lower_bound(x);
				if(L != posver[ver].end() && *L <= y) {
					cout << *L << ' ' << *L << '\n';
					continue; 
				}
			}
			if(poslca[ver].size() > 0) {
				auto L = poslca[ver].lower_bound(x + 1);
				if(L != poslca[ver].end() && *L <= y) {
					cout << (*L) - 1 << ' ' << *L << '\n';
					continue;
				}
			}
			cout << "-1 -1 \n";
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23800 KB n=100
3 Correct 23 ms 23800 KB n=100
4 Correct 23 ms 23928 KB n=100
5 Correct 23 ms 23800 KB n=100
6 Correct 22 ms 23800 KB n=100
7 Correct 22 ms 23800 KB n=100
8 Correct 23 ms 23800 KB n=100
9 Correct 22 ms 23796 KB n=100
10 Correct 23 ms 23800 KB n=100
11 Correct 23 ms 23800 KB n=100
12 Correct 23 ms 23800 KB n=100
13 Correct 23 ms 23800 KB n=100
14 Correct 22 ms 23800 KB n=100
15 Correct 22 ms 23800 KB n=100
16 Correct 24 ms 23908 KB n=100
17 Correct 23 ms 23800 KB n=100
18 Correct 23 ms 23928 KB n=100
19 Correct 23 ms 23800 KB n=100
20 Correct 23 ms 23800 KB n=100
21 Correct 23 ms 23800 KB n=100
22 Correct 27 ms 23800 KB n=100
23 Correct 22 ms 23860 KB n=100
24 Correct 23 ms 23800 KB n=100
25 Correct 23 ms 23800 KB n=100
26 Correct 23 ms 23800 KB n=12
27 Correct 23 ms 23800 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23800 KB n=100
3 Correct 23 ms 23800 KB n=100
4 Correct 23 ms 23928 KB n=100
5 Correct 23 ms 23800 KB n=100
6 Correct 22 ms 23800 KB n=100
7 Correct 22 ms 23800 KB n=100
8 Correct 23 ms 23800 KB n=100
9 Correct 22 ms 23796 KB n=100
10 Correct 23 ms 23800 KB n=100
11 Correct 23 ms 23800 KB n=100
12 Correct 23 ms 23800 KB n=100
13 Correct 23 ms 23800 KB n=100
14 Correct 22 ms 23800 KB n=100
15 Correct 22 ms 23800 KB n=100
16 Correct 24 ms 23908 KB n=100
17 Correct 23 ms 23800 KB n=100
18 Correct 23 ms 23928 KB n=100
19 Correct 23 ms 23800 KB n=100
20 Correct 23 ms 23800 KB n=100
21 Correct 23 ms 23800 KB n=100
22 Correct 27 ms 23800 KB n=100
23 Correct 22 ms 23860 KB n=100
24 Correct 23 ms 23800 KB n=100
25 Correct 23 ms 23800 KB n=100
26 Correct 23 ms 23800 KB n=12
27 Correct 23 ms 23800 KB n=100
28 Correct 24 ms 24056 KB n=500
29 Correct 23 ms 23900 KB n=500
30 Correct 23 ms 23928 KB n=500
31 Correct 23 ms 23928 KB n=500
32 Correct 23 ms 23928 KB n=500
33 Correct 25 ms 23928 KB n=500
34 Correct 24 ms 23928 KB n=500
35 Correct 23 ms 24056 KB n=500
36 Correct 23 ms 23928 KB n=500
37 Correct 24 ms 23928 KB n=500
38 Correct 24 ms 24008 KB n=500
39 Correct 24 ms 24028 KB n=500
40 Correct 24 ms 23928 KB n=500
41 Correct 23 ms 23932 KB n=500
42 Correct 24 ms 23996 KB n=500
43 Correct 24 ms 23928 KB n=500
44 Correct 24 ms 23928 KB n=500
45 Correct 24 ms 23928 KB n=500
46 Correct 23 ms 23928 KB n=500
47 Correct 24 ms 23928 KB n=500
48 Correct 24 ms 23928 KB n=500
49 Correct 24 ms 24028 KB n=500
50 Correct 24 ms 23928 KB n=500
51 Correct 24 ms 23928 KB n=500
52 Correct 25 ms 24056 KB n=500
53 Correct 29 ms 23920 KB n=500
54 Correct 25 ms 23928 KB n=500
55 Correct 23 ms 23928 KB n=278
56 Correct 24 ms 23928 KB n=500
57 Correct 24 ms 23928 KB n=500
58 Correct 25 ms 24020 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23800 KB n=100
3 Correct 23 ms 23800 KB n=100
4 Correct 23 ms 23928 KB n=100
5 Correct 23 ms 23800 KB n=100
6 Correct 22 ms 23800 KB n=100
7 Correct 22 ms 23800 KB n=100
8 Correct 23 ms 23800 KB n=100
9 Correct 22 ms 23796 KB n=100
10 Correct 23 ms 23800 KB n=100
11 Correct 23 ms 23800 KB n=100
12 Correct 23 ms 23800 KB n=100
13 Correct 23 ms 23800 KB n=100
14 Correct 22 ms 23800 KB n=100
15 Correct 22 ms 23800 KB n=100
16 Correct 24 ms 23908 KB n=100
17 Correct 23 ms 23800 KB n=100
18 Correct 23 ms 23928 KB n=100
19 Correct 23 ms 23800 KB n=100
20 Correct 23 ms 23800 KB n=100
21 Correct 23 ms 23800 KB n=100
22 Correct 27 ms 23800 KB n=100
23 Correct 22 ms 23860 KB n=100
24 Correct 23 ms 23800 KB n=100
25 Correct 23 ms 23800 KB n=100
26 Correct 23 ms 23800 KB n=12
27 Correct 23 ms 23800 KB n=100
28 Correct 24 ms 24056 KB n=500
29 Correct 23 ms 23900 KB n=500
30 Correct 23 ms 23928 KB n=500
31 Correct 23 ms 23928 KB n=500
32 Correct 23 ms 23928 KB n=500
33 Correct 25 ms 23928 KB n=500
34 Correct 24 ms 23928 KB n=500
35 Correct 23 ms 24056 KB n=500
36 Correct 23 ms 23928 KB n=500
37 Correct 24 ms 23928 KB n=500
38 Correct 24 ms 24008 KB n=500
39 Correct 24 ms 24028 KB n=500
40 Correct 24 ms 23928 KB n=500
41 Correct 23 ms 23932 KB n=500
42 Correct 24 ms 23996 KB n=500
43 Correct 24 ms 23928 KB n=500
44 Correct 24 ms 23928 KB n=500
45 Correct 24 ms 23928 KB n=500
46 Correct 23 ms 23928 KB n=500
47 Correct 24 ms 23928 KB n=500
48 Correct 24 ms 23928 KB n=500
49 Correct 24 ms 24028 KB n=500
50 Correct 24 ms 23928 KB n=500
51 Correct 24 ms 23928 KB n=500
52 Correct 25 ms 24056 KB n=500
53 Correct 29 ms 23920 KB n=500
54 Correct 25 ms 23928 KB n=500
55 Correct 23 ms 23928 KB n=278
56 Correct 24 ms 23928 KB n=500
57 Correct 24 ms 23928 KB n=500
58 Correct 25 ms 24020 KB n=500
59 Correct 27 ms 24288 KB n=2000
60 Correct 27 ms 24440 KB n=2000
61 Correct 28 ms 24440 KB n=2000
62 Correct 28 ms 24312 KB n=2000
63 Correct 27 ms 24312 KB n=2000
64 Correct 28 ms 24440 KB n=2000
65 Correct 28 ms 24312 KB n=2000
66 Correct 28 ms 24440 KB n=2000
67 Correct 28 ms 24312 KB n=2000
68 Correct 28 ms 24440 KB n=2000
69 Correct 27 ms 24288 KB n=2000
70 Correct 27 ms 24312 KB n=2000
71 Correct 27 ms 24312 KB n=2000
72 Correct 26 ms 24312 KB n=2000
73 Correct 27 ms 24312 KB n=2000
74 Correct 27 ms 24312 KB n=1844
75 Correct 27 ms 24312 KB n=2000
76 Correct 28 ms 24312 KB n=2000
77 Correct 28 ms 24312 KB n=2000
78 Correct 28 ms 24392 KB n=2000
79 Correct 28 ms 24312 KB n=2000
80 Correct 28 ms 24440 KB n=2000
81 Correct 29 ms 24396 KB n=2000
82 Correct 28 ms 24312 KB n=2000
83 Correct 28 ms 24440 KB n=2000
84 Correct 28 ms 24384 KB n=2000
85 Correct 28 ms 24312 KB n=2000
86 Correct 27 ms 24312 KB n=2000
87 Correct 27 ms 24324 KB n=2000
88 Correct 27 ms 24312 KB n=2000
89 Correct 27 ms 24444 KB n=2000
90 Correct 28 ms 24456 KB n=2000
91 Correct 26 ms 24440 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23800 KB n=100
3 Correct 23 ms 23800 KB n=100
4 Correct 23 ms 23928 KB n=100
5 Correct 23 ms 23800 KB n=100
6 Correct 22 ms 23800 KB n=100
7 Correct 22 ms 23800 KB n=100
8 Correct 23 ms 23800 KB n=100
9 Correct 22 ms 23796 KB n=100
10 Correct 23 ms 23800 KB n=100
11 Correct 23 ms 23800 KB n=100
12 Correct 23 ms 23800 KB n=100
13 Correct 23 ms 23800 KB n=100
14 Correct 22 ms 23800 KB n=100
15 Correct 22 ms 23800 KB n=100
16 Correct 24 ms 23908 KB n=100
17 Correct 23 ms 23800 KB n=100
18 Correct 23 ms 23928 KB n=100
19 Correct 23 ms 23800 KB n=100
20 Correct 23 ms 23800 KB n=100
21 Correct 23 ms 23800 KB n=100
22 Correct 27 ms 23800 KB n=100
23 Correct 22 ms 23860 KB n=100
24 Correct 23 ms 23800 KB n=100
25 Correct 23 ms 23800 KB n=100
26 Correct 23 ms 23800 KB n=12
27 Correct 23 ms 23800 KB n=100
28 Correct 24 ms 24056 KB n=500
29 Correct 23 ms 23900 KB n=500
30 Correct 23 ms 23928 KB n=500
31 Correct 23 ms 23928 KB n=500
32 Correct 23 ms 23928 KB n=500
33 Correct 25 ms 23928 KB n=500
34 Correct 24 ms 23928 KB n=500
35 Correct 23 ms 24056 KB n=500
36 Correct 23 ms 23928 KB n=500
37 Correct 24 ms 23928 KB n=500
38 Correct 24 ms 24008 KB n=500
39 Correct 24 ms 24028 KB n=500
40 Correct 24 ms 23928 KB n=500
41 Correct 23 ms 23932 KB n=500
42 Correct 24 ms 23996 KB n=500
43 Correct 24 ms 23928 KB n=500
44 Correct 24 ms 23928 KB n=500
45 Correct 24 ms 23928 KB n=500
46 Correct 23 ms 23928 KB n=500
47 Correct 24 ms 23928 KB n=500
48 Correct 24 ms 23928 KB n=500
49 Correct 24 ms 24028 KB n=500
50 Correct 24 ms 23928 KB n=500
51 Correct 24 ms 23928 KB n=500
52 Correct 25 ms 24056 KB n=500
53 Correct 29 ms 23920 KB n=500
54 Correct 25 ms 23928 KB n=500
55 Correct 23 ms 23928 KB n=278
56 Correct 24 ms 23928 KB n=500
57 Correct 24 ms 23928 KB n=500
58 Correct 25 ms 24020 KB n=500
59 Correct 27 ms 24288 KB n=2000
60 Correct 27 ms 24440 KB n=2000
61 Correct 28 ms 24440 KB n=2000
62 Correct 28 ms 24312 KB n=2000
63 Correct 27 ms 24312 KB n=2000
64 Correct 28 ms 24440 KB n=2000
65 Correct 28 ms 24312 KB n=2000
66 Correct 28 ms 24440 KB n=2000
67 Correct 28 ms 24312 KB n=2000
68 Correct 28 ms 24440 KB n=2000
69 Correct 27 ms 24288 KB n=2000
70 Correct 27 ms 24312 KB n=2000
71 Correct 27 ms 24312 KB n=2000
72 Correct 26 ms 24312 KB n=2000
73 Correct 27 ms 24312 KB n=2000
74 Correct 27 ms 24312 KB n=1844
75 Correct 27 ms 24312 KB n=2000
76 Correct 28 ms 24312 KB n=2000
77 Correct 28 ms 24312 KB n=2000
78 Correct 28 ms 24392 KB n=2000
79 Correct 28 ms 24312 KB n=2000
80 Correct 28 ms 24440 KB n=2000
81 Correct 29 ms 24396 KB n=2000
82 Correct 28 ms 24312 KB n=2000
83 Correct 28 ms 24440 KB n=2000
84 Correct 28 ms 24384 KB n=2000
85 Correct 28 ms 24312 KB n=2000
86 Correct 27 ms 24312 KB n=2000
87 Correct 27 ms 24324 KB n=2000
88 Correct 27 ms 24312 KB n=2000
89 Correct 27 ms 24444 KB n=2000
90 Correct 28 ms 24456 KB n=2000
91 Correct 26 ms 24440 KB n=2000
92 Correct 913 ms 73516 KB n=200000
93 Correct 1268 ms 76500 KB n=200000
94 Correct 1238 ms 79584 KB n=200000
95 Correct 932 ms 74348 KB n=200000
96 Correct 836 ms 73640 KB n=200000
97 Correct 1513 ms 75792 KB n=200000
98 Correct 895 ms 74900 KB n=200000
99 Correct 1074 ms 72348 KB n=200000
100 Correct 889 ms 76144 KB n=200000
101 Correct 1227 ms 81964 KB n=200000
102 Correct 587 ms 72824 KB n=200000
103 Correct 556 ms 72800 KB n=200000
104 Correct 590 ms 72648 KB n=200000
105 Correct 645 ms 73464 KB n=200000
106 Correct 572 ms 72868 KB n=200000
107 Correct 557 ms 73352 KB n=200000
108 Correct 1010 ms 71536 KB n=200000
109 Correct 975 ms 71516 KB n=200000
110 Correct 1008 ms 71604 KB n=200000
111 Correct 906 ms 76096 KB n=200000
112 Correct 1257 ms 80860 KB n=200000
113 Correct 1351 ms 80896 KB n=200000
114 Correct 884 ms 76264 KB n=200000
115 Correct 1359 ms 74240 KB n=200000
116 Correct 842 ms 75244 KB n=200000
117 Correct 1196 ms 80160 KB n=200000
118 Correct 1289 ms 75400 KB n=200000
119 Correct 872 ms 71516 KB n=200000
120 Correct 1137 ms 78852 KB n=200000
121 Correct 1145 ms 78700 KB n=200000
122 Correct 1127 ms 78920 KB n=200000
123 Correct 539 ms 70744 KB n=200000
124 Correct 309 ms 37188 KB n=25264