Submission #342295

# Submission time Handle Problem Language Result Execution time Memory
342295 2021-01-01T18:47:22 Z Gurban Birthday gift (IZhO18_treearray) C++17
100 / 100
1133 ms 83692 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int maxn=2e5+5;
int n,m,q,sp[maxn][21],lg;
int x,y,a[maxn],L[maxn],tp;
int l[maxn];
vector<int>E[maxn];
set<int>s[maxn];
set<int>st[maxn];

void dfs(int nd,int pr){
	L[nd] = L[pr] + 1;
	sp[nd][0] = pr;
	for(auto i : E[nd]) if(i != pr) dfs(i,nd);
}

int lca(int a,int b){
	if(L[a] < L[b]) swap(a,b);
	for(int i = lg;i >= 0;i--)
		if(L[a] - (1<<i) >= L[b]) a = sp[a][i];
	if(a==b) return a;
	for(int i = lg;i >= 0;i--)
		if(sp[a][i] and sp[a][i] != sp[b][i]) a=sp[a][i],b=sp[b][i];
	return sp[a][0];
}

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

	cin >> n >> m >> q;
	for(int i = 1;i < n;i++){
		cin >> x >> y;
		E[x].push_back(y);
		E[y].push_back(x);
	}
	dfs(1,0);
	lg = __lg(n);
	for(int i = 1;i <= lg;i++) for(int j = 1;j <= n;j++) sp[j][i] = sp[sp[j][i-1]][i-1];
	for(int i = 1;i <= m;i++){
		cin >> a[i];
		if(i == 1) continue;
		int lc = lca(a[i],a[i-1]);
		l[i] = lc;
		s[lc].insert(i);
		st[a[i]].insert(i);
	}
	// for(int i = 1;i <= m;i++) cout<<l[i]<<' '; cout<<'\n';
	while(q--){
		cin >> tp;
		if(tp == 1){
			int pos,val;
			cin >> pos >> val;
			if(pos > 1){
				s[l[pos]].erase(pos);
				int lc = lca(a[pos-1],val);
				l[pos] = lc;
				s[lc].insert(pos);
			}
			if(pos < m){
				s[l[pos+1]].erase(pos + 1);
				int lc = lca(a[pos+1],val);
				l[pos + 1] = lc;
				s[lc].insert(pos + 1);
			}
			st[a[pos]].erase(pos);
			st[val].insert(pos);
			a[pos] = val;
		}
		else {
			int l,r,val;
			cin >> l >> r >> val;
			auto p = st[val].lower_bound(l);
			if(p != st[val].end() and *p <= r){cout<<*p<<' '<<*p<<'\n';continue;}
			auto t = s[val].upper_bound(l);
			if(t != s[val].end() and *t <= r){cout<<*t-1<<' '<<*t<<'\n';continue;}
			cout<<"-1 -1\n";
		}
	}
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5 
2 3 4 5
2 1 3 1
*/
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23788 KB n=5
2 Correct 15 ms 23916 KB n=100
3 Correct 15 ms 23916 KB n=100
4 Correct 15 ms 23916 KB n=100
5 Correct 15 ms 23936 KB n=100
6 Correct 15 ms 23916 KB n=100
7 Correct 14 ms 23916 KB n=100
8 Correct 15 ms 23916 KB n=100
9 Correct 15 ms 23916 KB n=100
10 Correct 16 ms 23936 KB n=100
11 Correct 15 ms 23916 KB n=100
12 Correct 14 ms 23916 KB n=100
13 Correct 14 ms 23916 KB n=100
14 Correct 15 ms 23916 KB n=100
15 Correct 15 ms 23916 KB n=100
16 Correct 14 ms 23916 KB n=100
17 Correct 14 ms 23916 KB n=100
18 Correct 15 ms 23916 KB n=100
19 Correct 15 ms 23916 KB n=100
20 Correct 14 ms 23916 KB n=100
21 Correct 15 ms 23916 KB n=100
22 Correct 15 ms 23916 KB n=100
23 Correct 15 ms 23936 KB n=100
24 Correct 14 ms 23916 KB n=100
25 Correct 15 ms 23916 KB n=100
26 Correct 15 ms 23788 KB n=12
27 Correct 14 ms 23916 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23788 KB n=5
2 Correct 15 ms 23916 KB n=100
3 Correct 15 ms 23916 KB n=100
4 Correct 15 ms 23916 KB n=100
5 Correct 15 ms 23936 KB n=100
6 Correct 15 ms 23916 KB n=100
7 Correct 14 ms 23916 KB n=100
8 Correct 15 ms 23916 KB n=100
9 Correct 15 ms 23916 KB n=100
10 Correct 16 ms 23936 KB n=100
11 Correct 15 ms 23916 KB n=100
12 Correct 14 ms 23916 KB n=100
13 Correct 14 ms 23916 KB n=100
14 Correct 15 ms 23916 KB n=100
15 Correct 15 ms 23916 KB n=100
16 Correct 14 ms 23916 KB n=100
17 Correct 14 ms 23916 KB n=100
18 Correct 15 ms 23916 KB n=100
19 Correct 15 ms 23916 KB n=100
20 Correct 14 ms 23916 KB n=100
21 Correct 15 ms 23916 KB n=100
22 Correct 15 ms 23916 KB n=100
23 Correct 15 ms 23936 KB n=100
24 Correct 14 ms 23916 KB n=100
25 Correct 15 ms 23916 KB n=100
26 Correct 15 ms 23788 KB n=12
27 Correct 14 ms 23916 KB n=100
28 Correct 15 ms 23936 KB n=500
29 Correct 15 ms 24044 KB n=500
30 Correct 16 ms 23916 KB n=500
31 Correct 15 ms 24044 KB n=500
32 Correct 15 ms 23916 KB n=500
33 Correct 15 ms 23916 KB n=500
34 Correct 15 ms 23916 KB n=500
35 Correct 16 ms 24044 KB n=500
36 Correct 15 ms 23916 KB n=500
37 Correct 15 ms 23936 KB n=500
38 Correct 15 ms 23916 KB n=500
39 Correct 15 ms 23916 KB n=500
40 Correct 14 ms 23916 KB n=500
41 Correct 15 ms 23916 KB n=500
42 Correct 15 ms 23916 KB n=500
43 Correct 15 ms 23916 KB n=500
44 Correct 15 ms 23916 KB n=500
45 Correct 15 ms 23916 KB n=500
46 Correct 15 ms 24044 KB n=500
47 Correct 15 ms 24044 KB n=500
48 Correct 15 ms 24044 KB n=500
49 Correct 16 ms 24044 KB n=500
50 Correct 16 ms 23916 KB n=500
51 Correct 15 ms 23916 KB n=500
52 Correct 15 ms 24044 KB n=500
53 Correct 15 ms 23916 KB n=500
54 Correct 15 ms 24044 KB n=500
55 Correct 15 ms 23916 KB n=278
56 Correct 15 ms 24044 KB n=500
57 Correct 15 ms 24044 KB n=500
58 Correct 15 ms 23916 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23788 KB n=5
2 Correct 15 ms 23916 KB n=100
3 Correct 15 ms 23916 KB n=100
4 Correct 15 ms 23916 KB n=100
5 Correct 15 ms 23936 KB n=100
6 Correct 15 ms 23916 KB n=100
7 Correct 14 ms 23916 KB n=100
8 Correct 15 ms 23916 KB n=100
9 Correct 15 ms 23916 KB n=100
10 Correct 16 ms 23936 KB n=100
11 Correct 15 ms 23916 KB n=100
12 Correct 14 ms 23916 KB n=100
13 Correct 14 ms 23916 KB n=100
14 Correct 15 ms 23916 KB n=100
15 Correct 15 ms 23916 KB n=100
16 Correct 14 ms 23916 KB n=100
17 Correct 14 ms 23916 KB n=100
18 Correct 15 ms 23916 KB n=100
19 Correct 15 ms 23916 KB n=100
20 Correct 14 ms 23916 KB n=100
21 Correct 15 ms 23916 KB n=100
22 Correct 15 ms 23916 KB n=100
23 Correct 15 ms 23936 KB n=100
24 Correct 14 ms 23916 KB n=100
25 Correct 15 ms 23916 KB n=100
26 Correct 15 ms 23788 KB n=12
27 Correct 14 ms 23916 KB n=100
28 Correct 15 ms 23936 KB n=500
29 Correct 15 ms 24044 KB n=500
30 Correct 16 ms 23916 KB n=500
31 Correct 15 ms 24044 KB n=500
32 Correct 15 ms 23916 KB n=500
33 Correct 15 ms 23916 KB n=500
34 Correct 15 ms 23916 KB n=500
35 Correct 16 ms 24044 KB n=500
36 Correct 15 ms 23916 KB n=500
37 Correct 15 ms 23936 KB n=500
38 Correct 15 ms 23916 KB n=500
39 Correct 15 ms 23916 KB n=500
40 Correct 14 ms 23916 KB n=500
41 Correct 15 ms 23916 KB n=500
42 Correct 15 ms 23916 KB n=500
43 Correct 15 ms 23916 KB n=500
44 Correct 15 ms 23916 KB n=500
45 Correct 15 ms 23916 KB n=500
46 Correct 15 ms 24044 KB n=500
47 Correct 15 ms 24044 KB n=500
48 Correct 15 ms 24044 KB n=500
49 Correct 16 ms 24044 KB n=500
50 Correct 16 ms 23916 KB n=500
51 Correct 15 ms 23916 KB n=500
52 Correct 15 ms 24044 KB n=500
53 Correct 15 ms 23916 KB n=500
54 Correct 15 ms 24044 KB n=500
55 Correct 15 ms 23916 KB n=278
56 Correct 15 ms 24044 KB n=500
57 Correct 15 ms 24044 KB n=500
58 Correct 15 ms 23916 KB n=500
59 Correct 17 ms 24300 KB n=2000
60 Correct 18 ms 24428 KB n=2000
61 Correct 19 ms 24428 KB n=2000
62 Correct 19 ms 24428 KB n=2000
63 Correct 18 ms 24376 KB n=2000
64 Correct 19 ms 24448 KB n=2000
65 Correct 18 ms 24300 KB n=2000
66 Correct 18 ms 24428 KB n=2000
67 Correct 18 ms 24300 KB n=2000
68 Correct 17 ms 24428 KB n=2000
69 Correct 18 ms 24428 KB n=2000
70 Correct 17 ms 24300 KB n=2000
71 Correct 18 ms 24300 KB n=2000
72 Correct 17 ms 24300 KB n=2000
73 Correct 17 ms 24320 KB n=2000
74 Correct 18 ms 24300 KB n=1844
75 Correct 18 ms 24300 KB n=2000
76 Correct 18 ms 24300 KB n=2000
77 Correct 18 ms 24300 KB n=2000
78 Correct 18 ms 24300 KB n=2000
79 Correct 18 ms 24300 KB n=2000
80 Correct 18 ms 24448 KB n=2000
81 Correct 18 ms 24428 KB n=2000
82 Correct 17 ms 24300 KB n=2000
83 Correct 18 ms 24428 KB n=2000
84 Correct 17 ms 24300 KB n=2000
85 Correct 19 ms 24428 KB n=2000
86 Correct 17 ms 24428 KB n=2000
87 Correct 18 ms 24300 KB n=2000
88 Correct 18 ms 24428 KB n=2000
89 Correct 19 ms 24556 KB n=2000
90 Correct 18 ms 24428 KB n=2000
91 Correct 18 ms 24428 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23788 KB n=5
2 Correct 15 ms 23916 KB n=100
3 Correct 15 ms 23916 KB n=100
4 Correct 15 ms 23916 KB n=100
5 Correct 15 ms 23936 KB n=100
6 Correct 15 ms 23916 KB n=100
7 Correct 14 ms 23916 KB n=100
8 Correct 15 ms 23916 KB n=100
9 Correct 15 ms 23916 KB n=100
10 Correct 16 ms 23936 KB n=100
11 Correct 15 ms 23916 KB n=100
12 Correct 14 ms 23916 KB n=100
13 Correct 14 ms 23916 KB n=100
14 Correct 15 ms 23916 KB n=100
15 Correct 15 ms 23916 KB n=100
16 Correct 14 ms 23916 KB n=100
17 Correct 14 ms 23916 KB n=100
18 Correct 15 ms 23916 KB n=100
19 Correct 15 ms 23916 KB n=100
20 Correct 14 ms 23916 KB n=100
21 Correct 15 ms 23916 KB n=100
22 Correct 15 ms 23916 KB n=100
23 Correct 15 ms 23936 KB n=100
24 Correct 14 ms 23916 KB n=100
25 Correct 15 ms 23916 KB n=100
26 Correct 15 ms 23788 KB n=12
27 Correct 14 ms 23916 KB n=100
28 Correct 15 ms 23936 KB n=500
29 Correct 15 ms 24044 KB n=500
30 Correct 16 ms 23916 KB n=500
31 Correct 15 ms 24044 KB n=500
32 Correct 15 ms 23916 KB n=500
33 Correct 15 ms 23916 KB n=500
34 Correct 15 ms 23916 KB n=500
35 Correct 16 ms 24044 KB n=500
36 Correct 15 ms 23916 KB n=500
37 Correct 15 ms 23936 KB n=500
38 Correct 15 ms 23916 KB n=500
39 Correct 15 ms 23916 KB n=500
40 Correct 14 ms 23916 KB n=500
41 Correct 15 ms 23916 KB n=500
42 Correct 15 ms 23916 KB n=500
43 Correct 15 ms 23916 KB n=500
44 Correct 15 ms 23916 KB n=500
45 Correct 15 ms 23916 KB n=500
46 Correct 15 ms 24044 KB n=500
47 Correct 15 ms 24044 KB n=500
48 Correct 15 ms 24044 KB n=500
49 Correct 16 ms 24044 KB n=500
50 Correct 16 ms 23916 KB n=500
51 Correct 15 ms 23916 KB n=500
52 Correct 15 ms 24044 KB n=500
53 Correct 15 ms 23916 KB n=500
54 Correct 15 ms 24044 KB n=500
55 Correct 15 ms 23916 KB n=278
56 Correct 15 ms 24044 KB n=500
57 Correct 15 ms 24044 KB n=500
58 Correct 15 ms 23916 KB n=500
59 Correct 17 ms 24300 KB n=2000
60 Correct 18 ms 24428 KB n=2000
61 Correct 19 ms 24428 KB n=2000
62 Correct 19 ms 24428 KB n=2000
63 Correct 18 ms 24376 KB n=2000
64 Correct 19 ms 24448 KB n=2000
65 Correct 18 ms 24300 KB n=2000
66 Correct 18 ms 24428 KB n=2000
67 Correct 18 ms 24300 KB n=2000
68 Correct 17 ms 24428 KB n=2000
69 Correct 18 ms 24428 KB n=2000
70 Correct 17 ms 24300 KB n=2000
71 Correct 18 ms 24300 KB n=2000
72 Correct 17 ms 24300 KB n=2000
73 Correct 17 ms 24320 KB n=2000
74 Correct 18 ms 24300 KB n=1844
75 Correct 18 ms 24300 KB n=2000
76 Correct 18 ms 24300 KB n=2000
77 Correct 18 ms 24300 KB n=2000
78 Correct 18 ms 24300 KB n=2000
79 Correct 18 ms 24300 KB n=2000
80 Correct 18 ms 24448 KB n=2000
81 Correct 18 ms 24428 KB n=2000
82 Correct 17 ms 24300 KB n=2000
83 Correct 18 ms 24428 KB n=2000
84 Correct 17 ms 24300 KB n=2000
85 Correct 19 ms 24428 KB n=2000
86 Correct 17 ms 24428 KB n=2000
87 Correct 18 ms 24300 KB n=2000
88 Correct 18 ms 24428 KB n=2000
89 Correct 19 ms 24556 KB n=2000
90 Correct 18 ms 24428 KB n=2000
91 Correct 18 ms 24428 KB n=2000
92 Correct 796 ms 75244 KB n=200000
93 Correct 1006 ms 79056 KB n=200000
94 Correct 962 ms 82284 KB n=200000
95 Correct 795 ms 75104 KB n=200000
96 Correct 742 ms 75104 KB n=200000
97 Correct 1025 ms 77932 KB n=200000
98 Correct 767 ms 75232 KB n=200000
99 Correct 940 ms 74476 KB n=200000
100 Correct 766 ms 75236 KB n=200000
101 Correct 954 ms 83436 KB n=200000
102 Correct 497 ms 76012 KB n=200000
103 Correct 504 ms 76012 KB n=200000
104 Correct 498 ms 76140 KB n=200000
105 Correct 505 ms 75756 KB n=200000
106 Correct 501 ms 75756 KB n=200000
107 Correct 505 ms 75628 KB n=200000
108 Correct 873 ms 74860 KB n=200000
109 Correct 871 ms 74732 KB n=200000
110 Correct 887 ms 74732 KB n=200000
111 Correct 805 ms 75168 KB n=200000
112 Correct 954 ms 82412 KB n=200000
113 Correct 1028 ms 77932 KB n=200000
114 Correct 819 ms 75180 KB n=200000
115 Correct 1133 ms 76140 KB n=200000
116 Correct 747 ms 74848 KB n=200000
117 Correct 957 ms 82912 KB n=200000
118 Correct 1067 ms 77036 KB n=200000
119 Correct 749 ms 74836 KB n=200000
120 Correct 915 ms 83436 KB n=200000
121 Correct 918 ms 83308 KB n=200000
122 Correct 928 ms 83692 KB n=200000
123 Correct 517 ms 75500 KB n=200000
124 Correct 269 ms 39276 KB n=25264