Submission #683357

# Submission time Handle Problem Language Result Execution time Memory
683357 2023-01-18T08:42:30 Z iskhakkutbilim Birthday gift (IZhO18_treearray) C++14
100 / 100
972 ms 91068 KB
#include <bits/stdc++.h>
using namespace std;

//#define int long long
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int N = 2e5;
const int LOG = 25;
int up[N + 1][LOG], depth[N+1];
vector<int> g[N+1];
vector< set<int> > LCA(N+1);
vector< set<int> > where(N+1);
int n, m, q;
vector<int> a;
void dfs(int v, int p){
	up[v][0] = p;
	for(int j = 1;j < LOG; j++){
		up[v][j] = up[up[v][j-1]][j-1];
	}
	for(int to : g[v]){
		if(to == p) continue;
		depth[to] = depth[v]+1;
		dfs(to, v);
	}
}

int lca(int a, int b){
	if(depth[a] > depth[b]) swap(a, b);
	int k = depth[b]-depth[a];
	for(int i = 0;i < LOG; i++){
		if(k&(1<<i)){
			b = up[b][i];
		}
	}
	if(a == b) return b;
	for(int i = LOG-1; i >= 0; i--){
		if(up[a][i] != up[b][i]){
			a = up[a][i], b = up[b][i];
		}
	}
	return up[a][0];
}


main(){
   ios::sync_with_stdio(0);
   cin.tie(0); cout.tie(0);
	cin >> n >> m >> q;
	a.resize(m);
	for(int i = 1;i < n; i++){
		int a, b; cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	for(int i = 0;i < m; i++){
		cin >> a[i];
		where[a[i]].insert(i);
	}
	dfs(1, 1);
	for(int i = 0;i < m-1; i++){
		LCA[lca(a[i], a[i+1])].insert(i);
	}
	while(q--){
		int type; cin >> type;
		if(type == 1){
			int pos, v; cin >> pos >> v; pos--;
			if(pos < m-1){
				LCA[lca(a[pos], a[pos+1])].erase(pos);
				LCA[lca(v, a[pos+1])].insert(pos);
			}
			if(pos > 0){
				LCA[lca(a[pos], a[pos-1])].erase(pos-1);
				LCA[lca(v, a[pos-1])].insert(pos-1);
			}
			where[a[pos]].erase(pos);
			a[pos] = v;
			where[v].insert(pos);
		}else{
			int l, r, v; cin >> l >> r >> v; l--, r--;
			auto it = where[v].lower_bound(l);
			if(it != where[v].end() and *it >= l and *it <= r){
				cout << *it+1 << ' ' << *it+1 << '\n';
				continue;
			}
			it = LCA[v].lower_bound(l);
			if(it != LCA[v].end() and *it >=l and *it < r){
				cout << *it+1 << ' ' << *it+2 << '\n';
				continue;
			}
			cout << "-1 -1" << '\n';
		}
	}
	return 0;
}

Compilation message

treearray.cpp:46:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   46 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23764 KB n=5
2 Correct 14 ms 23800 KB n=100
3 Correct 13 ms 23764 KB n=100
4 Correct 13 ms 23836 KB n=100
5 Correct 13 ms 23756 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 14 ms 23916 KB n=100
8 Correct 12 ms 23796 KB n=100
9 Correct 13 ms 23796 KB n=100
10 Correct 12 ms 23732 KB n=100
11 Correct 13 ms 23764 KB n=100
12 Correct 12 ms 23732 KB n=100
13 Correct 13 ms 23840 KB n=100
14 Correct 13 ms 23764 KB n=100
15 Correct 13 ms 23764 KB n=100
16 Correct 13 ms 23820 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 13 ms 23764 KB n=100
19 Correct 12 ms 23764 KB n=100
20 Correct 13 ms 23764 KB n=100
21 Correct 13 ms 23800 KB n=100
22 Correct 13 ms 23764 KB n=100
23 Correct 13 ms 23776 KB n=100
24 Correct 13 ms 23764 KB n=100
25 Correct 12 ms 23764 KB n=100
26 Correct 12 ms 23728 KB n=12
27 Correct 13 ms 23840 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23764 KB n=5
2 Correct 14 ms 23800 KB n=100
3 Correct 13 ms 23764 KB n=100
4 Correct 13 ms 23836 KB n=100
5 Correct 13 ms 23756 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 14 ms 23916 KB n=100
8 Correct 12 ms 23796 KB n=100
9 Correct 13 ms 23796 KB n=100
10 Correct 12 ms 23732 KB n=100
11 Correct 13 ms 23764 KB n=100
12 Correct 12 ms 23732 KB n=100
13 Correct 13 ms 23840 KB n=100
14 Correct 13 ms 23764 KB n=100
15 Correct 13 ms 23764 KB n=100
16 Correct 13 ms 23820 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 13 ms 23764 KB n=100
19 Correct 12 ms 23764 KB n=100
20 Correct 13 ms 23764 KB n=100
21 Correct 13 ms 23800 KB n=100
22 Correct 13 ms 23764 KB n=100
23 Correct 13 ms 23776 KB n=100
24 Correct 13 ms 23764 KB n=100
25 Correct 12 ms 23764 KB n=100
26 Correct 12 ms 23728 KB n=12
27 Correct 13 ms 23840 KB n=100
28 Correct 15 ms 23872 KB n=500
29 Correct 16 ms 23892 KB n=500
30 Correct 13 ms 23916 KB n=500
31 Correct 14 ms 23916 KB n=500
32 Correct 13 ms 23892 KB n=500
33 Correct 13 ms 24020 KB n=500
34 Correct 13 ms 23892 KB n=500
35 Correct 13 ms 23892 KB n=500
36 Correct 13 ms 23892 KB n=500
37 Correct 13 ms 23892 KB n=500
38 Correct 16 ms 23820 KB n=500
39 Correct 14 ms 23892 KB n=500
40 Correct 13 ms 23892 KB n=500
41 Correct 13 ms 23928 KB n=500
42 Correct 14 ms 23972 KB n=500
43 Correct 13 ms 23892 KB n=500
44 Correct 13 ms 23892 KB n=500
45 Correct 13 ms 23892 KB n=500
46 Correct 14 ms 23944 KB n=500
47 Correct 13 ms 23932 KB n=500
48 Correct 13 ms 23896 KB n=500
49 Correct 13 ms 23944 KB n=500
50 Correct 13 ms 23892 KB n=500
51 Correct 15 ms 23892 KB n=500
52 Correct 13 ms 23964 KB n=500
53 Correct 16 ms 23892 KB n=500
54 Correct 14 ms 23892 KB n=500
55 Correct 16 ms 23812 KB n=278
56 Correct 13 ms 23892 KB n=500
57 Correct 15 ms 23916 KB n=500
58 Correct 16 ms 23932 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23764 KB n=5
2 Correct 14 ms 23800 KB n=100
3 Correct 13 ms 23764 KB n=100
4 Correct 13 ms 23836 KB n=100
5 Correct 13 ms 23756 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 14 ms 23916 KB n=100
8 Correct 12 ms 23796 KB n=100
9 Correct 13 ms 23796 KB n=100
10 Correct 12 ms 23732 KB n=100
11 Correct 13 ms 23764 KB n=100
12 Correct 12 ms 23732 KB n=100
13 Correct 13 ms 23840 KB n=100
14 Correct 13 ms 23764 KB n=100
15 Correct 13 ms 23764 KB n=100
16 Correct 13 ms 23820 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 13 ms 23764 KB n=100
19 Correct 12 ms 23764 KB n=100
20 Correct 13 ms 23764 KB n=100
21 Correct 13 ms 23800 KB n=100
22 Correct 13 ms 23764 KB n=100
23 Correct 13 ms 23776 KB n=100
24 Correct 13 ms 23764 KB n=100
25 Correct 12 ms 23764 KB n=100
26 Correct 12 ms 23728 KB n=12
27 Correct 13 ms 23840 KB n=100
28 Correct 15 ms 23872 KB n=500
29 Correct 16 ms 23892 KB n=500
30 Correct 13 ms 23916 KB n=500
31 Correct 14 ms 23916 KB n=500
32 Correct 13 ms 23892 KB n=500
33 Correct 13 ms 24020 KB n=500
34 Correct 13 ms 23892 KB n=500
35 Correct 13 ms 23892 KB n=500
36 Correct 13 ms 23892 KB n=500
37 Correct 13 ms 23892 KB n=500
38 Correct 16 ms 23820 KB n=500
39 Correct 14 ms 23892 KB n=500
40 Correct 13 ms 23892 KB n=500
41 Correct 13 ms 23928 KB n=500
42 Correct 14 ms 23972 KB n=500
43 Correct 13 ms 23892 KB n=500
44 Correct 13 ms 23892 KB n=500
45 Correct 13 ms 23892 KB n=500
46 Correct 14 ms 23944 KB n=500
47 Correct 13 ms 23932 KB n=500
48 Correct 13 ms 23896 KB n=500
49 Correct 13 ms 23944 KB n=500
50 Correct 13 ms 23892 KB n=500
51 Correct 15 ms 23892 KB n=500
52 Correct 13 ms 23964 KB n=500
53 Correct 16 ms 23892 KB n=500
54 Correct 14 ms 23892 KB n=500
55 Correct 16 ms 23812 KB n=278
56 Correct 13 ms 23892 KB n=500
57 Correct 15 ms 23916 KB n=500
58 Correct 16 ms 23932 KB n=500
59 Correct 15 ms 24276 KB n=2000
60 Correct 15 ms 24416 KB n=2000
61 Correct 18 ms 24340 KB n=2000
62 Correct 16 ms 24272 KB n=2000
63 Correct 16 ms 24276 KB n=2000
64 Correct 16 ms 24276 KB n=2000
65 Correct 16 ms 24276 KB n=2000
66 Correct 16 ms 24276 KB n=2000
67 Correct 16 ms 24272 KB n=2000
68 Correct 17 ms 24276 KB n=2000
69 Correct 15 ms 24228 KB n=2000
70 Correct 16 ms 24276 KB n=2000
71 Correct 15 ms 24276 KB n=2000
72 Correct 14 ms 24276 KB n=2000
73 Correct 15 ms 24276 KB n=2000
74 Correct 16 ms 24276 KB n=1844
75 Correct 14 ms 24208 KB n=2000
76 Correct 15 ms 24276 KB n=2000
77 Correct 16 ms 24276 KB n=2000
78 Correct 16 ms 24220 KB n=2000
79 Correct 19 ms 24280 KB n=2000
80 Correct 15 ms 24348 KB n=2000
81 Correct 16 ms 24312 KB n=2000
82 Correct 16 ms 24276 KB n=2000
83 Correct 15 ms 24304 KB n=2000
84 Correct 16 ms 24308 KB n=2000
85 Correct 16 ms 24240 KB n=2000
86 Correct 16 ms 24276 KB n=2000
87 Correct 16 ms 24260 KB n=2000
88 Correct 15 ms 24404 KB n=2000
89 Correct 16 ms 24404 KB n=2000
90 Correct 16 ms 24292 KB n=2000
91 Correct 15 ms 24276 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23764 KB n=5
2 Correct 14 ms 23800 KB n=100
3 Correct 13 ms 23764 KB n=100
4 Correct 13 ms 23836 KB n=100
5 Correct 13 ms 23756 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 14 ms 23916 KB n=100
8 Correct 12 ms 23796 KB n=100
9 Correct 13 ms 23796 KB n=100
10 Correct 12 ms 23732 KB n=100
11 Correct 13 ms 23764 KB n=100
12 Correct 12 ms 23732 KB n=100
13 Correct 13 ms 23840 KB n=100
14 Correct 13 ms 23764 KB n=100
15 Correct 13 ms 23764 KB n=100
16 Correct 13 ms 23820 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 13 ms 23764 KB n=100
19 Correct 12 ms 23764 KB n=100
20 Correct 13 ms 23764 KB n=100
21 Correct 13 ms 23800 KB n=100
22 Correct 13 ms 23764 KB n=100
23 Correct 13 ms 23776 KB n=100
24 Correct 13 ms 23764 KB n=100
25 Correct 12 ms 23764 KB n=100
26 Correct 12 ms 23728 KB n=12
27 Correct 13 ms 23840 KB n=100
28 Correct 15 ms 23872 KB n=500
29 Correct 16 ms 23892 KB n=500
30 Correct 13 ms 23916 KB n=500
31 Correct 14 ms 23916 KB n=500
32 Correct 13 ms 23892 KB n=500
33 Correct 13 ms 24020 KB n=500
34 Correct 13 ms 23892 KB n=500
35 Correct 13 ms 23892 KB n=500
36 Correct 13 ms 23892 KB n=500
37 Correct 13 ms 23892 KB n=500
38 Correct 16 ms 23820 KB n=500
39 Correct 14 ms 23892 KB n=500
40 Correct 13 ms 23892 KB n=500
41 Correct 13 ms 23928 KB n=500
42 Correct 14 ms 23972 KB n=500
43 Correct 13 ms 23892 KB n=500
44 Correct 13 ms 23892 KB n=500
45 Correct 13 ms 23892 KB n=500
46 Correct 14 ms 23944 KB n=500
47 Correct 13 ms 23932 KB n=500
48 Correct 13 ms 23896 KB n=500
49 Correct 13 ms 23944 KB n=500
50 Correct 13 ms 23892 KB n=500
51 Correct 15 ms 23892 KB n=500
52 Correct 13 ms 23964 KB n=500
53 Correct 16 ms 23892 KB n=500
54 Correct 14 ms 23892 KB n=500
55 Correct 16 ms 23812 KB n=278
56 Correct 13 ms 23892 KB n=500
57 Correct 15 ms 23916 KB n=500
58 Correct 16 ms 23932 KB n=500
59 Correct 15 ms 24276 KB n=2000
60 Correct 15 ms 24416 KB n=2000
61 Correct 18 ms 24340 KB n=2000
62 Correct 16 ms 24272 KB n=2000
63 Correct 16 ms 24276 KB n=2000
64 Correct 16 ms 24276 KB n=2000
65 Correct 16 ms 24276 KB n=2000
66 Correct 16 ms 24276 KB n=2000
67 Correct 16 ms 24272 KB n=2000
68 Correct 17 ms 24276 KB n=2000
69 Correct 15 ms 24228 KB n=2000
70 Correct 16 ms 24276 KB n=2000
71 Correct 15 ms 24276 KB n=2000
72 Correct 14 ms 24276 KB n=2000
73 Correct 15 ms 24276 KB n=2000
74 Correct 16 ms 24276 KB n=1844
75 Correct 14 ms 24208 KB n=2000
76 Correct 15 ms 24276 KB n=2000
77 Correct 16 ms 24276 KB n=2000
78 Correct 16 ms 24220 KB n=2000
79 Correct 19 ms 24280 KB n=2000
80 Correct 15 ms 24348 KB n=2000
81 Correct 16 ms 24312 KB n=2000
82 Correct 16 ms 24276 KB n=2000
83 Correct 15 ms 24304 KB n=2000
84 Correct 16 ms 24308 KB n=2000
85 Correct 16 ms 24240 KB n=2000
86 Correct 16 ms 24276 KB n=2000
87 Correct 16 ms 24260 KB n=2000
88 Correct 15 ms 24404 KB n=2000
89 Correct 16 ms 24404 KB n=2000
90 Correct 16 ms 24292 KB n=2000
91 Correct 15 ms 24276 KB n=2000
92 Correct 692 ms 71968 KB n=200000
93 Correct 948 ms 77220 KB n=200000
94 Correct 870 ms 89356 KB n=200000
95 Correct 677 ms 78216 KB n=200000
96 Correct 665 ms 78340 KB n=200000
97 Correct 968 ms 83744 KB n=200000
98 Correct 668 ms 78276 KB n=200000
99 Correct 830 ms 78496 KB n=200000
100 Correct 641 ms 78468 KB n=200000
101 Correct 943 ms 91068 KB n=200000
102 Correct 431 ms 79472 KB n=200000
103 Correct 396 ms 79496 KB n=200000
104 Correct 417 ms 79436 KB n=200000
105 Correct 423 ms 79852 KB n=200000
106 Correct 434 ms 79904 KB n=200000
107 Correct 400 ms 79912 KB n=200000
108 Correct 745 ms 78360 KB n=200000
109 Correct 803 ms 78284 KB n=200000
110 Correct 745 ms 78468 KB n=200000
111 Correct 645 ms 77772 KB n=200000
112 Correct 856 ms 89636 KB n=200000
113 Correct 946 ms 83548 KB n=200000
114 Correct 665 ms 77824 KB n=200000
115 Correct 972 ms 80940 KB n=200000
116 Correct 643 ms 78372 KB n=200000
117 Correct 911 ms 90356 KB n=200000
118 Correct 956 ms 82184 KB n=200000
119 Correct 620 ms 78648 KB n=200000
120 Correct 775 ms 90012 KB n=200000
121 Correct 797 ms 90052 KB n=200000
122 Correct 831 ms 90380 KB n=200000
123 Correct 411 ms 79720 KB n=200000
124 Correct 187 ms 39492 KB n=25264