Submission #1109222

# Submission time Handle Problem Language Result Execution time Memory
1109222 2024-11-06T08:19:54 Z stdfloat Birthday gift (IZhO18_treearray) C++17
100 / 100
954 ms 80504 KB
#include <bits/stdc++.h>
using namespace std;

vector<int> d;

vector<vector<int>> E, sp;

void dfs(int x, int p = -1) {
	sp[x][0] = p;
	if (p != -1) d[x] = d[p] + 1;
	for (auto i : E[x])
		if (i != p) dfs(i, x);
}

int lca(int x, int y) {
	if (d[x] < d[y]) swap(x, y);

	for (int i = 19; i >= 0; i--)
		if (d[x] - (1 << i) >= d[y]) x = sp[x][i];

	if (x == y) return x;

	for (int i = 19; i >= 0; i--) {
		if (sp[x][i] != sp[y][i]) {
			x = sp[x][i]; y = sp[y][i];
		}
	}

	return sp[x][0];
}

int main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);

	int n, m, q;
	cin >> n >> m >> q;

	E.assign(n, {});
	for (int i = 1; i < n; i++) {
		int x, y;
		cin >> x >> y; x--; y--;

		E[x].push_back(y);
		E[y].push_back(x);
	}

	d.assign(n, 0);
	sp.assign(n, vector<int>(20, -1));
	dfs(0);

	for (int i = 1; i < 20; i++) {
		for (int j = 0; j < n; j++)
			if (sp[j][i - 1] != -1) sp[j][i] = sp[sp[j][i - 1]][i - 1];
	}

	vector<int> a(m);
	for (auto &i : a) {
		cin >> i; i--;
	}

	vector<set<pair<int, int>>> s(n);
	for (int i = 0; i < m; i++) {
		s[a[i]].insert({i, i});
		if (i + 1 < m) s[lca(a[i], a[i + 1])].insert({i, i + 1});
	}

	while (q--) {
		int tp;
		cin >> tp;

		if (tp == 1) {
			int ps, v;
			cin >> ps >> v; ps--; v--;
		
			s[a[ps]].erase({ps, ps});
			if (ps) s[lca(a[ps - 1], a[ps])].erase({ps - 1, ps});
			if (ps + 1 < m) s[lca(a[ps], a[ps + 1])].erase({ps, ps + 1});

			a[ps] = v;

			s[a[ps]].insert({ps, ps});
			if (ps) s[lca(a[ps - 1], a[ps])].insert({ps - 1, ps});
			if (ps + 1 < m) s[lca(a[ps], a[ps + 1])].insert({ps, ps + 1});
		}
		else {
			int l, r, v;
			cin >> l >> r >> v; l--; r--; v--;

			auto t = s[v].lower_bound({l, l});
			if (t == s[v].end() || (*t).second > r) cout << "-1 -1\n";
			else cout << (*t).first + 1 << ' ' << (*t).second + 1 << '\n';
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 336 KB n=100
3 Correct 1 ms 336 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 1 ms 336 KB n=100
7 Correct 1 ms 336 KB n=100
8 Correct 1 ms 336 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 1 ms 336 KB n=100
11 Correct 1 ms 336 KB n=100
12 Correct 1 ms 336 KB n=100
13 Correct 1 ms 348 KB n=100
14 Correct 1 ms 336 KB n=100
15 Correct 1 ms 336 KB n=100
16 Correct 1 ms 336 KB n=100
17 Correct 1 ms 336 KB n=100
18 Correct 1 ms 336 KB n=100
19 Correct 1 ms 336 KB n=100
20 Correct 1 ms 336 KB n=100
21 Correct 1 ms 336 KB n=100
22 Correct 1 ms 336 KB n=100
23 Correct 1 ms 336 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 1 ms 336 KB n=100
26 Correct 1 ms 336 KB n=12
27 Correct 1 ms 336 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 336 KB n=100
3 Correct 1 ms 336 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 1 ms 336 KB n=100
7 Correct 1 ms 336 KB n=100
8 Correct 1 ms 336 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 1 ms 336 KB n=100
11 Correct 1 ms 336 KB n=100
12 Correct 1 ms 336 KB n=100
13 Correct 1 ms 348 KB n=100
14 Correct 1 ms 336 KB n=100
15 Correct 1 ms 336 KB n=100
16 Correct 1 ms 336 KB n=100
17 Correct 1 ms 336 KB n=100
18 Correct 1 ms 336 KB n=100
19 Correct 1 ms 336 KB n=100
20 Correct 1 ms 336 KB n=100
21 Correct 1 ms 336 KB n=100
22 Correct 1 ms 336 KB n=100
23 Correct 1 ms 336 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 1 ms 336 KB n=100
26 Correct 1 ms 336 KB n=12
27 Correct 1 ms 336 KB n=100
28 Correct 1 ms 592 KB n=500
29 Correct 1 ms 592 KB n=500
30 Correct 2 ms 760 KB n=500
31 Correct 1 ms 592 KB n=500
32 Correct 1 ms 592 KB n=500
33 Correct 1 ms 592 KB n=500
34 Correct 1 ms 592 KB n=500
35 Correct 2 ms 592 KB n=500
36 Correct 1 ms 592 KB n=500
37 Correct 1 ms 592 KB n=500
38 Correct 1 ms 592 KB n=500
39 Correct 2 ms 592 KB n=500
40 Correct 1 ms 592 KB n=500
41 Correct 1 ms 592 KB n=500
42 Correct 2 ms 760 KB n=500
43 Correct 2 ms 592 KB n=500
44 Correct 2 ms 764 KB n=500
45 Correct 1 ms 592 KB n=500
46 Correct 1 ms 592 KB n=500
47 Correct 2 ms 592 KB n=500
48 Correct 2 ms 592 KB n=500
49 Correct 1 ms 592 KB n=500
50 Correct 1 ms 592 KB n=500
51 Correct 2 ms 592 KB n=500
52 Correct 1 ms 592 KB n=500
53 Correct 2 ms 592 KB n=500
54 Correct 1 ms 592 KB n=500
55 Correct 1 ms 336 KB n=278
56 Correct 1 ms 760 KB n=500
57 Correct 1 ms 592 KB n=500
58 Correct 1 ms 760 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 336 KB n=100
3 Correct 1 ms 336 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 1 ms 336 KB n=100
7 Correct 1 ms 336 KB n=100
8 Correct 1 ms 336 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 1 ms 336 KB n=100
11 Correct 1 ms 336 KB n=100
12 Correct 1 ms 336 KB n=100
13 Correct 1 ms 348 KB n=100
14 Correct 1 ms 336 KB n=100
15 Correct 1 ms 336 KB n=100
16 Correct 1 ms 336 KB n=100
17 Correct 1 ms 336 KB n=100
18 Correct 1 ms 336 KB n=100
19 Correct 1 ms 336 KB n=100
20 Correct 1 ms 336 KB n=100
21 Correct 1 ms 336 KB n=100
22 Correct 1 ms 336 KB n=100
23 Correct 1 ms 336 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 1 ms 336 KB n=100
26 Correct 1 ms 336 KB n=12
27 Correct 1 ms 336 KB n=100
28 Correct 1 ms 592 KB n=500
29 Correct 1 ms 592 KB n=500
30 Correct 2 ms 760 KB n=500
31 Correct 1 ms 592 KB n=500
32 Correct 1 ms 592 KB n=500
33 Correct 1 ms 592 KB n=500
34 Correct 1 ms 592 KB n=500
35 Correct 2 ms 592 KB n=500
36 Correct 1 ms 592 KB n=500
37 Correct 1 ms 592 KB n=500
38 Correct 1 ms 592 KB n=500
39 Correct 2 ms 592 KB n=500
40 Correct 1 ms 592 KB n=500
41 Correct 1 ms 592 KB n=500
42 Correct 2 ms 760 KB n=500
43 Correct 2 ms 592 KB n=500
44 Correct 2 ms 764 KB n=500
45 Correct 1 ms 592 KB n=500
46 Correct 1 ms 592 KB n=500
47 Correct 2 ms 592 KB n=500
48 Correct 2 ms 592 KB n=500
49 Correct 1 ms 592 KB n=500
50 Correct 1 ms 592 KB n=500
51 Correct 2 ms 592 KB n=500
52 Correct 1 ms 592 KB n=500
53 Correct 2 ms 592 KB n=500
54 Correct 1 ms 592 KB n=500
55 Correct 1 ms 336 KB n=278
56 Correct 1 ms 760 KB n=500
57 Correct 1 ms 592 KB n=500
58 Correct 1 ms 760 KB n=500
59 Correct 3 ms 1104 KB n=2000
60 Correct 4 ms 1104 KB n=2000
61 Correct 4 ms 1104 KB n=2000
62 Correct 4 ms 1104 KB n=2000
63 Correct 4 ms 1104 KB n=2000
64 Correct 4 ms 1272 KB n=2000
65 Correct 3 ms 1104 KB n=2000
66 Correct 4 ms 1104 KB n=2000
67 Correct 4 ms 1108 KB n=2000
68 Correct 4 ms 1104 KB n=2000
69 Correct 3 ms 1104 KB n=2000
70 Correct 3 ms 1104 KB n=2000
71 Correct 2 ms 1104 KB n=2000
72 Correct 3 ms 1104 KB n=2000
73 Correct 2 ms 1104 KB n=2000
74 Correct 3 ms 848 KB n=1844
75 Correct 3 ms 1104 KB n=2000
76 Correct 4 ms 1104 KB n=2000
77 Correct 5 ms 1104 KB n=2000
78 Correct 5 ms 1104 KB n=2000
79 Correct 3 ms 1104 KB n=2000
80 Correct 5 ms 1104 KB n=2000
81 Correct 4 ms 1104 KB n=2000
82 Correct 4 ms 1280 KB n=2000
83 Correct 3 ms 1104 KB n=2000
84 Correct 4 ms 1104 KB n=2000
85 Correct 4 ms 1104 KB n=2000
86 Correct 4 ms 1104 KB n=2000
87 Correct 4 ms 1104 KB n=2000
88 Correct 5 ms 1104 KB n=2000
89 Correct 5 ms 1104 KB n=2000
90 Correct 6 ms 1104 KB n=2000
91 Correct 3 ms 1104 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB n=5
2 Correct 1 ms 336 KB n=100
3 Correct 1 ms 336 KB n=100
4 Correct 1 ms 336 KB n=100
5 Correct 1 ms 336 KB n=100
6 Correct 1 ms 336 KB n=100
7 Correct 1 ms 336 KB n=100
8 Correct 1 ms 336 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 1 ms 336 KB n=100
11 Correct 1 ms 336 KB n=100
12 Correct 1 ms 336 KB n=100
13 Correct 1 ms 348 KB n=100
14 Correct 1 ms 336 KB n=100
15 Correct 1 ms 336 KB n=100
16 Correct 1 ms 336 KB n=100
17 Correct 1 ms 336 KB n=100
18 Correct 1 ms 336 KB n=100
19 Correct 1 ms 336 KB n=100
20 Correct 1 ms 336 KB n=100
21 Correct 1 ms 336 KB n=100
22 Correct 1 ms 336 KB n=100
23 Correct 1 ms 336 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 1 ms 336 KB n=100
26 Correct 1 ms 336 KB n=12
27 Correct 1 ms 336 KB n=100
28 Correct 1 ms 592 KB n=500
29 Correct 1 ms 592 KB n=500
30 Correct 2 ms 760 KB n=500
31 Correct 1 ms 592 KB n=500
32 Correct 1 ms 592 KB n=500
33 Correct 1 ms 592 KB n=500
34 Correct 1 ms 592 KB n=500
35 Correct 2 ms 592 KB n=500
36 Correct 1 ms 592 KB n=500
37 Correct 1 ms 592 KB n=500
38 Correct 1 ms 592 KB n=500
39 Correct 2 ms 592 KB n=500
40 Correct 1 ms 592 KB n=500
41 Correct 1 ms 592 KB n=500
42 Correct 2 ms 760 KB n=500
43 Correct 2 ms 592 KB n=500
44 Correct 2 ms 764 KB n=500
45 Correct 1 ms 592 KB n=500
46 Correct 1 ms 592 KB n=500
47 Correct 2 ms 592 KB n=500
48 Correct 2 ms 592 KB n=500
49 Correct 1 ms 592 KB n=500
50 Correct 1 ms 592 KB n=500
51 Correct 2 ms 592 KB n=500
52 Correct 1 ms 592 KB n=500
53 Correct 2 ms 592 KB n=500
54 Correct 1 ms 592 KB n=500
55 Correct 1 ms 336 KB n=278
56 Correct 1 ms 760 KB n=500
57 Correct 1 ms 592 KB n=500
58 Correct 1 ms 760 KB n=500
59 Correct 3 ms 1104 KB n=2000
60 Correct 4 ms 1104 KB n=2000
61 Correct 4 ms 1104 KB n=2000
62 Correct 4 ms 1104 KB n=2000
63 Correct 4 ms 1104 KB n=2000
64 Correct 4 ms 1272 KB n=2000
65 Correct 3 ms 1104 KB n=2000
66 Correct 4 ms 1104 KB n=2000
67 Correct 4 ms 1108 KB n=2000
68 Correct 4 ms 1104 KB n=2000
69 Correct 3 ms 1104 KB n=2000
70 Correct 3 ms 1104 KB n=2000
71 Correct 2 ms 1104 KB n=2000
72 Correct 3 ms 1104 KB n=2000
73 Correct 2 ms 1104 KB n=2000
74 Correct 3 ms 848 KB n=1844
75 Correct 3 ms 1104 KB n=2000
76 Correct 4 ms 1104 KB n=2000
77 Correct 5 ms 1104 KB n=2000
78 Correct 5 ms 1104 KB n=2000
79 Correct 3 ms 1104 KB n=2000
80 Correct 5 ms 1104 KB n=2000
81 Correct 4 ms 1104 KB n=2000
82 Correct 4 ms 1280 KB n=2000
83 Correct 3 ms 1104 KB n=2000
84 Correct 4 ms 1104 KB n=2000
85 Correct 4 ms 1104 KB n=2000
86 Correct 4 ms 1104 KB n=2000
87 Correct 4 ms 1104 KB n=2000
88 Correct 5 ms 1104 KB n=2000
89 Correct 5 ms 1104 KB n=2000
90 Correct 6 ms 1104 KB n=2000
91 Correct 3 ms 1104 KB n=2000
92 Correct 538 ms 66420 KB n=200000
93 Correct 811 ms 73288 KB n=200000
94 Correct 798 ms 77640 KB n=200000
95 Correct 528 ms 69820 KB n=200000
96 Correct 547 ms 71836 KB n=200000
97 Correct 771 ms 75456 KB n=200000
98 Correct 532 ms 71800 KB n=200000
99 Correct 639 ms 69592 KB n=200000
100 Correct 542 ms 66388 KB n=200000
101 Correct 794 ms 74880 KB n=200000
102 Correct 362 ms 69192 KB n=200000
103 Correct 332 ms 67400 KB n=200000
104 Correct 367 ms 67440 KB n=200000
105 Correct 440 ms 67108 KB n=200000
106 Correct 399 ms 70824 KB n=200000
107 Correct 351 ms 67144 KB n=200000
108 Correct 652 ms 71504 KB n=200000
109 Correct 582 ms 66120 KB n=200000
110 Correct 621 ms 66120 KB n=200000
111 Correct 567 ms 71328 KB n=200000
112 Correct 948 ms 73804 KB n=200000
113 Correct 824 ms 75532 KB n=200000
114 Correct 599 ms 71336 KB n=200000
115 Correct 751 ms 71156 KB n=200000
116 Correct 530 ms 71592 KB n=200000
117 Correct 954 ms 74168 KB n=200000
118 Correct 735 ms 72160 KB n=200000
119 Correct 451 ms 71392 KB n=200000
120 Correct 859 ms 74608 KB n=200000
121 Correct 741 ms 78408 KB n=200000
122 Correct 758 ms 80504 KB n=200000
123 Correct 349 ms 72776 KB n=200000
124 Correct 190 ms 17992 KB n=25264