Submission #713284

# Submission time Handle Problem Language Result Execution time Memory
713284 2023-03-21T15:52:41 Z amirhoseinfar1385 Birthday gift (IZhO18_treearray) C++17
56 / 100
4000 ms 250352 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)){
			if(wher==m+2){
				seg[i].erase(v);
			}
			else{
				seg[i][v]=wher;
			}
			return upd((i>>1),v,wher);
		}
		if(seg[(i<<1)].count(v)==0&&seg[(i<<1)^1].count(v)==0){
			seg[i].erase(v);
		}
		else 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 18 ms 29732 KB n=100
3 Correct 20 ms 29848 KB n=100
4 Correct 17 ms 29780 KB n=100
5 Correct 16 ms 29800 KB n=100
6 Correct 15 ms 29780 KB n=100
7 Correct 15 ms 29832 KB n=100
8 Correct 15 ms 29768 KB n=100
9 Correct 15 ms 29780 KB n=100
10 Correct 20 ms 29772 KB n=100
11 Correct 18 ms 29812 KB n=100
12 Correct 16 ms 29824 KB n=100
13 Correct 16 ms 29848 KB n=100
14 Correct 16 ms 29740 KB n=100
15 Correct 16 ms 29780 KB n=100
16 Correct 17 ms 29756 KB n=100
17 Correct 18 ms 29812 KB n=100
18 Correct 15 ms 29800 KB n=100
19 Correct 16 ms 29780 KB n=100
20 Correct 15 ms 29780 KB n=100
21 Correct 17 ms 29928 KB n=100
22 Correct 16 ms 29780 KB n=100
23 Correct 17 ms 29780 KB n=100
24 Correct 20 ms 29780 KB n=100
25 Correct 16 ms 29780 KB n=100
26 Correct 17 ms 29736 KB n=12
27 Correct 17 ms 29856 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 15 ms 29652 KB n=5
2 Correct 18 ms 29732 KB n=100
3 Correct 20 ms 29848 KB n=100
4 Correct 17 ms 29780 KB n=100
5 Correct 16 ms 29800 KB n=100
6 Correct 15 ms 29780 KB n=100
7 Correct 15 ms 29832 KB n=100
8 Correct 15 ms 29768 KB n=100
9 Correct 15 ms 29780 KB n=100
10 Correct 20 ms 29772 KB n=100
11 Correct 18 ms 29812 KB n=100
12 Correct 16 ms 29824 KB n=100
13 Correct 16 ms 29848 KB n=100
14 Correct 16 ms 29740 KB n=100
15 Correct 16 ms 29780 KB n=100
16 Correct 17 ms 29756 KB n=100
17 Correct 18 ms 29812 KB n=100
18 Correct 15 ms 29800 KB n=100
19 Correct 16 ms 29780 KB n=100
20 Correct 15 ms 29780 KB n=100
21 Correct 17 ms 29928 KB n=100
22 Correct 16 ms 29780 KB n=100
23 Correct 17 ms 29780 KB n=100
24 Correct 20 ms 29780 KB n=100
25 Correct 16 ms 29780 KB n=100
26 Correct 17 ms 29736 KB n=12
27 Correct 17 ms 29856 KB n=100
28 Correct 24 ms 30172 KB n=500
29 Correct 22 ms 30164 KB n=500
30 Correct 24 ms 30252 KB n=500
31 Correct 23 ms 30160 KB n=500
32 Correct 20 ms 30124 KB n=500
33 Correct 22 ms 30168 KB n=500
34 Correct 20 ms 30164 KB n=500
35 Correct 23 ms 30188 KB n=500
36 Correct 20 ms 30200 KB n=500
37 Correct 17 ms 30140 KB n=500
38 Correct 17 ms 30164 KB n=500
39 Correct 17 ms 30132 KB n=500
40 Correct 18 ms 30132 KB n=500
41 Correct 17 ms 30188 KB n=500
42 Correct 22 ms 30164 KB n=500
43 Correct 21 ms 30164 KB n=500
44 Correct 26 ms 30296 KB n=500
45 Correct 25 ms 30228 KB n=500
46 Correct 21 ms 30164 KB n=500
47 Correct 21 ms 30164 KB n=500
48 Correct 20 ms 30164 KB n=500
49 Correct 25 ms 30164 KB n=500
50 Correct 23 ms 30164 KB n=500
51 Correct 28 ms 30164 KB n=500
52 Correct 22 ms 30216 KB n=500
53 Correct 22 ms 30164 KB n=500
54 Correct 25 ms 30224 KB n=500
55 Correct 18 ms 30120 KB n=278
56 Correct 22 ms 30164 KB n=500
57 Correct 21 ms 30096 KB n=500
58 Correct 20 ms 30248 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 15 ms 29652 KB n=5
2 Correct 18 ms 29732 KB n=100
3 Correct 20 ms 29848 KB n=100
4 Correct 17 ms 29780 KB n=100
5 Correct 16 ms 29800 KB n=100
6 Correct 15 ms 29780 KB n=100
7 Correct 15 ms 29832 KB n=100
8 Correct 15 ms 29768 KB n=100
9 Correct 15 ms 29780 KB n=100
10 Correct 20 ms 29772 KB n=100
11 Correct 18 ms 29812 KB n=100
12 Correct 16 ms 29824 KB n=100
13 Correct 16 ms 29848 KB n=100
14 Correct 16 ms 29740 KB n=100
15 Correct 16 ms 29780 KB n=100
16 Correct 17 ms 29756 KB n=100
17 Correct 18 ms 29812 KB n=100
18 Correct 15 ms 29800 KB n=100
19 Correct 16 ms 29780 KB n=100
20 Correct 15 ms 29780 KB n=100
21 Correct 17 ms 29928 KB n=100
22 Correct 16 ms 29780 KB n=100
23 Correct 17 ms 29780 KB n=100
24 Correct 20 ms 29780 KB n=100
25 Correct 16 ms 29780 KB n=100
26 Correct 17 ms 29736 KB n=12
27 Correct 17 ms 29856 KB n=100
28 Correct 24 ms 30172 KB n=500
29 Correct 22 ms 30164 KB n=500
30 Correct 24 ms 30252 KB n=500
31 Correct 23 ms 30160 KB n=500
32 Correct 20 ms 30124 KB n=500
33 Correct 22 ms 30168 KB n=500
34 Correct 20 ms 30164 KB n=500
35 Correct 23 ms 30188 KB n=500
36 Correct 20 ms 30200 KB n=500
37 Correct 17 ms 30140 KB n=500
38 Correct 17 ms 30164 KB n=500
39 Correct 17 ms 30132 KB n=500
40 Correct 18 ms 30132 KB n=500
41 Correct 17 ms 30188 KB n=500
42 Correct 22 ms 30164 KB n=500
43 Correct 21 ms 30164 KB n=500
44 Correct 26 ms 30296 KB n=500
45 Correct 25 ms 30228 KB n=500
46 Correct 21 ms 30164 KB n=500
47 Correct 21 ms 30164 KB n=500
48 Correct 20 ms 30164 KB n=500
49 Correct 25 ms 30164 KB n=500
50 Correct 23 ms 30164 KB n=500
51 Correct 28 ms 30164 KB n=500
52 Correct 22 ms 30216 KB n=500
53 Correct 22 ms 30164 KB n=500
54 Correct 25 ms 30224 KB n=500
55 Correct 18 ms 30120 KB n=278
56 Correct 22 ms 30164 KB n=500
57 Correct 21 ms 30096 KB n=500
58 Correct 20 ms 30248 KB n=500
59 Correct 46 ms 31660 KB n=2000
60 Correct 52 ms 31648 KB n=2000
61 Correct 55 ms 31820 KB n=2000
62 Correct 57 ms 31924 KB n=2000
63 Correct 47 ms 31676 KB n=2000
64 Correct 63 ms 31888 KB n=2000
65 Correct 43 ms 31568 KB n=2000
66 Correct 52 ms 31700 KB n=2000
67 Correct 49 ms 31772 KB n=2000
68 Correct 68 ms 31844 KB n=2000
69 Correct 26 ms 31608 KB n=2000
70 Correct 26 ms 31584 KB n=2000
71 Correct 27 ms 31660 KB n=2000
72 Correct 41 ms 31552 KB n=2000
73 Correct 35 ms 31572 KB n=2000
74 Correct 56 ms 31580 KB n=1844
75 Correct 33 ms 31572 KB n=2000
76 Correct 50 ms 31700 KB n=2000
77 Correct 57 ms 31736 KB n=2000
78 Correct 52 ms 31740 KB n=2000
79 Correct 46 ms 31592 KB n=2000
80 Correct 55 ms 31700 KB n=2000
81 Correct 54 ms 31828 KB n=2000
82 Correct 40 ms 31692 KB n=2000
83 Correct 54 ms 31772 KB n=2000
84 Correct 52 ms 31676 KB n=2000
85 Correct 72 ms 31784 KB n=2000
86 Correct 59 ms 31816 KB n=2000
87 Correct 45 ms 31676 KB n=2000
88 Correct 50 ms 31676 KB n=2000
89 Correct 49 ms 31652 KB n=2000
90 Correct 52 ms 31692 KB n=2000
91 Correct 32 ms 31720 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 15 ms 29652 KB n=5
2 Correct 18 ms 29732 KB n=100
3 Correct 20 ms 29848 KB n=100
4 Correct 17 ms 29780 KB n=100
5 Correct 16 ms 29800 KB n=100
6 Correct 15 ms 29780 KB n=100
7 Correct 15 ms 29832 KB n=100
8 Correct 15 ms 29768 KB n=100
9 Correct 15 ms 29780 KB n=100
10 Correct 20 ms 29772 KB n=100
11 Correct 18 ms 29812 KB n=100
12 Correct 16 ms 29824 KB n=100
13 Correct 16 ms 29848 KB n=100
14 Correct 16 ms 29740 KB n=100
15 Correct 16 ms 29780 KB n=100
16 Correct 17 ms 29756 KB n=100
17 Correct 18 ms 29812 KB n=100
18 Correct 15 ms 29800 KB n=100
19 Correct 16 ms 29780 KB n=100
20 Correct 15 ms 29780 KB n=100
21 Correct 17 ms 29928 KB n=100
22 Correct 16 ms 29780 KB n=100
23 Correct 17 ms 29780 KB n=100
24 Correct 20 ms 29780 KB n=100
25 Correct 16 ms 29780 KB n=100
26 Correct 17 ms 29736 KB n=12
27 Correct 17 ms 29856 KB n=100
28 Correct 24 ms 30172 KB n=500
29 Correct 22 ms 30164 KB n=500
30 Correct 24 ms 30252 KB n=500
31 Correct 23 ms 30160 KB n=500
32 Correct 20 ms 30124 KB n=500
33 Correct 22 ms 30168 KB n=500
34 Correct 20 ms 30164 KB n=500
35 Correct 23 ms 30188 KB n=500
36 Correct 20 ms 30200 KB n=500
37 Correct 17 ms 30140 KB n=500
38 Correct 17 ms 30164 KB n=500
39 Correct 17 ms 30132 KB n=500
40 Correct 18 ms 30132 KB n=500
41 Correct 17 ms 30188 KB n=500
42 Correct 22 ms 30164 KB n=500
43 Correct 21 ms 30164 KB n=500
44 Correct 26 ms 30296 KB n=500
45 Correct 25 ms 30228 KB n=500
46 Correct 21 ms 30164 KB n=500
47 Correct 21 ms 30164 KB n=500
48 Correct 20 ms 30164 KB n=500
49 Correct 25 ms 30164 KB n=500
50 Correct 23 ms 30164 KB n=500
51 Correct 28 ms 30164 KB n=500
52 Correct 22 ms 30216 KB n=500
53 Correct 22 ms 30164 KB n=500
54 Correct 25 ms 30224 KB n=500
55 Correct 18 ms 30120 KB n=278
56 Correct 22 ms 30164 KB n=500
57 Correct 21 ms 30096 KB n=500
58 Correct 20 ms 30248 KB n=500
59 Correct 46 ms 31660 KB n=2000
60 Correct 52 ms 31648 KB n=2000
61 Correct 55 ms 31820 KB n=2000
62 Correct 57 ms 31924 KB n=2000
63 Correct 47 ms 31676 KB n=2000
64 Correct 63 ms 31888 KB n=2000
65 Correct 43 ms 31568 KB n=2000
66 Correct 52 ms 31700 KB n=2000
67 Correct 49 ms 31772 KB n=2000
68 Correct 68 ms 31844 KB n=2000
69 Correct 26 ms 31608 KB n=2000
70 Correct 26 ms 31584 KB n=2000
71 Correct 27 ms 31660 KB n=2000
72 Correct 41 ms 31552 KB n=2000
73 Correct 35 ms 31572 KB n=2000
74 Correct 56 ms 31580 KB n=1844
75 Correct 33 ms 31572 KB n=2000
76 Correct 50 ms 31700 KB n=2000
77 Correct 57 ms 31736 KB n=2000
78 Correct 52 ms 31740 KB n=2000
79 Correct 46 ms 31592 KB n=2000
80 Correct 55 ms 31700 KB n=2000
81 Correct 54 ms 31828 KB n=2000
82 Correct 40 ms 31692 KB n=2000
83 Correct 54 ms 31772 KB n=2000
84 Correct 52 ms 31676 KB n=2000
85 Correct 72 ms 31784 KB n=2000
86 Correct 59 ms 31816 KB n=2000
87 Correct 45 ms 31676 KB n=2000
88 Correct 50 ms 31676 KB n=2000
89 Correct 49 ms 31652 KB n=2000
90 Correct 52 ms 31692 KB n=2000
91 Correct 32 ms 31720 KB n=2000
92 Execution timed out 4059 ms 250352 KB Time limit exceeded
93 Halted 0 ms 0 KB -