Submission #492871

# Submission time Handle Problem Language Result Execution time Memory
492871 2021-12-09T12:17:40 Z duchung Birthday gift (IZhO18_treearray) C++17
100 / 100
1231 ms 90396 KB
#include<bits/stdc++.h>
using namespace std;

const int N = 2e5 + 5;

int n , m , q;
vector<int> edge[N];
int depth[N];
int up[N][20];
int a[N];
set<int> pos[N][2];

void dfs(int u , int p = -1)
{
	for (int v : edge[u])
	{
		if (v == p) continue;
		
		up[v][0] = u;
		for (int j = 1 ; j < 20 ; ++j) up[v][j] = up[up[v][j - 1]][j - 1];

		depth[v] = depth[u] + 1;
		dfs(v , u);
	}
}

int lca(int u , int v)
{
	if (u == v) return u;
	if (depth[u] < depth[v]) swap(u , v);

	for (int j = 19 ; j >= 0 ; --j)
	{
		if (depth[u] - (1 << j) >= depth[v])
		{
			u = up[u][j];
		}
	}

	if (u == v) return u;

	for (int j = 19 ; j >= 0 ; --j)
	{
		if (up[u][j] != up[v][j])
		{
			u = up[u][j];
			v = up[v][j];
		}
	}

	return up[u][0];
}

int main()
{
	// freopen("input.inp","r",stdin);
	// freopen("output.out","w",stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> m >> q;
	for (int i = 1 ; i < n ; ++i)
	{
		int u , v;
		cin >> u >> v;
		edge[u].push_back(v);
		edge[v].push_back(u);
	}

	dfs(1);

	for (int i = 1 ; i <= m ; ++i) cin >> a[i];
	for (int i = 1 ; i <= m ; ++i) pos[a[i]][0].insert(i);
	for (int i = 1 ; i < m ; ++i) pos[lca(a[i] , a[i + 1])][1].insert(i);

	while(q--)
	{
		int opt;
		cin >> opt;
		if (opt == 1)
		{
			int p , v;
			cin >> p >> v;

			if (p + 1 <= m)
			{
				pos[lca(a[p] , a[p + 1])][1].erase(p);
				pos[lca(v , a[p + 1])][1].insert(p);
			}

			if (p >= 2)
			{
				pos[lca(a[p] , a[p - 1])][1].erase(p - 1);
				pos[lca(v , a[p - 1])][1].insert(p - 1);
			}

			pos[a[p]][0].erase(p);
			pos[v][0].insert(p);

			a[p] = v;
		}
		else
		{
			int l , r , v;
			cin >> l >> r >> v;

			auto single_node = pos[v][0].lower_bound(l);
			auto double_node = pos[v][1].lower_bound(l);

			if (single_node != pos[v][0].end() && *single_node <= r) cout << *single_node << " " << *single_node << "\n";
			else if (double_node != pos[v][1].end() && *double_node + 1 <= r) cout << *double_node << " " << *double_node + 1 << "\n";
			else cout << "-1 -1\n";
		}
	}

}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23756 KB n=5
2 Correct 11 ms 23804 KB n=100
3 Correct 11 ms 23808 KB n=100
4 Correct 14 ms 23756 KB n=100
5 Correct 13 ms 23804 KB n=100
6 Correct 14 ms 23756 KB n=100
7 Correct 13 ms 23756 KB n=100
8 Correct 14 ms 23756 KB n=100
9 Correct 12 ms 23808 KB n=100
10 Correct 13 ms 23756 KB n=100
11 Correct 13 ms 23808 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 15 ms 23800 KB n=100
14 Correct 14 ms 23884 KB n=100
15 Correct 13 ms 23812 KB n=100
16 Correct 15 ms 23724 KB n=100
17 Correct 13 ms 23744 KB n=100
18 Correct 13 ms 23756 KB n=100
19 Correct 12 ms 23760 KB n=100
20 Correct 12 ms 23788 KB n=100
21 Correct 13 ms 23812 KB n=100
22 Correct 14 ms 23812 KB n=100
23 Correct 12 ms 23756 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 17 ms 23800 KB n=100
26 Correct 16 ms 23820 KB n=12
27 Correct 13 ms 23732 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23756 KB n=5
2 Correct 11 ms 23804 KB n=100
3 Correct 11 ms 23808 KB n=100
4 Correct 14 ms 23756 KB n=100
5 Correct 13 ms 23804 KB n=100
6 Correct 14 ms 23756 KB n=100
7 Correct 13 ms 23756 KB n=100
8 Correct 14 ms 23756 KB n=100
9 Correct 12 ms 23808 KB n=100
10 Correct 13 ms 23756 KB n=100
11 Correct 13 ms 23808 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 15 ms 23800 KB n=100
14 Correct 14 ms 23884 KB n=100
15 Correct 13 ms 23812 KB n=100
16 Correct 15 ms 23724 KB n=100
17 Correct 13 ms 23744 KB n=100
18 Correct 13 ms 23756 KB n=100
19 Correct 12 ms 23760 KB n=100
20 Correct 12 ms 23788 KB n=100
21 Correct 13 ms 23812 KB n=100
22 Correct 14 ms 23812 KB n=100
23 Correct 12 ms 23756 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 17 ms 23800 KB n=100
26 Correct 16 ms 23820 KB n=12
27 Correct 13 ms 23732 KB n=100
28 Correct 12 ms 23944 KB n=500
29 Correct 13 ms 23884 KB n=500
30 Correct 14 ms 23884 KB n=500
31 Correct 16 ms 23924 KB n=500
32 Correct 15 ms 23884 KB n=500
33 Correct 14 ms 23928 KB n=500
34 Correct 12 ms 23940 KB n=500
35 Correct 13 ms 23944 KB n=500
36 Correct 13 ms 23884 KB n=500
37 Correct 13 ms 23872 KB n=500
38 Correct 14 ms 23940 KB n=500
39 Correct 13 ms 23928 KB n=500
40 Correct 14 ms 23948 KB n=500
41 Correct 15 ms 23944 KB n=500
42 Correct 14 ms 23884 KB n=500
43 Correct 13 ms 23884 KB n=500
44 Correct 14 ms 23816 KB n=500
45 Correct 14 ms 23884 KB n=500
46 Correct 12 ms 23884 KB n=500
47 Correct 13 ms 23900 KB n=500
48 Correct 14 ms 23948 KB n=500
49 Correct 18 ms 23884 KB n=500
50 Correct 16 ms 23860 KB n=500
51 Correct 13 ms 23884 KB n=500
52 Correct 17 ms 24008 KB n=500
53 Correct 14 ms 23948 KB n=500
54 Correct 15 ms 23884 KB n=500
55 Correct 12 ms 23884 KB n=278
56 Correct 13 ms 23924 KB n=500
57 Correct 15 ms 23884 KB n=500
58 Correct 13 ms 23852 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23756 KB n=5
2 Correct 11 ms 23804 KB n=100
3 Correct 11 ms 23808 KB n=100
4 Correct 14 ms 23756 KB n=100
5 Correct 13 ms 23804 KB n=100
6 Correct 14 ms 23756 KB n=100
7 Correct 13 ms 23756 KB n=100
8 Correct 14 ms 23756 KB n=100
9 Correct 12 ms 23808 KB n=100
10 Correct 13 ms 23756 KB n=100
11 Correct 13 ms 23808 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 15 ms 23800 KB n=100
14 Correct 14 ms 23884 KB n=100
15 Correct 13 ms 23812 KB n=100
16 Correct 15 ms 23724 KB n=100
17 Correct 13 ms 23744 KB n=100
18 Correct 13 ms 23756 KB n=100
19 Correct 12 ms 23760 KB n=100
20 Correct 12 ms 23788 KB n=100
21 Correct 13 ms 23812 KB n=100
22 Correct 14 ms 23812 KB n=100
23 Correct 12 ms 23756 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 17 ms 23800 KB n=100
26 Correct 16 ms 23820 KB n=12
27 Correct 13 ms 23732 KB n=100
28 Correct 12 ms 23944 KB n=500
29 Correct 13 ms 23884 KB n=500
30 Correct 14 ms 23884 KB n=500
31 Correct 16 ms 23924 KB n=500
32 Correct 15 ms 23884 KB n=500
33 Correct 14 ms 23928 KB n=500
34 Correct 12 ms 23940 KB n=500
35 Correct 13 ms 23944 KB n=500
36 Correct 13 ms 23884 KB n=500
37 Correct 13 ms 23872 KB n=500
38 Correct 14 ms 23940 KB n=500
39 Correct 13 ms 23928 KB n=500
40 Correct 14 ms 23948 KB n=500
41 Correct 15 ms 23944 KB n=500
42 Correct 14 ms 23884 KB n=500
43 Correct 13 ms 23884 KB n=500
44 Correct 14 ms 23816 KB n=500
45 Correct 14 ms 23884 KB n=500
46 Correct 12 ms 23884 KB n=500
47 Correct 13 ms 23900 KB n=500
48 Correct 14 ms 23948 KB n=500
49 Correct 18 ms 23884 KB n=500
50 Correct 16 ms 23860 KB n=500
51 Correct 13 ms 23884 KB n=500
52 Correct 17 ms 24008 KB n=500
53 Correct 14 ms 23948 KB n=500
54 Correct 15 ms 23884 KB n=500
55 Correct 12 ms 23884 KB n=278
56 Correct 13 ms 23924 KB n=500
57 Correct 15 ms 23884 KB n=500
58 Correct 13 ms 23852 KB n=500
59 Correct 15 ms 24204 KB n=2000
60 Correct 15 ms 24456 KB n=2000
61 Correct 15 ms 24260 KB n=2000
62 Correct 15 ms 24268 KB n=2000
63 Correct 16 ms 24204 KB n=2000
64 Correct 15 ms 24268 KB n=2000
65 Correct 17 ms 24256 KB n=2000
66 Correct 15 ms 24396 KB n=2000
67 Correct 16 ms 24260 KB n=2000
68 Correct 15 ms 24268 KB n=2000
69 Correct 15 ms 24192 KB n=2000
70 Correct 15 ms 24212 KB n=2000
71 Correct 16 ms 24220 KB n=2000
72 Correct 15 ms 24268 KB n=2000
73 Correct 15 ms 24208 KB n=2000
74 Correct 18 ms 24268 KB n=1844
75 Correct 14 ms 24300 KB n=2000
76 Correct 15 ms 24204 KB n=2000
77 Correct 15 ms 24308 KB n=2000
78 Correct 15 ms 24268 KB n=2000
79 Correct 18 ms 24268 KB n=2000
80 Correct 15 ms 24396 KB n=2000
81 Correct 16 ms 24332 KB n=2000
82 Correct 17 ms 24268 KB n=2000
83 Correct 19 ms 24404 KB n=2000
84 Correct 17 ms 24268 KB n=2000
85 Correct 15 ms 24328 KB n=2000
86 Correct 15 ms 24268 KB n=2000
87 Correct 16 ms 24268 KB n=2000
88 Correct 16 ms 24396 KB n=2000
89 Correct 15 ms 24396 KB n=2000
90 Correct 16 ms 24332 KB n=2000
91 Correct 17 ms 24200 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23756 KB n=5
2 Correct 11 ms 23804 KB n=100
3 Correct 11 ms 23808 KB n=100
4 Correct 14 ms 23756 KB n=100
5 Correct 13 ms 23804 KB n=100
6 Correct 14 ms 23756 KB n=100
7 Correct 13 ms 23756 KB n=100
8 Correct 14 ms 23756 KB n=100
9 Correct 12 ms 23808 KB n=100
10 Correct 13 ms 23756 KB n=100
11 Correct 13 ms 23808 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 15 ms 23800 KB n=100
14 Correct 14 ms 23884 KB n=100
15 Correct 13 ms 23812 KB n=100
16 Correct 15 ms 23724 KB n=100
17 Correct 13 ms 23744 KB n=100
18 Correct 13 ms 23756 KB n=100
19 Correct 12 ms 23760 KB n=100
20 Correct 12 ms 23788 KB n=100
21 Correct 13 ms 23812 KB n=100
22 Correct 14 ms 23812 KB n=100
23 Correct 12 ms 23756 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 17 ms 23800 KB n=100
26 Correct 16 ms 23820 KB n=12
27 Correct 13 ms 23732 KB n=100
28 Correct 12 ms 23944 KB n=500
29 Correct 13 ms 23884 KB n=500
30 Correct 14 ms 23884 KB n=500
31 Correct 16 ms 23924 KB n=500
32 Correct 15 ms 23884 KB n=500
33 Correct 14 ms 23928 KB n=500
34 Correct 12 ms 23940 KB n=500
35 Correct 13 ms 23944 KB n=500
36 Correct 13 ms 23884 KB n=500
37 Correct 13 ms 23872 KB n=500
38 Correct 14 ms 23940 KB n=500
39 Correct 13 ms 23928 KB n=500
40 Correct 14 ms 23948 KB n=500
41 Correct 15 ms 23944 KB n=500
42 Correct 14 ms 23884 KB n=500
43 Correct 13 ms 23884 KB n=500
44 Correct 14 ms 23816 KB n=500
45 Correct 14 ms 23884 KB n=500
46 Correct 12 ms 23884 KB n=500
47 Correct 13 ms 23900 KB n=500
48 Correct 14 ms 23948 KB n=500
49 Correct 18 ms 23884 KB n=500
50 Correct 16 ms 23860 KB n=500
51 Correct 13 ms 23884 KB n=500
52 Correct 17 ms 24008 KB n=500
53 Correct 14 ms 23948 KB n=500
54 Correct 15 ms 23884 KB n=500
55 Correct 12 ms 23884 KB n=278
56 Correct 13 ms 23924 KB n=500
57 Correct 15 ms 23884 KB n=500
58 Correct 13 ms 23852 KB n=500
59 Correct 15 ms 24204 KB n=2000
60 Correct 15 ms 24456 KB n=2000
61 Correct 15 ms 24260 KB n=2000
62 Correct 15 ms 24268 KB n=2000
63 Correct 16 ms 24204 KB n=2000
64 Correct 15 ms 24268 KB n=2000
65 Correct 17 ms 24256 KB n=2000
66 Correct 15 ms 24396 KB n=2000
67 Correct 16 ms 24260 KB n=2000
68 Correct 15 ms 24268 KB n=2000
69 Correct 15 ms 24192 KB n=2000
70 Correct 15 ms 24212 KB n=2000
71 Correct 16 ms 24220 KB n=2000
72 Correct 15 ms 24268 KB n=2000
73 Correct 15 ms 24208 KB n=2000
74 Correct 18 ms 24268 KB n=1844
75 Correct 14 ms 24300 KB n=2000
76 Correct 15 ms 24204 KB n=2000
77 Correct 15 ms 24308 KB n=2000
78 Correct 15 ms 24268 KB n=2000
79 Correct 18 ms 24268 KB n=2000
80 Correct 15 ms 24396 KB n=2000
81 Correct 16 ms 24332 KB n=2000
82 Correct 17 ms 24268 KB n=2000
83 Correct 19 ms 24404 KB n=2000
84 Correct 17 ms 24268 KB n=2000
85 Correct 15 ms 24328 KB n=2000
86 Correct 15 ms 24268 KB n=2000
87 Correct 16 ms 24268 KB n=2000
88 Correct 16 ms 24396 KB n=2000
89 Correct 15 ms 24396 KB n=2000
90 Correct 16 ms 24332 KB n=2000
91 Correct 17 ms 24200 KB n=2000
92 Correct 649 ms 74644 KB n=200000
93 Correct 1002 ms 82524 KB n=200000
94 Correct 956 ms 88112 KB n=200000
95 Correct 717 ms 74296 KB n=200000
96 Correct 668 ms 74488 KB n=200000
97 Correct 1091 ms 81036 KB n=200000
98 Correct 708 ms 74312 KB n=200000
99 Correct 826 ms 74476 KB n=200000
100 Correct 725 ms 74496 KB n=200000
101 Correct 978 ms 90396 KB n=200000
102 Correct 391 ms 75484 KB n=200000
103 Correct 432 ms 75552 KB n=200000
104 Correct 418 ms 75544 KB n=200000
105 Correct 389 ms 75892 KB n=200000
106 Correct 534 ms 75968 KB n=200000
107 Correct 438 ms 75968 KB n=200000
108 Correct 762 ms 74472 KB n=200000
109 Correct 799 ms 74436 KB n=200000
110 Correct 777 ms 74520 KB n=200000
111 Correct 726 ms 73920 KB n=200000
112 Correct 1057 ms 88444 KB n=200000
113 Correct 1072 ms 80872 KB n=200000
114 Correct 834 ms 73900 KB n=200000
115 Correct 1231 ms 77464 KB n=200000
116 Correct 836 ms 74464 KB n=200000
117 Correct 1097 ms 89216 KB n=200000
118 Correct 1174 ms 79100 KB n=200000
119 Correct 785 ms 74508 KB n=200000
120 Correct 937 ms 89236 KB n=200000
121 Correct 958 ms 89184 KB n=200000
122 Correct 906 ms 89624 KB n=200000
123 Correct 459 ms 75748 KB n=200000
124 Correct 275 ms 39352 KB n=25264