Submission #683814

# Submission time Handle Problem Language Result Execution time Memory
683814 2023-01-19T11:57:11 Z luka1234 Birthday gift (IZhO18_treearray) C++14
100 / 100
860 ms 87040 KB
#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define db double
using namespace std;
int n,m,q;
int a[200001];
vector<int> g[200001];
int A[200001][21];
int p[200001];
int tin[200001],tout[200001];
int timer=0;
int lcas[200001];
set<int> s[200001];
set<int> s2[200001];
set<int>::iterator it;
void dfs(int v,int e){
	p[v]=e;
	tin[v]=++timer;
	for(int i:g[v]){
		if(i!=e){
			dfs(i,v);
		}
	}
	tout[v]=++timer;
}
void prepare(){
	for(int i=1;i<=n;i++){
		A[i][0]=p[i];
		for(int k=1;k<=20;k++){
			A[i][k]=-1;
		}
	}
	for(int k=1;k<=20;k++){
		for(int i=1;i<=n;i++){
			if(A[i][k]==-1){
				if(A[i][k-1]!=-1){
					A[i][k]=A[A[i][k-1]][k-1];
				}
			}
		}
	}
}
bool isancestor(int u,int v){
	return (tin[u]<=tin[v]&&tout[u]>=tout[v]);
}
int lca(int x,int y){
	if(isancestor(x,y))
	   return x;
	if(isancestor(y,x))
	   return y;
	for(int k=20;k>=0;k--){
		if(A[x][k]==-1) 
		   continue;
		if(!isancestor(A[x][k],y)){
			x=A[x][k];
		}
	}
	return p[x];
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>m>>q;
	for(int k=1;k<n;k++){
		int x,y;
		cin>>x>>y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	for(int k=1;k<=m;k++){
		cin>>a[k];
		s2[a[k]].insert(k);
	}
	dfs(1,-1);
	prepare();
	for(int k=1;k<m;k++){
		int f=lca(a[k],a[k+1]);
		lcas[k]=f;
		s[f].insert(k);
	}
	while(q--){
		int ind;
		cin>>ind;
		if(ind==2){
			int x,y,z;
			cin>>x>>y>>z;
			it=s2[z].lower_bound(x);
			if(it!=s2[z].end()){
				int f1=*it;
				if(f1<=y){
					cout<<f1<<' '<<f1<<"\n";
					continue;
				}
			}
			it=s[z].lower_bound(x);
			if(it==s[z].end()){
				cout<<-1<<' '<<-1<<"\n";
				continue;
			}
			int f=*it;
			if(f>(y-1)){
				cout<<-1<<' '<<-1<<"\n";
				continue;
			}
			cout<<f<<' '<<f+1<<"\n";
		}
		else{
			int x,y;
			cin>>x>>y;
			s2[a[x]].erase(x);
			s2[y].insert(x);
			a[x]=y;
			if(x>1){
				int prev=lcas[x-1];
				s[prev].erase(x-1);
				int cur=lca(a[x-1],a[x]);
				s[cur].insert(x-1);
				lcas[x-1]=cur;
			}
			if(x<m){
				int prev=lcas[x];
				s[prev].erase(x);
				int cur=lca(a[x],a[x+1]);
				s[cur].insert(x);
				lcas[x]=cur;
			}
		}
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23800 KB n=5
2 Correct 15 ms 23836 KB n=100
3 Correct 13 ms 23764 KB n=100
4 Correct 11 ms 23764 KB n=100
5 Correct 12 ms 23764 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 12 ms 23816 KB n=100
8 Correct 12 ms 23788 KB n=100
9 Correct 15 ms 23820 KB n=100
10 Correct 13 ms 23824 KB n=100
11 Correct 12 ms 23764 KB n=100
12 Correct 13 ms 23820 KB n=100
13 Correct 12 ms 23764 KB n=100
14 Correct 12 ms 23824 KB n=100
15 Correct 13 ms 23764 KB n=100
16 Correct 12 ms 23764 KB n=100
17 Correct 12 ms 23808 KB n=100
18 Correct 12 ms 23764 KB n=100
19 Correct 14 ms 23824 KB n=100
20 Correct 12 ms 23824 KB n=100
21 Correct 12 ms 23764 KB n=100
22 Correct 13 ms 23844 KB n=100
23 Correct 12 ms 23800 KB n=100
24 Correct 14 ms 23764 KB n=100
25 Correct 12 ms 23764 KB n=100
26 Correct 15 ms 23764 KB n=12
27 Correct 12 ms 23764 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23800 KB n=5
2 Correct 15 ms 23836 KB n=100
3 Correct 13 ms 23764 KB n=100
4 Correct 11 ms 23764 KB n=100
5 Correct 12 ms 23764 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 12 ms 23816 KB n=100
8 Correct 12 ms 23788 KB n=100
9 Correct 15 ms 23820 KB n=100
10 Correct 13 ms 23824 KB n=100
11 Correct 12 ms 23764 KB n=100
12 Correct 13 ms 23820 KB n=100
13 Correct 12 ms 23764 KB n=100
14 Correct 12 ms 23824 KB n=100
15 Correct 13 ms 23764 KB n=100
16 Correct 12 ms 23764 KB n=100
17 Correct 12 ms 23808 KB n=100
18 Correct 12 ms 23764 KB n=100
19 Correct 14 ms 23824 KB n=100
20 Correct 12 ms 23824 KB n=100
21 Correct 12 ms 23764 KB n=100
22 Correct 13 ms 23844 KB n=100
23 Correct 12 ms 23800 KB n=100
24 Correct 14 ms 23764 KB n=100
25 Correct 12 ms 23764 KB n=100
26 Correct 15 ms 23764 KB n=12
27 Correct 12 ms 23764 KB n=100
28 Correct 14 ms 23980 KB n=500
29 Correct 14 ms 24020 KB n=500
30 Correct 13 ms 23892 KB n=500
31 Correct 14 ms 23960 KB n=500
32 Correct 13 ms 23956 KB n=500
33 Correct 12 ms 23892 KB n=500
34 Correct 12 ms 23860 KB n=500
35 Correct 16 ms 23888 KB n=500
36 Correct 15 ms 23956 KB n=500
37 Correct 13 ms 23892 KB n=500
38 Correct 13 ms 23964 KB n=500
39 Correct 13 ms 23964 KB n=500
40 Correct 13 ms 23944 KB n=500
41 Correct 12 ms 23892 KB n=500
42 Correct 14 ms 23892 KB n=500
43 Correct 13 ms 23892 KB n=500
44 Correct 14 ms 24024 KB n=500
45 Correct 13 ms 23908 KB n=500
46 Correct 13 ms 23892 KB n=500
47 Correct 13 ms 23972 KB n=500
48 Correct 12 ms 23956 KB n=500
49 Correct 12 ms 23892 KB n=500
50 Correct 13 ms 23880 KB n=500
51 Correct 13 ms 23892 KB n=500
52 Correct 13 ms 23976 KB n=500
53 Correct 13 ms 23892 KB n=500
54 Correct 13 ms 23892 KB n=500
55 Correct 12 ms 23892 KB n=278
56 Correct 15 ms 23892 KB n=500
57 Correct 12 ms 23868 KB n=500
58 Correct 12 ms 23892 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23800 KB n=5
2 Correct 15 ms 23836 KB n=100
3 Correct 13 ms 23764 KB n=100
4 Correct 11 ms 23764 KB n=100
5 Correct 12 ms 23764 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 12 ms 23816 KB n=100
8 Correct 12 ms 23788 KB n=100
9 Correct 15 ms 23820 KB n=100
10 Correct 13 ms 23824 KB n=100
11 Correct 12 ms 23764 KB n=100
12 Correct 13 ms 23820 KB n=100
13 Correct 12 ms 23764 KB n=100
14 Correct 12 ms 23824 KB n=100
15 Correct 13 ms 23764 KB n=100
16 Correct 12 ms 23764 KB n=100
17 Correct 12 ms 23808 KB n=100
18 Correct 12 ms 23764 KB n=100
19 Correct 14 ms 23824 KB n=100
20 Correct 12 ms 23824 KB n=100
21 Correct 12 ms 23764 KB n=100
22 Correct 13 ms 23844 KB n=100
23 Correct 12 ms 23800 KB n=100
24 Correct 14 ms 23764 KB n=100
25 Correct 12 ms 23764 KB n=100
26 Correct 15 ms 23764 KB n=12
27 Correct 12 ms 23764 KB n=100
28 Correct 14 ms 23980 KB n=500
29 Correct 14 ms 24020 KB n=500
30 Correct 13 ms 23892 KB n=500
31 Correct 14 ms 23960 KB n=500
32 Correct 13 ms 23956 KB n=500
33 Correct 12 ms 23892 KB n=500
34 Correct 12 ms 23860 KB n=500
35 Correct 16 ms 23888 KB n=500
36 Correct 15 ms 23956 KB n=500
37 Correct 13 ms 23892 KB n=500
38 Correct 13 ms 23964 KB n=500
39 Correct 13 ms 23964 KB n=500
40 Correct 13 ms 23944 KB n=500
41 Correct 12 ms 23892 KB n=500
42 Correct 14 ms 23892 KB n=500
43 Correct 13 ms 23892 KB n=500
44 Correct 14 ms 24024 KB n=500
45 Correct 13 ms 23908 KB n=500
46 Correct 13 ms 23892 KB n=500
47 Correct 13 ms 23972 KB n=500
48 Correct 12 ms 23956 KB n=500
49 Correct 12 ms 23892 KB n=500
50 Correct 13 ms 23880 KB n=500
51 Correct 13 ms 23892 KB n=500
52 Correct 13 ms 23976 KB n=500
53 Correct 13 ms 23892 KB n=500
54 Correct 13 ms 23892 KB n=500
55 Correct 12 ms 23892 KB n=278
56 Correct 15 ms 23892 KB n=500
57 Correct 12 ms 23868 KB n=500
58 Correct 12 ms 23892 KB n=500
59 Correct 14 ms 24348 KB n=2000
60 Correct 17 ms 24492 KB n=2000
61 Correct 16 ms 24372 KB n=2000
62 Correct 15 ms 24276 KB n=2000
63 Correct 15 ms 24276 KB n=2000
64 Correct 14 ms 24344 KB n=2000
65 Correct 15 ms 24348 KB n=2000
66 Correct 18 ms 24404 KB n=2000
67 Correct 15 ms 24276 KB n=2000
68 Correct 15 ms 24268 KB n=2000
69 Correct 14 ms 24352 KB n=2000
70 Correct 14 ms 24288 KB n=2000
71 Correct 14 ms 24352 KB n=2000
72 Correct 14 ms 24276 KB n=2000
73 Correct 14 ms 24276 KB n=2000
74 Correct 15 ms 24216 KB n=1844
75 Correct 16 ms 24268 KB n=2000
76 Correct 19 ms 24260 KB n=2000
77 Correct 15 ms 24296 KB n=2000
78 Correct 16 ms 24296 KB n=2000
79 Correct 14 ms 24292 KB n=2000
80 Correct 14 ms 24404 KB n=2000
81 Correct 14 ms 24276 KB n=2000
82 Correct 15 ms 24276 KB n=2000
83 Correct 15 ms 24332 KB n=2000
84 Correct 14 ms 24276 KB n=2000
85 Correct 14 ms 24276 KB n=2000
86 Correct 15 ms 24276 KB n=2000
87 Correct 14 ms 24276 KB n=2000
88 Correct 17 ms 24328 KB n=2000
89 Correct 14 ms 24408 KB n=2000
90 Correct 16 ms 24368 KB n=2000
91 Correct 15 ms 24368 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23800 KB n=5
2 Correct 15 ms 23836 KB n=100
3 Correct 13 ms 23764 KB n=100
4 Correct 11 ms 23764 KB n=100
5 Correct 12 ms 23764 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 12 ms 23816 KB n=100
8 Correct 12 ms 23788 KB n=100
9 Correct 15 ms 23820 KB n=100
10 Correct 13 ms 23824 KB n=100
11 Correct 12 ms 23764 KB n=100
12 Correct 13 ms 23820 KB n=100
13 Correct 12 ms 23764 KB n=100
14 Correct 12 ms 23824 KB n=100
15 Correct 13 ms 23764 KB n=100
16 Correct 12 ms 23764 KB n=100
17 Correct 12 ms 23808 KB n=100
18 Correct 12 ms 23764 KB n=100
19 Correct 14 ms 23824 KB n=100
20 Correct 12 ms 23824 KB n=100
21 Correct 12 ms 23764 KB n=100
22 Correct 13 ms 23844 KB n=100
23 Correct 12 ms 23800 KB n=100
24 Correct 14 ms 23764 KB n=100
25 Correct 12 ms 23764 KB n=100
26 Correct 15 ms 23764 KB n=12
27 Correct 12 ms 23764 KB n=100
28 Correct 14 ms 23980 KB n=500
29 Correct 14 ms 24020 KB n=500
30 Correct 13 ms 23892 KB n=500
31 Correct 14 ms 23960 KB n=500
32 Correct 13 ms 23956 KB n=500
33 Correct 12 ms 23892 KB n=500
34 Correct 12 ms 23860 KB n=500
35 Correct 16 ms 23888 KB n=500
36 Correct 15 ms 23956 KB n=500
37 Correct 13 ms 23892 KB n=500
38 Correct 13 ms 23964 KB n=500
39 Correct 13 ms 23964 KB n=500
40 Correct 13 ms 23944 KB n=500
41 Correct 12 ms 23892 KB n=500
42 Correct 14 ms 23892 KB n=500
43 Correct 13 ms 23892 KB n=500
44 Correct 14 ms 24024 KB n=500
45 Correct 13 ms 23908 KB n=500
46 Correct 13 ms 23892 KB n=500
47 Correct 13 ms 23972 KB n=500
48 Correct 12 ms 23956 KB n=500
49 Correct 12 ms 23892 KB n=500
50 Correct 13 ms 23880 KB n=500
51 Correct 13 ms 23892 KB n=500
52 Correct 13 ms 23976 KB n=500
53 Correct 13 ms 23892 KB n=500
54 Correct 13 ms 23892 KB n=500
55 Correct 12 ms 23892 KB n=278
56 Correct 15 ms 23892 KB n=500
57 Correct 12 ms 23868 KB n=500
58 Correct 12 ms 23892 KB n=500
59 Correct 14 ms 24348 KB n=2000
60 Correct 17 ms 24492 KB n=2000
61 Correct 16 ms 24372 KB n=2000
62 Correct 15 ms 24276 KB n=2000
63 Correct 15 ms 24276 KB n=2000
64 Correct 14 ms 24344 KB n=2000
65 Correct 15 ms 24348 KB n=2000
66 Correct 18 ms 24404 KB n=2000
67 Correct 15 ms 24276 KB n=2000
68 Correct 15 ms 24268 KB n=2000
69 Correct 14 ms 24352 KB n=2000
70 Correct 14 ms 24288 KB n=2000
71 Correct 14 ms 24352 KB n=2000
72 Correct 14 ms 24276 KB n=2000
73 Correct 14 ms 24276 KB n=2000
74 Correct 15 ms 24216 KB n=1844
75 Correct 16 ms 24268 KB n=2000
76 Correct 19 ms 24260 KB n=2000
77 Correct 15 ms 24296 KB n=2000
78 Correct 16 ms 24296 KB n=2000
79 Correct 14 ms 24292 KB n=2000
80 Correct 14 ms 24404 KB n=2000
81 Correct 14 ms 24276 KB n=2000
82 Correct 15 ms 24276 KB n=2000
83 Correct 15 ms 24332 KB n=2000
84 Correct 14 ms 24276 KB n=2000
85 Correct 14 ms 24276 KB n=2000
86 Correct 15 ms 24276 KB n=2000
87 Correct 14 ms 24276 KB n=2000
88 Correct 17 ms 24328 KB n=2000
89 Correct 14 ms 24408 KB n=2000
90 Correct 16 ms 24368 KB n=2000
91 Correct 15 ms 24368 KB n=2000
92 Correct 550 ms 77676 KB n=200000
93 Correct 708 ms 82536 KB n=200000
94 Correct 590 ms 85820 KB n=200000
95 Correct 584 ms 77420 KB n=200000
96 Correct 596 ms 77492 KB n=200000
97 Correct 746 ms 81672 KB n=200000
98 Correct 590 ms 77448 KB n=200000
99 Correct 683 ms 77748 KB n=200000
100 Correct 600 ms 77556 KB n=200000
101 Correct 522 ms 87040 KB n=200000
102 Correct 345 ms 78644 KB n=200000
103 Correct 337 ms 78716 KB n=200000
104 Correct 354 ms 78596 KB n=200000
105 Correct 345 ms 79004 KB n=200000
106 Correct 384 ms 79104 KB n=200000
107 Correct 388 ms 79148 KB n=200000
108 Correct 592 ms 77516 KB n=200000
109 Correct 603 ms 77580 KB n=200000
110 Correct 627 ms 77460 KB n=200000
111 Correct 572 ms 76880 KB n=200000
112 Correct 519 ms 86068 KB n=200000
113 Correct 768 ms 81608 KB n=200000
114 Correct 611 ms 76924 KB n=200000
115 Correct 860 ms 79556 KB n=200000
116 Correct 564 ms 77736 KB n=200000
117 Correct 518 ms 86520 KB n=200000
118 Correct 793 ms 80492 KB n=200000
119 Correct 620 ms 77768 KB n=200000
120 Correct 516 ms 86152 KB n=200000
121 Correct 488 ms 86064 KB n=200000
122 Correct 500 ms 86420 KB n=200000
123 Correct 406 ms 78804 KB n=200000
124 Correct 159 ms 39372 KB n=25264