Submission #876461

# Submission time Handle Problem Language Result Execution time Memory
876461 2023-11-21T19:15:37 Z serifefedartar Birthday gift (IZhO18_treearray) C++17
100 / 100
756 ms 91208 KB
#include <bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 998244353;
const ll LOGN = 21;
const ll MAXN = 2e5 + 100;

vector<vector<int>> graph;
vector<int> A;
int depth[MAXN], up[LOGN][MAXN];
set<int> single[MAXN], two[MAXN];
void dfs(int node, int parent) {
	for (auto u : graph[node]) {
		if (u == parent)
			continue;
		depth[u] = depth[node] + 1;
		up[0][u] = node;
		for (int i = 1; i < LOGN; i++)
			up[i][u] = up[i-1][up[i-1][u]];
		dfs(u, node);
	}
}

int find(int node, int k) {
	for (int i = 0; i < LOGN; i++) {
		if ((1<<i) & k)
			node = up[i][node];
	}
	return node;
}

int lca(int a, int b) {
	if (depth[b] > depth[a])
		swap(a, b);
	a = find(a, depth[a] - depth[b]);
	if (a == b)
		return a;

	for (int i = LOGN - 1; i >= 0; i--) {
		if (up[i][a] != up[i][b]) {
			a = up[i][a];
			b = up[i][b];
		}
	}
	return up[0][a];
}

int main() {
	fast
	int n, m, q, a, b;
	cin >> n >> m >> q;

	graph = vector<vector<int>>(n+1, vector<int>());
	A = vector<int>(m+1);
	for (int i = 1; i < n; i++) {
		cin >> a >> b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	for (int i = 0; i < LOGN; i++)
		up[i][1] = 1;
	dfs(1, 1);
	for (int i = 1; i <= m; i++) {
		cin >> A[i];
		single[A[i]].insert(i);
	}
	for (int i = 2; i <= m; i++)
		two[lca(A[i], A[i-1])].insert(i-1);

	while (q--) {
		int type;
		cin >> type;
		if (type == 1) {
			int pos, v;
			cin >> pos >> v;
			single[A[pos]].erase(pos);
			if (pos > 1)
				two[lca(A[pos], A[pos-1])].erase(pos-1);
			if (pos < m)
				two[lca(A[pos], A[pos+1])].erase(pos);

			A[pos] = v;
			single[A[pos]].insert(pos);
			if (pos > 1)
				two[lca(A[pos], A[pos-1])].insert(pos-1);
			if (pos < m)
				two[lca(A[pos], A[pos+1])].insert(pos);
		} else {
			int x, y, low;
			cin >> x >> y >> low;

			auto it = single[low].lower_bound(x);
			auto it2 = two[low].lower_bound(x);
			if (it != single[low].end() && *it <= y)
				cout << *it << " " << *it << "\n";
			else if (it2 != two[low].end() && *it2 + 1 <= y)
				cout << *it2 << " " << *it2 + 1 << "\n";
			else
				cout << "-1 -1\n";
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 35420 KB n=5
2 Correct 6 ms 35536 KB n=100
3 Correct 6 ms 35420 KB n=100
4 Correct 6 ms 35420 KB n=100
5 Correct 7 ms 35420 KB n=100
6 Correct 6 ms 35420 KB n=100
7 Correct 6 ms 35420 KB n=100
8 Correct 6 ms 35416 KB n=100
9 Correct 6 ms 35416 KB n=100
10 Correct 7 ms 35528 KB n=100
11 Correct 6 ms 35420 KB n=100
12 Correct 6 ms 35420 KB n=100
13 Correct 6 ms 35420 KB n=100
14 Correct 6 ms 35420 KB n=100
15 Correct 6 ms 35672 KB n=100
16 Correct 6 ms 35416 KB n=100
17 Correct 6 ms 35432 KB n=100
18 Correct 6 ms 35420 KB n=100
19 Correct 8 ms 35676 KB n=100
20 Correct 6 ms 35420 KB n=100
21 Correct 6 ms 35420 KB n=100
22 Correct 6 ms 35420 KB n=100
23 Correct 6 ms 35420 KB n=100
24 Correct 7 ms 35416 KB n=100
25 Correct 6 ms 35448 KB n=100
26 Correct 6 ms 35420 KB n=12
27 Correct 6 ms 35520 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 6 ms 35420 KB n=5
2 Correct 6 ms 35536 KB n=100
3 Correct 6 ms 35420 KB n=100
4 Correct 6 ms 35420 KB n=100
5 Correct 7 ms 35420 KB n=100
6 Correct 6 ms 35420 KB n=100
7 Correct 6 ms 35420 KB n=100
8 Correct 6 ms 35416 KB n=100
9 Correct 6 ms 35416 KB n=100
10 Correct 7 ms 35528 KB n=100
11 Correct 6 ms 35420 KB n=100
12 Correct 6 ms 35420 KB n=100
13 Correct 6 ms 35420 KB n=100
14 Correct 6 ms 35420 KB n=100
15 Correct 6 ms 35672 KB n=100
16 Correct 6 ms 35416 KB n=100
17 Correct 6 ms 35432 KB n=100
18 Correct 6 ms 35420 KB n=100
19 Correct 8 ms 35676 KB n=100
20 Correct 6 ms 35420 KB n=100
21 Correct 6 ms 35420 KB n=100
22 Correct 6 ms 35420 KB n=100
23 Correct 6 ms 35420 KB n=100
24 Correct 7 ms 35416 KB n=100
25 Correct 6 ms 35448 KB n=100
26 Correct 6 ms 35420 KB n=12
27 Correct 6 ms 35520 KB n=100
28 Correct 6 ms 35420 KB n=500
29 Correct 7 ms 35828 KB n=500
30 Correct 6 ms 35416 KB n=500
31 Correct 6 ms 35420 KB n=500
32 Correct 7 ms 35416 KB n=500
33 Correct 6 ms 35512 KB n=500
34 Correct 6 ms 35420 KB n=500
35 Correct 7 ms 35672 KB n=500
36 Correct 6 ms 35516 KB n=500
37 Correct 6 ms 35420 KB n=500
38 Correct 7 ms 35420 KB n=500
39 Correct 7 ms 35416 KB n=500
40 Correct 7 ms 35416 KB n=500
41 Correct 7 ms 35416 KB n=500
42 Correct 7 ms 35416 KB n=500
43 Correct 9 ms 35420 KB n=500
44 Correct 7 ms 35416 KB n=500
45 Correct 6 ms 35420 KB n=500
46 Correct 7 ms 35452 KB n=500
47 Correct 6 ms 35676 KB n=500
48 Correct 6 ms 35420 KB n=500
49 Correct 6 ms 35420 KB n=500
50 Correct 6 ms 35420 KB n=500
51 Correct 7 ms 35416 KB n=500
52 Correct 6 ms 35576 KB n=500
53 Correct 6 ms 35420 KB n=500
54 Correct 6 ms 35508 KB n=500
55 Correct 6 ms 35416 KB n=278
56 Correct 7 ms 35420 KB n=500
57 Correct 6 ms 35420 KB n=500
58 Correct 6 ms 35416 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 6 ms 35420 KB n=5
2 Correct 6 ms 35536 KB n=100
3 Correct 6 ms 35420 KB n=100
4 Correct 6 ms 35420 KB n=100
5 Correct 7 ms 35420 KB n=100
6 Correct 6 ms 35420 KB n=100
7 Correct 6 ms 35420 KB n=100
8 Correct 6 ms 35416 KB n=100
9 Correct 6 ms 35416 KB n=100
10 Correct 7 ms 35528 KB n=100
11 Correct 6 ms 35420 KB n=100
12 Correct 6 ms 35420 KB n=100
13 Correct 6 ms 35420 KB n=100
14 Correct 6 ms 35420 KB n=100
15 Correct 6 ms 35672 KB n=100
16 Correct 6 ms 35416 KB n=100
17 Correct 6 ms 35432 KB n=100
18 Correct 6 ms 35420 KB n=100
19 Correct 8 ms 35676 KB n=100
20 Correct 6 ms 35420 KB n=100
21 Correct 6 ms 35420 KB n=100
22 Correct 6 ms 35420 KB n=100
23 Correct 6 ms 35420 KB n=100
24 Correct 7 ms 35416 KB n=100
25 Correct 6 ms 35448 KB n=100
26 Correct 6 ms 35420 KB n=12
27 Correct 6 ms 35520 KB n=100
28 Correct 6 ms 35420 KB n=500
29 Correct 7 ms 35828 KB n=500
30 Correct 6 ms 35416 KB n=500
31 Correct 6 ms 35420 KB n=500
32 Correct 7 ms 35416 KB n=500
33 Correct 6 ms 35512 KB n=500
34 Correct 6 ms 35420 KB n=500
35 Correct 7 ms 35672 KB n=500
36 Correct 6 ms 35516 KB n=500
37 Correct 6 ms 35420 KB n=500
38 Correct 7 ms 35420 KB n=500
39 Correct 7 ms 35416 KB n=500
40 Correct 7 ms 35416 KB n=500
41 Correct 7 ms 35416 KB n=500
42 Correct 7 ms 35416 KB n=500
43 Correct 9 ms 35420 KB n=500
44 Correct 7 ms 35416 KB n=500
45 Correct 6 ms 35420 KB n=500
46 Correct 7 ms 35452 KB n=500
47 Correct 6 ms 35676 KB n=500
48 Correct 6 ms 35420 KB n=500
49 Correct 6 ms 35420 KB n=500
50 Correct 6 ms 35420 KB n=500
51 Correct 7 ms 35416 KB n=500
52 Correct 6 ms 35576 KB n=500
53 Correct 6 ms 35420 KB n=500
54 Correct 6 ms 35508 KB n=500
55 Correct 6 ms 35416 KB n=278
56 Correct 7 ms 35420 KB n=500
57 Correct 6 ms 35420 KB n=500
58 Correct 6 ms 35416 KB n=500
59 Correct 8 ms 35928 KB n=2000
60 Correct 9 ms 35932 KB n=2000
61 Correct 8 ms 35932 KB n=2000
62 Correct 9 ms 35932 KB n=2000
63 Correct 8 ms 35676 KB n=2000
64 Correct 9 ms 35768 KB n=2000
65 Correct 8 ms 35896 KB n=2000
66 Correct 8 ms 35928 KB n=2000
67 Correct 8 ms 35712 KB n=2000
68 Correct 8 ms 35928 KB n=2000
69 Correct 8 ms 35712 KB n=2000
70 Correct 7 ms 35696 KB n=2000
71 Correct 8 ms 35776 KB n=2000
72 Correct 8 ms 35676 KB n=2000
73 Correct 7 ms 35672 KB n=2000
74 Correct 7 ms 35676 KB n=1844
75 Correct 8 ms 35672 KB n=2000
76 Correct 8 ms 35676 KB n=2000
77 Correct 8 ms 35676 KB n=2000
78 Correct 9 ms 35676 KB n=2000
79 Correct 8 ms 35900 KB n=2000
80 Correct 9 ms 35932 KB n=2000
81 Correct 9 ms 35928 KB n=2000
82 Correct 9 ms 35672 KB n=2000
83 Correct 8 ms 35932 KB n=2000
84 Correct 8 ms 35676 KB n=2000
85 Correct 9 ms 35928 KB n=2000
86 Correct 9 ms 35932 KB n=2000
87 Correct 8 ms 35676 KB n=2000
88 Correct 8 ms 35928 KB n=2000
89 Correct 8 ms 36044 KB n=2000
90 Correct 8 ms 35932 KB n=2000
91 Correct 8 ms 35676 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 6 ms 35420 KB n=5
2 Correct 6 ms 35536 KB n=100
3 Correct 6 ms 35420 KB n=100
4 Correct 6 ms 35420 KB n=100
5 Correct 7 ms 35420 KB n=100
6 Correct 6 ms 35420 KB n=100
7 Correct 6 ms 35420 KB n=100
8 Correct 6 ms 35416 KB n=100
9 Correct 6 ms 35416 KB n=100
10 Correct 7 ms 35528 KB n=100
11 Correct 6 ms 35420 KB n=100
12 Correct 6 ms 35420 KB n=100
13 Correct 6 ms 35420 KB n=100
14 Correct 6 ms 35420 KB n=100
15 Correct 6 ms 35672 KB n=100
16 Correct 6 ms 35416 KB n=100
17 Correct 6 ms 35432 KB n=100
18 Correct 6 ms 35420 KB n=100
19 Correct 8 ms 35676 KB n=100
20 Correct 6 ms 35420 KB n=100
21 Correct 6 ms 35420 KB n=100
22 Correct 6 ms 35420 KB n=100
23 Correct 6 ms 35420 KB n=100
24 Correct 7 ms 35416 KB n=100
25 Correct 6 ms 35448 KB n=100
26 Correct 6 ms 35420 KB n=12
27 Correct 6 ms 35520 KB n=100
28 Correct 6 ms 35420 KB n=500
29 Correct 7 ms 35828 KB n=500
30 Correct 6 ms 35416 KB n=500
31 Correct 6 ms 35420 KB n=500
32 Correct 7 ms 35416 KB n=500
33 Correct 6 ms 35512 KB n=500
34 Correct 6 ms 35420 KB n=500
35 Correct 7 ms 35672 KB n=500
36 Correct 6 ms 35516 KB n=500
37 Correct 6 ms 35420 KB n=500
38 Correct 7 ms 35420 KB n=500
39 Correct 7 ms 35416 KB n=500
40 Correct 7 ms 35416 KB n=500
41 Correct 7 ms 35416 KB n=500
42 Correct 7 ms 35416 KB n=500
43 Correct 9 ms 35420 KB n=500
44 Correct 7 ms 35416 KB n=500
45 Correct 6 ms 35420 KB n=500
46 Correct 7 ms 35452 KB n=500
47 Correct 6 ms 35676 KB n=500
48 Correct 6 ms 35420 KB n=500
49 Correct 6 ms 35420 KB n=500
50 Correct 6 ms 35420 KB n=500
51 Correct 7 ms 35416 KB n=500
52 Correct 6 ms 35576 KB n=500
53 Correct 6 ms 35420 KB n=500
54 Correct 6 ms 35508 KB n=500
55 Correct 6 ms 35416 KB n=278
56 Correct 7 ms 35420 KB n=500
57 Correct 6 ms 35420 KB n=500
58 Correct 6 ms 35416 KB n=500
59 Correct 8 ms 35928 KB n=2000
60 Correct 9 ms 35932 KB n=2000
61 Correct 8 ms 35932 KB n=2000
62 Correct 9 ms 35932 KB n=2000
63 Correct 8 ms 35676 KB n=2000
64 Correct 9 ms 35768 KB n=2000
65 Correct 8 ms 35896 KB n=2000
66 Correct 8 ms 35928 KB n=2000
67 Correct 8 ms 35712 KB n=2000
68 Correct 8 ms 35928 KB n=2000
69 Correct 8 ms 35712 KB n=2000
70 Correct 7 ms 35696 KB n=2000
71 Correct 8 ms 35776 KB n=2000
72 Correct 8 ms 35676 KB n=2000
73 Correct 7 ms 35672 KB n=2000
74 Correct 7 ms 35676 KB n=1844
75 Correct 8 ms 35672 KB n=2000
76 Correct 8 ms 35676 KB n=2000
77 Correct 8 ms 35676 KB n=2000
78 Correct 9 ms 35676 KB n=2000
79 Correct 8 ms 35900 KB n=2000
80 Correct 9 ms 35932 KB n=2000
81 Correct 9 ms 35928 KB n=2000
82 Correct 9 ms 35672 KB n=2000
83 Correct 8 ms 35932 KB n=2000
84 Correct 8 ms 35676 KB n=2000
85 Correct 9 ms 35928 KB n=2000
86 Correct 9 ms 35932 KB n=2000
87 Correct 8 ms 35676 KB n=2000
88 Correct 8 ms 35928 KB n=2000
89 Correct 8 ms 36044 KB n=2000
90 Correct 8 ms 35932 KB n=2000
91 Correct 8 ms 35676 KB n=2000
92 Correct 628 ms 75556 KB n=200000
93 Correct 753 ms 83400 KB n=200000
94 Correct 640 ms 88976 KB n=200000
95 Correct 557 ms 75208 KB n=200000
96 Correct 567 ms 75352 KB n=200000
97 Correct 738 ms 81892 KB n=200000
98 Correct 565 ms 75436 KB n=200000
99 Correct 721 ms 75332 KB n=200000
100 Correct 579 ms 75208 KB n=200000
101 Correct 607 ms 91208 KB n=200000
102 Correct 277 ms 76692 KB n=200000
103 Correct 264 ms 76376 KB n=200000
104 Correct 287 ms 76496 KB n=200000
105 Correct 282 ms 76828 KB n=200000
106 Correct 294 ms 77140 KB n=200000
107 Correct 284 ms 76700 KB n=200000
108 Correct 664 ms 75604 KB n=200000
109 Correct 630 ms 75356 KB n=200000
110 Correct 613 ms 75404 KB n=200000
111 Correct 529 ms 75044 KB n=200000
112 Correct 628 ms 89456 KB n=200000
113 Correct 716 ms 81960 KB n=200000
114 Correct 512 ms 74696 KB n=200000
115 Correct 756 ms 78304 KB n=200000
116 Correct 513 ms 75492 KB n=200000
117 Correct 623 ms 90200 KB n=200000
118 Correct 711 ms 80144 KB n=200000
119 Correct 568 ms 75492 KB n=200000
120 Correct 560 ms 90164 KB n=200000
121 Correct 547 ms 90148 KB n=200000
122 Correct 589 ms 90436 KB n=200000
123 Correct 309 ms 76672 KB n=200000
124 Correct 126 ms 49524 KB n=25264