Submission #713283

# Submission time Handle Problem Language Result Execution time Memory
713283 2023-03-21T15:49:20 Z amirhoseinfar1385 Birthday gift (IZhO18_treearray) C++17
56 / 100
3319 ms 262148 KB
#include<bits/stdc++.h>
using namespace std;
int n,m,q;
const int maxn=200000+10;
vector<int>adj[maxn];
int timea=0;
pair<int,int>stf[maxn];
int lca[19][maxn];
 
void dfs(int u,int par=0){
	timea++;
	stf[u].first=timea;
	lca[0][u]=par;
	for(auto x:adj[u]){
		if(x==par){
			continue;
		}
		dfs(x,u);
	}
	stf[u].second=timea;
}
 
void callca(){
	for(int i=1;i<19;i++){
		for(int j=1;j<=n;j++){
			lca[i][j]=lca[i-1][lca[i-1][j]];
		}
	}
}
 
bool zird(int u,int v){
	if(stf[v].first<=stf[u].first&&stf[v].second>=stf[u].second){
		return 1;
	}
	return 0;
}
 
int getlca(int u,int v){
	if(zird(u,v)){
		return v;
	}
	if(zird(v,u)){
		return u;
	}
	for(int i=18;i>=0;i--){
		if(lca[i][u]!=0&&!zird(v,lca[i][u])){
			u=lca[i][u];
		}
	}
	u=lca[0][u];
	return u;
}
int kaf=(1<<18);
struct segment{
	map<int,int>seg[(1<<19)];
	void upd(int i,int v,int wher){
		if(i==0){
			return ;
		}
		if((i<<1)>=(1<<19)){
			seg[i][v]=wher;
			return upd((i>>1),v,wher);
		}
		if(seg[(i<<1)].count(v)==0){
			seg[i][v]=seg[(i<<1)^1][v];
		}
		else if(seg[(i<<1)^1].count(v)==0){
			seg[i][v]=seg[(i<<1)][v];
		}
		else{
			seg[i][v]=min(seg[(i<<1)][v],seg[(i<<1)^1][v]);
		}
		return upd((i>>1),v,wher);
	}
	int pors(int i,int l,int r,int tl,int tr,int v){
		if(l>r||l>tr||r<tl||seg[i].count(v)==0){
			return m+2;
		}
		if(l>=tl&&r<=tr){
			return seg[i][v];
		}
		int ma=(l+r)>>1;
		int ret=pors((i<<1),l,ma,tl,tr,v);
		if(ret==m+2){
			ret=pors((i<<1)^1,ma+1,r,tl,tr,v);
		}
		return ret;
	}
}seg;
 
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>q;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs(1);
	callca();
	vector<int>allm(m);
	for(int i=0;i<m;i++){
		cin>>allm[i];
	}
	for(int i=0;i<m;i++){
		seg.upd(kaf+i,allm[i],i);
		if(i!=m-1){
			seg.upd(kaf+i,getlca(allm[i],allm[i+1]),i);
		}
	}
	for(int i=0;i<q;i++){
		int qq;
		cin>>qq;
		if(qq==1){
			int ind,v;
			cin>>ind>>v;
			ind--;
			seg.upd(kaf+ind,allm[ind],m+2);
			if(ind!=m-1){
				seg.upd(kaf+ind,getlca(allm[ind],allm[ind+1]),m+2);
			}
			if(ind!=0){
				seg.upd(kaf+ind-1,getlca(allm[ind],allm[ind-1]),m+2);
			}
			allm[ind]=v;
			seg.upd(kaf+ind,allm[ind],ind);
			if(ind!=m-1){
				seg.upd(kaf+ind,getlca(allm[ind],allm[ind+1]),ind);
			}
			if(ind!=0){
				seg.upd(kaf+ind-1,getlca(allm[ind],allm[ind-1]),ind-1);
				seg.upd(kaf+ind-1,allm[ind-1],ind-1);
			}
		}
		else{
			int l,r,v;
			cin>>l>>r>>v;
			l--;
			r--;
			if(allm[r]==v){
				cout<<r+1<<" "<<r+1<<"\n";
				continue;
			}
			int w=seg.pors(1,0,kaf-1,l,r-1,v);
			if(w==m+2){
				cout<<-1<<" "<<-1<<"\n";
				continue;
			}
			if(allm[w]==v){
				cout<<w+1<<" "<<w+1<<"\n";
			}
			else{
				cout<<w+1<<" "<<w+1+1<<"\n";
			}
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 29652 KB n=5
2 Correct 20 ms 29864 KB n=100
3 Correct 17 ms 29784 KB n=100
4 Correct 16 ms 29876 KB n=100
5 Correct 15 ms 29804 KB n=100
6 Correct 15 ms 29776 KB n=100
7 Correct 15 ms 29764 KB n=100
8 Correct 15 ms 29780 KB n=100
9 Correct 15 ms 29780 KB n=100
10 Correct 17 ms 29780 KB n=100
11 Correct 20 ms 29808 KB n=100
12 Correct 16 ms 29780 KB n=100
13 Correct 16 ms 29780 KB n=100
14 Correct 16 ms 29764 KB n=100
15 Correct 15 ms 29780 KB n=100
16 Correct 16 ms 29864 KB n=100
17 Correct 16 ms 29780 KB n=100
18 Correct 17 ms 29780 KB n=100
19 Correct 16 ms 29848 KB n=100
20 Correct 16 ms 29780 KB n=100
21 Correct 16 ms 29864 KB n=100
22 Correct 16 ms 29868 KB n=100
23 Correct 19 ms 29820 KB n=100
24 Correct 16 ms 29780 KB n=100
25 Correct 20 ms 29824 KB n=100
26 Correct 16 ms 29720 KB n=12
27 Correct 16 ms 29768 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 15 ms 29652 KB n=5
2 Correct 20 ms 29864 KB n=100
3 Correct 17 ms 29784 KB n=100
4 Correct 16 ms 29876 KB n=100
5 Correct 15 ms 29804 KB n=100
6 Correct 15 ms 29776 KB n=100
7 Correct 15 ms 29764 KB n=100
8 Correct 15 ms 29780 KB n=100
9 Correct 15 ms 29780 KB n=100
10 Correct 17 ms 29780 KB n=100
11 Correct 20 ms 29808 KB n=100
12 Correct 16 ms 29780 KB n=100
13 Correct 16 ms 29780 KB n=100
14 Correct 16 ms 29764 KB n=100
15 Correct 15 ms 29780 KB n=100
16 Correct 16 ms 29864 KB n=100
17 Correct 16 ms 29780 KB n=100
18 Correct 17 ms 29780 KB n=100
19 Correct 16 ms 29848 KB n=100
20 Correct 16 ms 29780 KB n=100
21 Correct 16 ms 29864 KB n=100
22 Correct 16 ms 29868 KB n=100
23 Correct 19 ms 29820 KB n=100
24 Correct 16 ms 29780 KB n=100
25 Correct 20 ms 29824 KB n=100
26 Correct 16 ms 29720 KB n=12
27 Correct 16 ms 29768 KB n=100
28 Correct 20 ms 30292 KB n=500
29 Correct 23 ms 30316 KB n=500
30 Correct 21 ms 30420 KB n=500
31 Correct 23 ms 30292 KB n=500
32 Correct 20 ms 30292 KB n=500
33 Correct 27 ms 30328 KB n=500
34 Correct 21 ms 30228 KB n=500
35 Correct 22 ms 30312 KB n=500
36 Correct 17 ms 30120 KB n=500
37 Correct 17 ms 30164 KB n=500
38 Correct 17 ms 30168 KB n=500
39 Correct 17 ms 30164 KB n=500
40 Correct 19 ms 30228 KB n=500
41 Correct 22 ms 30208 KB n=500
42 Correct 20 ms 30320 KB n=500
43 Correct 20 ms 30360 KB n=500
44 Correct 20 ms 30292 KB n=500
45 Correct 22 ms 30164 KB n=500
46 Correct 21 ms 30328 KB n=500
47 Correct 27 ms 30304 KB n=500
48 Correct 19 ms 30164 KB n=500
49 Correct 22 ms 30292 KB n=500
50 Correct 20 ms 30272 KB n=500
51 Correct 21 ms 30420 KB n=500
52 Correct 20 ms 30344 KB n=500
53 Correct 24 ms 30392 KB n=500
54 Correct 23 ms 30236 KB n=500
55 Correct 17 ms 30164 KB n=278
56 Correct 20 ms 30312 KB n=500
57 Correct 20 ms 30236 KB n=500
58 Correct 17 ms 30164 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 15 ms 29652 KB n=5
2 Correct 20 ms 29864 KB n=100
3 Correct 17 ms 29784 KB n=100
4 Correct 16 ms 29876 KB n=100
5 Correct 15 ms 29804 KB n=100
6 Correct 15 ms 29776 KB n=100
7 Correct 15 ms 29764 KB n=100
8 Correct 15 ms 29780 KB n=100
9 Correct 15 ms 29780 KB n=100
10 Correct 17 ms 29780 KB n=100
11 Correct 20 ms 29808 KB n=100
12 Correct 16 ms 29780 KB n=100
13 Correct 16 ms 29780 KB n=100
14 Correct 16 ms 29764 KB n=100
15 Correct 15 ms 29780 KB n=100
16 Correct 16 ms 29864 KB n=100
17 Correct 16 ms 29780 KB n=100
18 Correct 17 ms 29780 KB n=100
19 Correct 16 ms 29848 KB n=100
20 Correct 16 ms 29780 KB n=100
21 Correct 16 ms 29864 KB n=100
22 Correct 16 ms 29868 KB n=100
23 Correct 19 ms 29820 KB n=100
24 Correct 16 ms 29780 KB n=100
25 Correct 20 ms 29824 KB n=100
26 Correct 16 ms 29720 KB n=12
27 Correct 16 ms 29768 KB n=100
28 Correct 20 ms 30292 KB n=500
29 Correct 23 ms 30316 KB n=500
30 Correct 21 ms 30420 KB n=500
31 Correct 23 ms 30292 KB n=500
32 Correct 20 ms 30292 KB n=500
33 Correct 27 ms 30328 KB n=500
34 Correct 21 ms 30228 KB n=500
35 Correct 22 ms 30312 KB n=500
36 Correct 17 ms 30120 KB n=500
37 Correct 17 ms 30164 KB n=500
38 Correct 17 ms 30168 KB n=500
39 Correct 17 ms 30164 KB n=500
40 Correct 19 ms 30228 KB n=500
41 Correct 22 ms 30208 KB n=500
42 Correct 20 ms 30320 KB n=500
43 Correct 20 ms 30360 KB n=500
44 Correct 20 ms 30292 KB n=500
45 Correct 22 ms 30164 KB n=500
46 Correct 21 ms 30328 KB n=500
47 Correct 27 ms 30304 KB n=500
48 Correct 19 ms 30164 KB n=500
49 Correct 22 ms 30292 KB n=500
50 Correct 20 ms 30272 KB n=500
51 Correct 21 ms 30420 KB n=500
52 Correct 20 ms 30344 KB n=500
53 Correct 24 ms 30392 KB n=500
54 Correct 23 ms 30236 KB n=500
55 Correct 17 ms 30164 KB n=278
56 Correct 20 ms 30312 KB n=500
57 Correct 20 ms 30236 KB n=500
58 Correct 17 ms 30164 KB n=500
59 Correct 39 ms 32140 KB n=2000
60 Correct 53 ms 32264 KB n=2000
61 Correct 61 ms 32460 KB n=2000
62 Correct 60 ms 32656 KB n=2000
63 Correct 43 ms 32140 KB n=2000
64 Correct 50 ms 32592 KB n=2000
65 Correct 46 ms 32192 KB n=2000
66 Correct 48 ms 32344 KB n=2000
67 Correct 44 ms 32404 KB n=2000
68 Correct 60 ms 32556 KB n=2000
69 Correct 26 ms 31600 KB n=2000
70 Correct 30 ms 31540 KB n=2000
71 Correct 26 ms 31624 KB n=2000
72 Correct 25 ms 31568 KB n=2000
73 Correct 25 ms 31600 KB n=2000
74 Correct 41 ms 32052 KB n=1844
75 Correct 29 ms 31544 KB n=2000
76 Correct 47 ms 32356 KB n=2000
77 Correct 45 ms 32368 KB n=2000
78 Correct 42 ms 32376 KB n=2000
79 Correct 43 ms 31812 KB n=2000
80 Correct 49 ms 32332 KB n=2000
81 Correct 53 ms 32464 KB n=2000
82 Correct 36 ms 31756 KB n=2000
83 Correct 49 ms 32368 KB n=2000
84 Correct 41 ms 32188 KB n=2000
85 Correct 50 ms 32588 KB n=2000
86 Correct 55 ms 32620 KB n=2000
87 Correct 44 ms 32188 KB n=2000
88 Correct 49 ms 32272 KB n=2000
89 Correct 56 ms 32240 KB n=2000
90 Correct 57 ms 32184 KB n=2000
91 Correct 30 ms 31708 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 15 ms 29652 KB n=5
2 Correct 20 ms 29864 KB n=100
3 Correct 17 ms 29784 KB n=100
4 Correct 16 ms 29876 KB n=100
5 Correct 15 ms 29804 KB n=100
6 Correct 15 ms 29776 KB n=100
7 Correct 15 ms 29764 KB n=100
8 Correct 15 ms 29780 KB n=100
9 Correct 15 ms 29780 KB n=100
10 Correct 17 ms 29780 KB n=100
11 Correct 20 ms 29808 KB n=100
12 Correct 16 ms 29780 KB n=100
13 Correct 16 ms 29780 KB n=100
14 Correct 16 ms 29764 KB n=100
15 Correct 15 ms 29780 KB n=100
16 Correct 16 ms 29864 KB n=100
17 Correct 16 ms 29780 KB n=100
18 Correct 17 ms 29780 KB n=100
19 Correct 16 ms 29848 KB n=100
20 Correct 16 ms 29780 KB n=100
21 Correct 16 ms 29864 KB n=100
22 Correct 16 ms 29868 KB n=100
23 Correct 19 ms 29820 KB n=100
24 Correct 16 ms 29780 KB n=100
25 Correct 20 ms 29824 KB n=100
26 Correct 16 ms 29720 KB n=12
27 Correct 16 ms 29768 KB n=100
28 Correct 20 ms 30292 KB n=500
29 Correct 23 ms 30316 KB n=500
30 Correct 21 ms 30420 KB n=500
31 Correct 23 ms 30292 KB n=500
32 Correct 20 ms 30292 KB n=500
33 Correct 27 ms 30328 KB n=500
34 Correct 21 ms 30228 KB n=500
35 Correct 22 ms 30312 KB n=500
36 Correct 17 ms 30120 KB n=500
37 Correct 17 ms 30164 KB n=500
38 Correct 17 ms 30168 KB n=500
39 Correct 17 ms 30164 KB n=500
40 Correct 19 ms 30228 KB n=500
41 Correct 22 ms 30208 KB n=500
42 Correct 20 ms 30320 KB n=500
43 Correct 20 ms 30360 KB n=500
44 Correct 20 ms 30292 KB n=500
45 Correct 22 ms 30164 KB n=500
46 Correct 21 ms 30328 KB n=500
47 Correct 27 ms 30304 KB n=500
48 Correct 19 ms 30164 KB n=500
49 Correct 22 ms 30292 KB n=500
50 Correct 20 ms 30272 KB n=500
51 Correct 21 ms 30420 KB n=500
52 Correct 20 ms 30344 KB n=500
53 Correct 24 ms 30392 KB n=500
54 Correct 23 ms 30236 KB n=500
55 Correct 17 ms 30164 KB n=278
56 Correct 20 ms 30312 KB n=500
57 Correct 20 ms 30236 KB n=500
58 Correct 17 ms 30164 KB n=500
59 Correct 39 ms 32140 KB n=2000
60 Correct 53 ms 32264 KB n=2000
61 Correct 61 ms 32460 KB n=2000
62 Correct 60 ms 32656 KB n=2000
63 Correct 43 ms 32140 KB n=2000
64 Correct 50 ms 32592 KB n=2000
65 Correct 46 ms 32192 KB n=2000
66 Correct 48 ms 32344 KB n=2000
67 Correct 44 ms 32404 KB n=2000
68 Correct 60 ms 32556 KB n=2000
69 Correct 26 ms 31600 KB n=2000
70 Correct 30 ms 31540 KB n=2000
71 Correct 26 ms 31624 KB n=2000
72 Correct 25 ms 31568 KB n=2000
73 Correct 25 ms 31600 KB n=2000
74 Correct 41 ms 32052 KB n=1844
75 Correct 29 ms 31544 KB n=2000
76 Correct 47 ms 32356 KB n=2000
77 Correct 45 ms 32368 KB n=2000
78 Correct 42 ms 32376 KB n=2000
79 Correct 43 ms 31812 KB n=2000
80 Correct 49 ms 32332 KB n=2000
81 Correct 53 ms 32464 KB n=2000
82 Correct 36 ms 31756 KB n=2000
83 Correct 49 ms 32368 KB n=2000
84 Correct 41 ms 32188 KB n=2000
85 Correct 50 ms 32588 KB n=2000
86 Correct 55 ms 32620 KB n=2000
87 Correct 44 ms 32188 KB n=2000
88 Correct 49 ms 32272 KB n=2000
89 Correct 56 ms 32240 KB n=2000
90 Correct 57 ms 32184 KB n=2000
91 Correct 30 ms 31708 KB n=2000
92 Runtime error 3319 ms 262148 KB Execution killed with signal 9
93 Halted 0 ms 0 KB -