Submission #654111

# Submission time Handle Problem Language Result Execution time Memory
654111 2022-10-30T02:23:15 Z thiago_bastos Birthday gift (IZhO18_treearray) C++17
100 / 100
1737 ms 72276 KB
#include "bits/stdc++.h"

using namespace std;

#define INF 1000000000
#define INFLL 1000000000000000000ll
#define EPS 1e-9
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
#define fi first
#define sc second
 
using i64 = long long;
using u64 = unsigned long long;
using ld = long double;
using ii = pair<int, int>;	

const int N = 2e5 + 100, K = 18;

int sp[K][N], depth[N], a[N], n, m, q;

vector<int> adj[N];

set<tuple<int, int, int>> S;

void dfs(int u, int p) {
	sp[0][u] = p;
	for(int i = 1; (1 << i) < n; ++i) sp[i][u] = sp[i - 1][sp[i - 1][u]];
	for(int v : adj[u]) {
		if(v == p) continue;
		depth[v] = 1 + depth[u];
		dfs(v, u);
	}
}

int lca(int u, int v) {
	if(depth[u] > depth[v]) swap(u, v);
	int d = depth[v] - depth[u];
	for(int i = 0; (1 << i) <= d; ++i) if(d & (1 << i)) v = sp[i][v];
	if(u == v) return u;
	for(int i = 31 - __builtin_clz(depth[u]); i >= 0; --i) {
		if(sp[i][u] == sp[i][v]) continue;
		u = sp[i][u], v = sp[i][v];
	}
	return sp[0][u];
}

void solve() {
	cin >> n >> m >> q;

	for(int i = 1; i < n; ++i) {
		int u, v;
		cin >> u >> v;
		--u, --v;
		adj[u].pb(v); adj[v].pb(u);
	}

	dfs(0, 0);

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

	while(q--) {
		int t;
		cin >> t;
		if(t == 1) {
			int pos, v;
			cin >> pos >> v;
			--pos, --v;
			if(pos) S.erase(make_tuple(lca(a[pos], a[pos - 1]), pos - 1, pos));
			if(pos + 1 < m) S.erase(make_tuple(lca(a[pos], a[pos + 1]), pos, pos + 1));
			S.erase(make_tuple(a[pos], pos, pos));
			a[pos] = v;
			S.insert(make_tuple(a[pos], pos, pos));
			if(pos) S.insert(make_tuple(lca(a[pos], a[pos - 1]), pos - 1, pos));
			if(pos + 1 < m) S.insert(make_tuple(lca(a[pos], a[pos + 1]), pos, pos + 1));
		} else {
			int l, r, v;
			cin >> l >> r >> v;
			--l, --r, --v;
			auto it = S.lower_bound(make_tuple(v, l, l));
			if(it == S.end() || get<0>(*it) != v || get<2>(*it) > r) {
				cout << "-1 -1\n";
				continue;
			}
			cout << 1 + get<1>(*it) << ' ' << 1 + get<2>(*it) << '\n';
		}
	}
}	

int main() {
	ios_base :: sync_with_stdio(false);
	cin.tie(0);
	int t = 1;
 //	cin >> t;
	while(t--) solve();
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB n=5
2 Correct 3 ms 5076 KB n=100
3 Correct 3 ms 5076 KB n=100
4 Correct 3 ms 5076 KB n=100
5 Correct 3 ms 5076 KB n=100
6 Correct 3 ms 5076 KB n=100
7 Correct 3 ms 5076 KB n=100
8 Correct 3 ms 5076 KB n=100
9 Correct 3 ms 5044 KB n=100
10 Correct 3 ms 5076 KB n=100
11 Correct 3 ms 5076 KB n=100
12 Correct 3 ms 5076 KB n=100
13 Correct 4 ms 5076 KB n=100
14 Correct 3 ms 5028 KB n=100
15 Correct 3 ms 5076 KB n=100
16 Correct 3 ms 5028 KB n=100
17 Correct 3 ms 5032 KB n=100
18 Correct 3 ms 5024 KB n=100
19 Correct 3 ms 5076 KB n=100
20 Correct 3 ms 5076 KB n=100
21 Correct 3 ms 5076 KB n=100
22 Correct 3 ms 5024 KB n=100
23 Correct 3 ms 5076 KB n=100
24 Correct 3 ms 5076 KB n=100
25 Correct 4 ms 5076 KB n=100
26 Correct 3 ms 4948 KB n=12
27 Correct 3 ms 5032 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB n=5
2 Correct 3 ms 5076 KB n=100
3 Correct 3 ms 5076 KB n=100
4 Correct 3 ms 5076 KB n=100
5 Correct 3 ms 5076 KB n=100
6 Correct 3 ms 5076 KB n=100
7 Correct 3 ms 5076 KB n=100
8 Correct 3 ms 5076 KB n=100
9 Correct 3 ms 5044 KB n=100
10 Correct 3 ms 5076 KB n=100
11 Correct 3 ms 5076 KB n=100
12 Correct 3 ms 5076 KB n=100
13 Correct 4 ms 5076 KB n=100
14 Correct 3 ms 5028 KB n=100
15 Correct 3 ms 5076 KB n=100
16 Correct 3 ms 5028 KB n=100
17 Correct 3 ms 5032 KB n=100
18 Correct 3 ms 5024 KB n=100
19 Correct 3 ms 5076 KB n=100
20 Correct 3 ms 5076 KB n=100
21 Correct 3 ms 5076 KB n=100
22 Correct 3 ms 5024 KB n=100
23 Correct 3 ms 5076 KB n=100
24 Correct 3 ms 5076 KB n=100
25 Correct 4 ms 5076 KB n=100
26 Correct 3 ms 4948 KB n=12
27 Correct 3 ms 5032 KB n=100
28 Correct 5 ms 5204 KB n=500
29 Correct 5 ms 5204 KB n=500
30 Correct 4 ms 5204 KB n=500
31 Correct 4 ms 5204 KB n=500
32 Correct 4 ms 5168 KB n=500
33 Correct 4 ms 5204 KB n=500
34 Correct 3 ms 5160 KB n=500
35 Correct 4 ms 5168 KB n=500
36 Correct 3 ms 5160 KB n=500
37 Correct 3 ms 5076 KB n=500
38 Correct 3 ms 5160 KB n=500
39 Correct 3 ms 5204 KB n=500
40 Correct 3 ms 5204 KB n=500
41 Correct 4 ms 5204 KB n=500
42 Correct 4 ms 5076 KB n=500
43 Correct 4 ms 5160 KB n=500
44 Correct 3 ms 5076 KB n=500
45 Correct 4 ms 5204 KB n=500
46 Correct 4 ms 5172 KB n=500
47 Correct 4 ms 5176 KB n=500
48 Correct 4 ms 5160 KB n=500
49 Correct 4 ms 5160 KB n=500
50 Correct 3 ms 5204 KB n=500
51 Correct 4 ms 5204 KB n=500
52 Correct 4 ms 5204 KB n=500
53 Correct 4 ms 5204 KB n=500
54 Correct 4 ms 5204 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 4 ms 5204 KB n=500
57 Correct 4 ms 5156 KB n=500
58 Correct 4 ms 5164 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB n=5
2 Correct 3 ms 5076 KB n=100
3 Correct 3 ms 5076 KB n=100
4 Correct 3 ms 5076 KB n=100
5 Correct 3 ms 5076 KB n=100
6 Correct 3 ms 5076 KB n=100
7 Correct 3 ms 5076 KB n=100
8 Correct 3 ms 5076 KB n=100
9 Correct 3 ms 5044 KB n=100
10 Correct 3 ms 5076 KB n=100
11 Correct 3 ms 5076 KB n=100
12 Correct 3 ms 5076 KB n=100
13 Correct 4 ms 5076 KB n=100
14 Correct 3 ms 5028 KB n=100
15 Correct 3 ms 5076 KB n=100
16 Correct 3 ms 5028 KB n=100
17 Correct 3 ms 5032 KB n=100
18 Correct 3 ms 5024 KB n=100
19 Correct 3 ms 5076 KB n=100
20 Correct 3 ms 5076 KB n=100
21 Correct 3 ms 5076 KB n=100
22 Correct 3 ms 5024 KB n=100
23 Correct 3 ms 5076 KB n=100
24 Correct 3 ms 5076 KB n=100
25 Correct 4 ms 5076 KB n=100
26 Correct 3 ms 4948 KB n=12
27 Correct 3 ms 5032 KB n=100
28 Correct 5 ms 5204 KB n=500
29 Correct 5 ms 5204 KB n=500
30 Correct 4 ms 5204 KB n=500
31 Correct 4 ms 5204 KB n=500
32 Correct 4 ms 5168 KB n=500
33 Correct 4 ms 5204 KB n=500
34 Correct 3 ms 5160 KB n=500
35 Correct 4 ms 5168 KB n=500
36 Correct 3 ms 5160 KB n=500
37 Correct 3 ms 5076 KB n=500
38 Correct 3 ms 5160 KB n=500
39 Correct 3 ms 5204 KB n=500
40 Correct 3 ms 5204 KB n=500
41 Correct 4 ms 5204 KB n=500
42 Correct 4 ms 5076 KB n=500
43 Correct 4 ms 5160 KB n=500
44 Correct 3 ms 5076 KB n=500
45 Correct 4 ms 5204 KB n=500
46 Correct 4 ms 5172 KB n=500
47 Correct 4 ms 5176 KB n=500
48 Correct 4 ms 5160 KB n=500
49 Correct 4 ms 5160 KB n=500
50 Correct 3 ms 5204 KB n=500
51 Correct 4 ms 5204 KB n=500
52 Correct 4 ms 5204 KB n=500
53 Correct 4 ms 5204 KB n=500
54 Correct 4 ms 5204 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 4 ms 5204 KB n=500
57 Correct 4 ms 5156 KB n=500
58 Correct 4 ms 5164 KB n=500
59 Correct 5 ms 5460 KB n=2000
60 Correct 9 ms 5588 KB n=2000
61 Correct 9 ms 5588 KB n=2000
62 Correct 8 ms 5588 KB n=2000
63 Correct 6 ms 5460 KB n=2000
64 Correct 6 ms 5588 KB n=2000
65 Correct 6 ms 5460 KB n=2000
66 Correct 7 ms 5640 KB n=2000
67 Correct 7 ms 5460 KB n=2000
68 Correct 7 ms 5552 KB n=2000
69 Correct 6 ms 5588 KB n=2000
70 Correct 6 ms 5588 KB n=2000
71 Correct 6 ms 5460 KB n=2000
72 Correct 5 ms 5508 KB n=2000
73 Correct 6 ms 5556 KB n=2000
74 Correct 6 ms 5460 KB n=1844
75 Correct 5 ms 5584 KB n=2000
76 Correct 6 ms 5552 KB n=2000
77 Correct 7 ms 5588 KB n=2000
78 Correct 6 ms 5504 KB n=2000
79 Correct 7 ms 5548 KB n=2000
80 Correct 8 ms 5556 KB n=2000
81 Correct 7 ms 5588 KB n=2000
82 Correct 9 ms 5460 KB n=2000
83 Correct 7 ms 5588 KB n=2000
84 Correct 6 ms 5460 KB n=2000
85 Correct 6 ms 5588 KB n=2000
86 Correct 7 ms 5512 KB n=2000
87 Correct 6 ms 5588 KB n=2000
88 Correct 6 ms 5640 KB n=2000
89 Correct 7 ms 5588 KB n=2000
90 Correct 7 ms 5604 KB n=2000
91 Correct 6 ms 5548 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB n=5
2 Correct 3 ms 5076 KB n=100
3 Correct 3 ms 5076 KB n=100
4 Correct 3 ms 5076 KB n=100
5 Correct 3 ms 5076 KB n=100
6 Correct 3 ms 5076 KB n=100
7 Correct 3 ms 5076 KB n=100
8 Correct 3 ms 5076 KB n=100
9 Correct 3 ms 5044 KB n=100
10 Correct 3 ms 5076 KB n=100
11 Correct 3 ms 5076 KB n=100
12 Correct 3 ms 5076 KB n=100
13 Correct 4 ms 5076 KB n=100
14 Correct 3 ms 5028 KB n=100
15 Correct 3 ms 5076 KB n=100
16 Correct 3 ms 5028 KB n=100
17 Correct 3 ms 5032 KB n=100
18 Correct 3 ms 5024 KB n=100
19 Correct 3 ms 5076 KB n=100
20 Correct 3 ms 5076 KB n=100
21 Correct 3 ms 5076 KB n=100
22 Correct 3 ms 5024 KB n=100
23 Correct 3 ms 5076 KB n=100
24 Correct 3 ms 5076 KB n=100
25 Correct 4 ms 5076 KB n=100
26 Correct 3 ms 4948 KB n=12
27 Correct 3 ms 5032 KB n=100
28 Correct 5 ms 5204 KB n=500
29 Correct 5 ms 5204 KB n=500
30 Correct 4 ms 5204 KB n=500
31 Correct 4 ms 5204 KB n=500
32 Correct 4 ms 5168 KB n=500
33 Correct 4 ms 5204 KB n=500
34 Correct 3 ms 5160 KB n=500
35 Correct 4 ms 5168 KB n=500
36 Correct 3 ms 5160 KB n=500
37 Correct 3 ms 5076 KB n=500
38 Correct 3 ms 5160 KB n=500
39 Correct 3 ms 5204 KB n=500
40 Correct 3 ms 5204 KB n=500
41 Correct 4 ms 5204 KB n=500
42 Correct 4 ms 5076 KB n=500
43 Correct 4 ms 5160 KB n=500
44 Correct 3 ms 5076 KB n=500
45 Correct 4 ms 5204 KB n=500
46 Correct 4 ms 5172 KB n=500
47 Correct 4 ms 5176 KB n=500
48 Correct 4 ms 5160 KB n=500
49 Correct 4 ms 5160 KB n=500
50 Correct 3 ms 5204 KB n=500
51 Correct 4 ms 5204 KB n=500
52 Correct 4 ms 5204 KB n=500
53 Correct 4 ms 5204 KB n=500
54 Correct 4 ms 5204 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 4 ms 5204 KB n=500
57 Correct 4 ms 5156 KB n=500
58 Correct 4 ms 5164 KB n=500
59 Correct 5 ms 5460 KB n=2000
60 Correct 9 ms 5588 KB n=2000
61 Correct 9 ms 5588 KB n=2000
62 Correct 8 ms 5588 KB n=2000
63 Correct 6 ms 5460 KB n=2000
64 Correct 6 ms 5588 KB n=2000
65 Correct 6 ms 5460 KB n=2000
66 Correct 7 ms 5640 KB n=2000
67 Correct 7 ms 5460 KB n=2000
68 Correct 7 ms 5552 KB n=2000
69 Correct 6 ms 5588 KB n=2000
70 Correct 6 ms 5588 KB n=2000
71 Correct 6 ms 5460 KB n=2000
72 Correct 5 ms 5508 KB n=2000
73 Correct 6 ms 5556 KB n=2000
74 Correct 6 ms 5460 KB n=1844
75 Correct 5 ms 5584 KB n=2000
76 Correct 6 ms 5552 KB n=2000
77 Correct 7 ms 5588 KB n=2000
78 Correct 6 ms 5504 KB n=2000
79 Correct 7 ms 5548 KB n=2000
80 Correct 8 ms 5556 KB n=2000
81 Correct 7 ms 5588 KB n=2000
82 Correct 9 ms 5460 KB n=2000
83 Correct 7 ms 5588 KB n=2000
84 Correct 6 ms 5460 KB n=2000
85 Correct 6 ms 5588 KB n=2000
86 Correct 7 ms 5512 KB n=2000
87 Correct 6 ms 5588 KB n=2000
88 Correct 6 ms 5640 KB n=2000
89 Correct 7 ms 5588 KB n=2000
90 Correct 7 ms 5604 KB n=2000
91 Correct 6 ms 5548 KB n=2000
92 Correct 940 ms 60456 KB n=200000
93 Correct 1385 ms 66232 KB n=200000
94 Correct 1377 ms 70548 KB n=200000
95 Correct 981 ms 60248 KB n=200000
96 Correct 1121 ms 60308 KB n=200000
97 Correct 1737 ms 64992 KB n=200000
98 Correct 885 ms 60252 KB n=200000
99 Correct 1219 ms 60032 KB n=200000
100 Correct 1016 ms 60328 KB n=200000
101 Correct 1352 ms 72176 KB n=200000
102 Correct 556 ms 61540 KB n=200000
103 Correct 501 ms 61404 KB n=200000
104 Correct 585 ms 61476 KB n=200000
105 Correct 526 ms 61316 KB n=200000
106 Correct 558 ms 61432 KB n=200000
107 Correct 563 ms 61432 KB n=200000
108 Correct 1098 ms 60308 KB n=200000
109 Correct 1059 ms 60244 KB n=200000
110 Correct 1088 ms 60280 KB n=200000
111 Correct 849 ms 59764 KB n=200000
112 Correct 1296 ms 71124 KB n=200000
113 Correct 1485 ms 65016 KB n=200000
114 Correct 856 ms 59824 KB n=200000
115 Correct 1509 ms 62200 KB n=200000
116 Correct 972 ms 60480 KB n=200000
117 Correct 1420 ms 71692 KB n=200000
118 Correct 1506 ms 63612 KB n=200000
119 Correct 856 ms 60324 KB n=200000
120 Correct 1313 ms 72060 KB n=200000
121 Correct 1249 ms 72068 KB n=200000
122 Correct 1208 ms 72276 KB n=200000
123 Correct 597 ms 61260 KB n=200000
124 Correct 281 ms 22652 KB n=25264