Submission #167640

# Submission time Handle Problem Language Result Execution time Memory
167640 2019-12-09T07:53:07 Z HungAnhGoldIBO2020 Birthday gift (IZhO18_treearray) C++14
56 / 100
1248 ms 262148 KB
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+1;
const int LOG=18;
set<pair<int,pair<int,int> > > it[N<<2];
vector<int> adj[N];
int level[N],ancestor[LOG][N],ar[N],lst,lef,rig;
void dfs(int x,int p){
	for(int i=1;i<LOG;i++){
		ancestor[i][x]=ancestor[i-1][ancestor[i-1][x]];
	}
	for(auto i:adj[x]){
		if(i!=p){
			level[i]=level[x]+1;
			ancestor[0][i]=x;
			dfs(i,x);
		}
	}
}
void print(int idx,int l,int r){
	cout<<idx<<' '<<l<<' '<<r<<" START\n";
	for(auto ite=it[idx].begin();ite!=it[idx].end();ite++){
		cout<<ite->first<<' '<<ite->second.first<<' '<<ite->second.second<<endl;
	}
	cout<<"END"<<endl;
}
int findd(int x,int k){
	while(k){
		int y=__lg(k);
		x=ancestor[y][x];
		k-=(1<<y);
	}
	return x;
}
int LCA(int x,int y){
	if(level[x]>level[y]){
		x=findd(x,level[x]-level[y]);
	}
	y=findd(y,level[y]-level[x]);
	if(x==y){
		return x;
	}
	for(int i=LOG-1;i>-1;i--){
	 	if(ancestor[i][x]!=ancestor[i][y]){
	 		x=ancestor[i][x],y=ancestor[i][y];
		}
	}
	return ancestor[0][x];
}
void upd(int idx,int l,int r,int l1,int r1,int val){
	if(l>r1||r<l1){
		return;
	}
	if(l<=l1&&r>=r1){
		it[idx].insert({val,{l1,r1}});
	}
	if(l>=l1&&r<=r1){
		return;
	}
	upd(2*idx,l,(l+r)/2,l1,r1,val);
	upd(2*idx+1,(l+r)/2+1,r,l1,r1,val);
}
void upd1(int idx,int l,int r,int l1,int r1,int val){
	if(l>r1||r<l1){
		return;
	}
	if(l<=l1&&r>=r1){
		it[idx].erase({val,{l1,r1}});
	}
	if(l>=l1&&r<=r1){
		return;
	}
	upd1(2*idx,l,(l+r)/2,l1,r1,val);
	upd1(2*idx+1,(l+r)/2+1,r,l1,r1,val);
}
void query(int idx,int l,int r,int l1,int r1,int val){
	if(lef!=-1){
		return;
	}
	if(l>r1||r<l1){
		return;
	}
	if(l>=l1&&r<=r1){
//		print(idx,l,r);
		auto ite=it[idx].lower_bound({val,{0,0}});
		if(ite!=it[idx].end()&&ite->first==val){
			lef=ite->second.first;
			rig=ite->second.second;
		}
		if(r<r1){
			if(val==LCA(ar[r],ar[r+1])){
				lef=r;
				rig=r+1;
			}
		}
		return;
	}
	query(2*idx,l,(l+r)/2,l1,r1,val);
	query(2*idx+1,(l+r)/2+1,r,l1,r1,val);
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,i,j,k,l,m,q;
	cin>>n>>m>>q;
	for(i=1;i<n;i++){
		cin>>j>>k;
		adj[j].push_back(k);
		adj[k].push_back(j);
	}
	dfs(1,1);
	for(i=1;i<=m;i++){
		cin>>ar[i];
		upd(1,1,m,i,i,ar[i]);
		if(i>1){
			upd(1,1,m,i-1,i,LCA(ar[i-1],ar[i]));
		}
	}
//	cout<<LCA(4,5)<<endl;
	for(i=1;i<=q;i++){
		cin>>j;
		if(j==1){
			cin>>k>>l;
			upd1(1,1,m,k,k,ar[k]);
			if(k>1){
				upd1(1,1,m,k-1,k,LCA(ar[k-1],ar[k]));
			}
			if(k<m){
				upd1(1,1,m,k,k+1,LCA(ar[k],ar[k+1]));
			}
			ar[k]=l;
			upd(1,1,m,k,k,ar[k]);
			if(k>1){
				upd(1,1,m,k-1,k,LCA(ar[k-1],ar[k]));
			}
			if(k<m){
				upd(1,1,m,k,k+1,LCA(ar[k],ar[k+1]));
			}
		}
		else{
			cin>>j>>k>>l;
			lef=rig=-1;
			query(1,1,m,j,k,l);
			cout<<lef<<' '<<rig<<'\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 48 ms 42744 KB n=5
2 Correct 40 ms 42872 KB n=100
3 Correct 40 ms 42744 KB n=100
4 Correct 47 ms 42744 KB n=100
5 Correct 40 ms 42872 KB n=100
6 Correct 39 ms 42872 KB n=100
7 Correct 40 ms 42844 KB n=100
8 Correct 40 ms 42844 KB n=100
9 Correct 40 ms 42860 KB n=100
10 Correct 40 ms 42844 KB n=100
11 Correct 40 ms 42932 KB n=100
12 Correct 40 ms 42744 KB n=100
13 Correct 40 ms 42840 KB n=100
14 Correct 40 ms 42872 KB n=100
15 Correct 40 ms 42788 KB n=100
16 Correct 40 ms 42740 KB n=100
17 Correct 41 ms 42872 KB n=100
18 Correct 40 ms 42872 KB n=100
19 Correct 40 ms 42872 KB n=100
20 Correct 40 ms 42872 KB n=100
21 Correct 41 ms 42856 KB n=100
22 Correct 40 ms 42844 KB n=100
23 Correct 40 ms 42744 KB n=100
24 Correct 40 ms 42744 KB n=100
25 Correct 41 ms 42796 KB n=100
26 Correct 39 ms 42744 KB n=12
27 Correct 40 ms 42764 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 48 ms 42744 KB n=5
2 Correct 40 ms 42872 KB n=100
3 Correct 40 ms 42744 KB n=100
4 Correct 47 ms 42744 KB n=100
5 Correct 40 ms 42872 KB n=100
6 Correct 39 ms 42872 KB n=100
7 Correct 40 ms 42844 KB n=100
8 Correct 40 ms 42844 KB n=100
9 Correct 40 ms 42860 KB n=100
10 Correct 40 ms 42844 KB n=100
11 Correct 40 ms 42932 KB n=100
12 Correct 40 ms 42744 KB n=100
13 Correct 40 ms 42840 KB n=100
14 Correct 40 ms 42872 KB n=100
15 Correct 40 ms 42788 KB n=100
16 Correct 40 ms 42740 KB n=100
17 Correct 41 ms 42872 KB n=100
18 Correct 40 ms 42872 KB n=100
19 Correct 40 ms 42872 KB n=100
20 Correct 40 ms 42872 KB n=100
21 Correct 41 ms 42856 KB n=100
22 Correct 40 ms 42844 KB n=100
23 Correct 40 ms 42744 KB n=100
24 Correct 40 ms 42744 KB n=100
25 Correct 41 ms 42796 KB n=100
26 Correct 39 ms 42744 KB n=12
27 Correct 40 ms 42764 KB n=100
28 Correct 44 ms 43384 KB n=500
29 Correct 45 ms 43384 KB n=500
30 Correct 45 ms 43484 KB n=500
31 Correct 44 ms 43384 KB n=500
32 Correct 51 ms 43384 KB n=500
33 Correct 45 ms 43384 KB n=500
34 Correct 44 ms 43384 KB n=500
35 Correct 45 ms 43428 KB n=500
36 Correct 42 ms 43384 KB n=500
37 Correct 43 ms 43480 KB n=500
38 Correct 42 ms 43384 KB n=500
39 Correct 42 ms 43384 KB n=500
40 Correct 43 ms 43384 KB n=500
41 Correct 43 ms 43364 KB n=500
42 Correct 45 ms 43384 KB n=500
43 Correct 45 ms 43276 KB n=500
44 Correct 45 ms 43384 KB n=500
45 Correct 45 ms 43308 KB n=500
46 Correct 45 ms 43384 KB n=500
47 Correct 45 ms 43384 KB n=500
48 Correct 45 ms 43384 KB n=500
49 Correct 45 ms 43384 KB n=500
50 Correct 45 ms 43384 KB n=500
51 Correct 45 ms 43484 KB n=500
52 Correct 45 ms 43384 KB n=500
53 Correct 45 ms 43384 KB n=500
54 Correct 54 ms 43384 KB n=500
55 Correct 43 ms 43256 KB n=278
56 Correct 44 ms 43384 KB n=500
57 Correct 45 ms 43384 KB n=500
58 Correct 43 ms 43484 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 48 ms 42744 KB n=5
2 Correct 40 ms 42872 KB n=100
3 Correct 40 ms 42744 KB n=100
4 Correct 47 ms 42744 KB n=100
5 Correct 40 ms 42872 KB n=100
6 Correct 39 ms 42872 KB n=100
7 Correct 40 ms 42844 KB n=100
8 Correct 40 ms 42844 KB n=100
9 Correct 40 ms 42860 KB n=100
10 Correct 40 ms 42844 KB n=100
11 Correct 40 ms 42932 KB n=100
12 Correct 40 ms 42744 KB n=100
13 Correct 40 ms 42840 KB n=100
14 Correct 40 ms 42872 KB n=100
15 Correct 40 ms 42788 KB n=100
16 Correct 40 ms 42740 KB n=100
17 Correct 41 ms 42872 KB n=100
18 Correct 40 ms 42872 KB n=100
19 Correct 40 ms 42872 KB n=100
20 Correct 40 ms 42872 KB n=100
21 Correct 41 ms 42856 KB n=100
22 Correct 40 ms 42844 KB n=100
23 Correct 40 ms 42744 KB n=100
24 Correct 40 ms 42744 KB n=100
25 Correct 41 ms 42796 KB n=100
26 Correct 39 ms 42744 KB n=12
27 Correct 40 ms 42764 KB n=100
28 Correct 44 ms 43384 KB n=500
29 Correct 45 ms 43384 KB n=500
30 Correct 45 ms 43484 KB n=500
31 Correct 44 ms 43384 KB n=500
32 Correct 51 ms 43384 KB n=500
33 Correct 45 ms 43384 KB n=500
34 Correct 44 ms 43384 KB n=500
35 Correct 45 ms 43428 KB n=500
36 Correct 42 ms 43384 KB n=500
37 Correct 43 ms 43480 KB n=500
38 Correct 42 ms 43384 KB n=500
39 Correct 42 ms 43384 KB n=500
40 Correct 43 ms 43384 KB n=500
41 Correct 43 ms 43364 KB n=500
42 Correct 45 ms 43384 KB n=500
43 Correct 45 ms 43276 KB n=500
44 Correct 45 ms 43384 KB n=500
45 Correct 45 ms 43308 KB n=500
46 Correct 45 ms 43384 KB n=500
47 Correct 45 ms 43384 KB n=500
48 Correct 45 ms 43384 KB n=500
49 Correct 45 ms 43384 KB n=500
50 Correct 45 ms 43384 KB n=500
51 Correct 45 ms 43484 KB n=500
52 Correct 45 ms 43384 KB n=500
53 Correct 45 ms 43384 KB n=500
54 Correct 54 ms 43384 KB n=500
55 Correct 43 ms 43256 KB n=278
56 Correct 44 ms 43384 KB n=500
57 Correct 45 ms 43384 KB n=500
58 Correct 43 ms 43484 KB n=500
59 Correct 67 ms 45736 KB n=2000
60 Correct 71 ms 46100 KB n=2000
61 Correct 70 ms 45816 KB n=2000
62 Correct 71 ms 45944 KB n=2000
63 Correct 67 ms 45744 KB n=2000
64 Correct 69 ms 45816 KB n=2000
65 Correct 67 ms 45820 KB n=2000
66 Correct 69 ms 45816 KB n=2000
67 Correct 69 ms 45688 KB n=2000
68 Correct 71 ms 45816 KB n=2000
69 Correct 56 ms 45688 KB n=2000
70 Correct 56 ms 45816 KB n=2000
71 Correct 95 ms 45688 KB n=2000
72 Correct 60 ms 45816 KB n=2000
73 Correct 59 ms 45944 KB n=2000
74 Correct 69 ms 45688 KB n=1844
75 Correct 65 ms 45820 KB n=2000
76 Correct 69 ms 45816 KB n=2000
77 Correct 68 ms 45688 KB n=2000
78 Correct 68 ms 45788 KB n=2000
79 Correct 66 ms 45816 KB n=2000
80 Correct 70 ms 45816 KB n=2000
81 Correct 70 ms 45816 KB n=2000
82 Correct 67 ms 45816 KB n=2000
83 Correct 77 ms 45840 KB n=2000
84 Correct 70 ms 45688 KB n=2000
85 Correct 72 ms 45856 KB n=2000
86 Correct 70 ms 45816 KB n=2000
87 Correct 69 ms 45816 KB n=2000
88 Correct 69 ms 45816 KB n=2000
89 Correct 69 ms 45816 KB n=2000
90 Correct 67 ms 45816 KB n=2000
91 Correct 61 ms 45792 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 48 ms 42744 KB n=5
2 Correct 40 ms 42872 KB n=100
3 Correct 40 ms 42744 KB n=100
4 Correct 47 ms 42744 KB n=100
5 Correct 40 ms 42872 KB n=100
6 Correct 39 ms 42872 KB n=100
7 Correct 40 ms 42844 KB n=100
8 Correct 40 ms 42844 KB n=100
9 Correct 40 ms 42860 KB n=100
10 Correct 40 ms 42844 KB n=100
11 Correct 40 ms 42932 KB n=100
12 Correct 40 ms 42744 KB n=100
13 Correct 40 ms 42840 KB n=100
14 Correct 40 ms 42872 KB n=100
15 Correct 40 ms 42788 KB n=100
16 Correct 40 ms 42740 KB n=100
17 Correct 41 ms 42872 KB n=100
18 Correct 40 ms 42872 KB n=100
19 Correct 40 ms 42872 KB n=100
20 Correct 40 ms 42872 KB n=100
21 Correct 41 ms 42856 KB n=100
22 Correct 40 ms 42844 KB n=100
23 Correct 40 ms 42744 KB n=100
24 Correct 40 ms 42744 KB n=100
25 Correct 41 ms 42796 KB n=100
26 Correct 39 ms 42744 KB n=12
27 Correct 40 ms 42764 KB n=100
28 Correct 44 ms 43384 KB n=500
29 Correct 45 ms 43384 KB n=500
30 Correct 45 ms 43484 KB n=500
31 Correct 44 ms 43384 KB n=500
32 Correct 51 ms 43384 KB n=500
33 Correct 45 ms 43384 KB n=500
34 Correct 44 ms 43384 KB n=500
35 Correct 45 ms 43428 KB n=500
36 Correct 42 ms 43384 KB n=500
37 Correct 43 ms 43480 KB n=500
38 Correct 42 ms 43384 KB n=500
39 Correct 42 ms 43384 KB n=500
40 Correct 43 ms 43384 KB n=500
41 Correct 43 ms 43364 KB n=500
42 Correct 45 ms 43384 KB n=500
43 Correct 45 ms 43276 KB n=500
44 Correct 45 ms 43384 KB n=500
45 Correct 45 ms 43308 KB n=500
46 Correct 45 ms 43384 KB n=500
47 Correct 45 ms 43384 KB n=500
48 Correct 45 ms 43384 KB n=500
49 Correct 45 ms 43384 KB n=500
50 Correct 45 ms 43384 KB n=500
51 Correct 45 ms 43484 KB n=500
52 Correct 45 ms 43384 KB n=500
53 Correct 45 ms 43384 KB n=500
54 Correct 54 ms 43384 KB n=500
55 Correct 43 ms 43256 KB n=278
56 Correct 44 ms 43384 KB n=500
57 Correct 45 ms 43384 KB n=500
58 Correct 43 ms 43484 KB n=500
59 Correct 67 ms 45736 KB n=2000
60 Correct 71 ms 46100 KB n=2000
61 Correct 70 ms 45816 KB n=2000
62 Correct 71 ms 45944 KB n=2000
63 Correct 67 ms 45744 KB n=2000
64 Correct 69 ms 45816 KB n=2000
65 Correct 67 ms 45820 KB n=2000
66 Correct 69 ms 45816 KB n=2000
67 Correct 69 ms 45688 KB n=2000
68 Correct 71 ms 45816 KB n=2000
69 Correct 56 ms 45688 KB n=2000
70 Correct 56 ms 45816 KB n=2000
71 Correct 95 ms 45688 KB n=2000
72 Correct 60 ms 45816 KB n=2000
73 Correct 59 ms 45944 KB n=2000
74 Correct 69 ms 45688 KB n=1844
75 Correct 65 ms 45820 KB n=2000
76 Correct 69 ms 45816 KB n=2000
77 Correct 68 ms 45688 KB n=2000
78 Correct 68 ms 45788 KB n=2000
79 Correct 66 ms 45816 KB n=2000
80 Correct 70 ms 45816 KB n=2000
81 Correct 70 ms 45816 KB n=2000
82 Correct 67 ms 45816 KB n=2000
83 Correct 77 ms 45840 KB n=2000
84 Correct 70 ms 45688 KB n=2000
85 Correct 72 ms 45856 KB n=2000
86 Correct 70 ms 45816 KB n=2000
87 Correct 69 ms 45816 KB n=2000
88 Correct 69 ms 45816 KB n=2000
89 Correct 69 ms 45816 KB n=2000
90 Correct 67 ms 45816 KB n=2000
91 Correct 61 ms 45792 KB n=2000
92 Runtime error 1248 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
93 Halted 0 ms 0 KB -