답안 #713287

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
713287 2023-03-21T16:01:14 Z amirhoseinfar1385 Birthday gift (IZhO18_treearray) C++17
100 / 100
1290 ms 77700 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;
}
set<pair<int,int>>st[maxn];
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++){
		st[allm[i]].insert(make_pair(i,i));
		if(i!=m-1){
			st[getlca(allm[i],allm[i+1])].insert(make_pair(i,i+1));
		}
	}
	for(int i=0;i<q;i++){
		int qq;
		cin>>qq;
		if(qq==1){
			int ind,v;
			cin>>ind>>v;
			ind--;
			st[allm[ind]].erase(make_pair(ind,ind));
			if(ind!=m-1){
				st[getlca(allm[ind],allm[ind+1])].erase(make_pair(ind,ind+1));
			}
			if(ind!=0){
				st[getlca(allm[ind],allm[ind-1])].erase(make_pair(ind-1,ind));
			}
			allm[ind]=v;
			st[allm[ind]].insert(make_pair(ind,ind));
			if(ind!=m-1){
				st[getlca(allm[ind],allm[ind+1])].insert(make_pair(ind,ind+1));
			}
			if(ind!=0){
				st[getlca(allm[ind],allm[ind-1])].insert(make_pair(ind-1,ind));
			}
		}
		else{
			int l,r,v;
			cin>>l>>r>>v;
			l--;
			r--;
			auto xx=st[v].lower_bound(make_pair(l,0));
			if(xx==st[v].end()){
				cout<<-1<<" "<<-1<<"\n";
				continue;
			}
			auto x=*xx;
			if(x.second<=r){
				cout<<x.first+1<<" "<<x.second+1<<"\n";
			}
			else{
				cout<<-1<<" "<<-1<<"\n";
			}
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14456 KB n=5
2 Correct 8 ms 14420 KB n=100
3 Correct 8 ms 14420 KB n=100
4 Correct 8 ms 14420 KB n=100
5 Correct 8 ms 14432 KB n=100
6 Correct 7 ms 14420 KB n=100
7 Correct 7 ms 14420 KB n=100
8 Correct 7 ms 14420 KB n=100
9 Correct 8 ms 14420 KB n=100
10 Correct 7 ms 14420 KB n=100
11 Correct 7 ms 14540 KB n=100
12 Correct 8 ms 14536 KB n=100
13 Correct 8 ms 14420 KB n=100
14 Correct 7 ms 14532 KB n=100
15 Correct 7 ms 14420 KB n=100
16 Correct 7 ms 14420 KB n=100
17 Correct 7 ms 14420 KB n=100
18 Correct 7 ms 14420 KB n=100
19 Correct 8 ms 14420 KB n=100
20 Correct 7 ms 14420 KB n=100
21 Correct 7 ms 14420 KB n=100
22 Correct 8 ms 14500 KB n=100
23 Correct 8 ms 14420 KB n=100
24 Correct 8 ms 14420 KB n=100
25 Correct 8 ms 14420 KB n=100
26 Correct 8 ms 14420 KB n=12
27 Correct 8 ms 14536 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14456 KB n=5
2 Correct 8 ms 14420 KB n=100
3 Correct 8 ms 14420 KB n=100
4 Correct 8 ms 14420 KB n=100
5 Correct 8 ms 14432 KB n=100
6 Correct 7 ms 14420 KB n=100
7 Correct 7 ms 14420 KB n=100
8 Correct 7 ms 14420 KB n=100
9 Correct 8 ms 14420 KB n=100
10 Correct 7 ms 14420 KB n=100
11 Correct 7 ms 14540 KB n=100
12 Correct 8 ms 14536 KB n=100
13 Correct 8 ms 14420 KB n=100
14 Correct 7 ms 14532 KB n=100
15 Correct 7 ms 14420 KB n=100
16 Correct 7 ms 14420 KB n=100
17 Correct 7 ms 14420 KB n=100
18 Correct 7 ms 14420 KB n=100
19 Correct 8 ms 14420 KB n=100
20 Correct 7 ms 14420 KB n=100
21 Correct 7 ms 14420 KB n=100
22 Correct 8 ms 14500 KB n=100
23 Correct 8 ms 14420 KB n=100
24 Correct 8 ms 14420 KB n=100
25 Correct 8 ms 14420 KB n=100
26 Correct 8 ms 14420 KB n=12
27 Correct 8 ms 14536 KB n=100
28 Correct 8 ms 14608 KB n=500
29 Correct 8 ms 14548 KB n=500
30 Correct 9 ms 14540 KB n=500
31 Correct 8 ms 14580 KB n=500
32 Correct 8 ms 14548 KB n=500
33 Correct 8 ms 14548 KB n=500
34 Correct 8 ms 14548 KB n=500
35 Correct 8 ms 14548 KB n=500
36 Correct 9 ms 14580 KB n=500
37 Correct 8 ms 14552 KB n=500
38 Correct 9 ms 14548 KB n=500
39 Correct 9 ms 14548 KB n=500
40 Correct 7 ms 14548 KB n=500
41 Correct 8 ms 14548 KB n=500
42 Correct 7 ms 14548 KB n=500
43 Correct 9 ms 14548 KB n=500
44 Correct 8 ms 14548 KB n=500
45 Correct 8 ms 14520 KB n=500
46 Correct 8 ms 14652 KB n=500
47 Correct 9 ms 14548 KB n=500
48 Correct 8 ms 14548 KB n=500
49 Correct 8 ms 14560 KB n=500
50 Correct 8 ms 14532 KB n=500
51 Correct 8 ms 14676 KB n=500
52 Correct 8 ms 14548 KB n=500
53 Correct 8 ms 14592 KB n=500
54 Correct 8 ms 14548 KB n=500
55 Correct 7 ms 14548 KB n=278
56 Correct 9 ms 14548 KB n=500
57 Correct 9 ms 14572 KB n=500
58 Correct 9 ms 14676 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14456 KB n=5
2 Correct 8 ms 14420 KB n=100
3 Correct 8 ms 14420 KB n=100
4 Correct 8 ms 14420 KB n=100
5 Correct 8 ms 14432 KB n=100
6 Correct 7 ms 14420 KB n=100
7 Correct 7 ms 14420 KB n=100
8 Correct 7 ms 14420 KB n=100
9 Correct 8 ms 14420 KB n=100
10 Correct 7 ms 14420 KB n=100
11 Correct 7 ms 14540 KB n=100
12 Correct 8 ms 14536 KB n=100
13 Correct 8 ms 14420 KB n=100
14 Correct 7 ms 14532 KB n=100
15 Correct 7 ms 14420 KB n=100
16 Correct 7 ms 14420 KB n=100
17 Correct 7 ms 14420 KB n=100
18 Correct 7 ms 14420 KB n=100
19 Correct 8 ms 14420 KB n=100
20 Correct 7 ms 14420 KB n=100
21 Correct 7 ms 14420 KB n=100
22 Correct 8 ms 14500 KB n=100
23 Correct 8 ms 14420 KB n=100
24 Correct 8 ms 14420 KB n=100
25 Correct 8 ms 14420 KB n=100
26 Correct 8 ms 14420 KB n=12
27 Correct 8 ms 14536 KB n=100
28 Correct 8 ms 14608 KB n=500
29 Correct 8 ms 14548 KB n=500
30 Correct 9 ms 14540 KB n=500
31 Correct 8 ms 14580 KB n=500
32 Correct 8 ms 14548 KB n=500
33 Correct 8 ms 14548 KB n=500
34 Correct 8 ms 14548 KB n=500
35 Correct 8 ms 14548 KB n=500
36 Correct 9 ms 14580 KB n=500
37 Correct 8 ms 14552 KB n=500
38 Correct 9 ms 14548 KB n=500
39 Correct 9 ms 14548 KB n=500
40 Correct 7 ms 14548 KB n=500
41 Correct 8 ms 14548 KB n=500
42 Correct 7 ms 14548 KB n=500
43 Correct 9 ms 14548 KB n=500
44 Correct 8 ms 14548 KB n=500
45 Correct 8 ms 14520 KB n=500
46 Correct 8 ms 14652 KB n=500
47 Correct 9 ms 14548 KB n=500
48 Correct 8 ms 14548 KB n=500
49 Correct 8 ms 14560 KB n=500
50 Correct 8 ms 14532 KB n=500
51 Correct 8 ms 14676 KB n=500
52 Correct 8 ms 14548 KB n=500
53 Correct 8 ms 14592 KB n=500
54 Correct 8 ms 14548 KB n=500
55 Correct 7 ms 14548 KB n=278
56 Correct 9 ms 14548 KB n=500
57 Correct 9 ms 14572 KB n=500
58 Correct 9 ms 14676 KB n=500
59 Correct 10 ms 14932 KB n=2000
60 Correct 10 ms 15060 KB n=2000
61 Correct 12 ms 14932 KB n=2000
62 Correct 11 ms 14988 KB n=2000
63 Correct 10 ms 14932 KB n=2000
64 Correct 10 ms 14932 KB n=2000
65 Correct 10 ms 14964 KB n=2000
66 Correct 10 ms 14928 KB n=2000
67 Correct 12 ms 14868 KB n=2000
68 Correct 10 ms 15016 KB n=2000
69 Correct 10 ms 14932 KB n=2000
70 Correct 10 ms 14864 KB n=2000
71 Correct 10 ms 14932 KB n=2000
72 Correct 10 ms 14932 KB n=2000
73 Correct 10 ms 14932 KB n=2000
74 Correct 10 ms 14804 KB n=1844
75 Correct 10 ms 14844 KB n=2000
76 Correct 11 ms 14956 KB n=2000
77 Correct 10 ms 14932 KB n=2000
78 Correct 10 ms 14932 KB n=2000
79 Correct 10 ms 14964 KB n=2000
80 Correct 10 ms 15048 KB n=2000
81 Correct 11 ms 14928 KB n=2000
82 Correct 11 ms 14932 KB n=2000
83 Correct 9 ms 14932 KB n=2000
84 Correct 10 ms 14932 KB n=2000
85 Correct 10 ms 14944 KB n=2000
86 Correct 10 ms 14992 KB n=2000
87 Correct 10 ms 14932 KB n=2000
88 Correct 9 ms 15060 KB n=2000
89 Correct 9 ms 15056 KB n=2000
90 Correct 10 ms 15060 KB n=2000
91 Correct 11 ms 14964 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14456 KB n=5
2 Correct 8 ms 14420 KB n=100
3 Correct 8 ms 14420 KB n=100
4 Correct 8 ms 14420 KB n=100
5 Correct 8 ms 14432 KB n=100
6 Correct 7 ms 14420 KB n=100
7 Correct 7 ms 14420 KB n=100
8 Correct 7 ms 14420 KB n=100
9 Correct 8 ms 14420 KB n=100
10 Correct 7 ms 14420 KB n=100
11 Correct 7 ms 14540 KB n=100
12 Correct 8 ms 14536 KB n=100
13 Correct 8 ms 14420 KB n=100
14 Correct 7 ms 14532 KB n=100
15 Correct 7 ms 14420 KB n=100
16 Correct 7 ms 14420 KB n=100
17 Correct 7 ms 14420 KB n=100
18 Correct 7 ms 14420 KB n=100
19 Correct 8 ms 14420 KB n=100
20 Correct 7 ms 14420 KB n=100
21 Correct 7 ms 14420 KB n=100
22 Correct 8 ms 14500 KB n=100
23 Correct 8 ms 14420 KB n=100
24 Correct 8 ms 14420 KB n=100
25 Correct 8 ms 14420 KB n=100
26 Correct 8 ms 14420 KB n=12
27 Correct 8 ms 14536 KB n=100
28 Correct 8 ms 14608 KB n=500
29 Correct 8 ms 14548 KB n=500
30 Correct 9 ms 14540 KB n=500
31 Correct 8 ms 14580 KB n=500
32 Correct 8 ms 14548 KB n=500
33 Correct 8 ms 14548 KB n=500
34 Correct 8 ms 14548 KB n=500
35 Correct 8 ms 14548 KB n=500
36 Correct 9 ms 14580 KB n=500
37 Correct 8 ms 14552 KB n=500
38 Correct 9 ms 14548 KB n=500
39 Correct 9 ms 14548 KB n=500
40 Correct 7 ms 14548 KB n=500
41 Correct 8 ms 14548 KB n=500
42 Correct 7 ms 14548 KB n=500
43 Correct 9 ms 14548 KB n=500
44 Correct 8 ms 14548 KB n=500
45 Correct 8 ms 14520 KB n=500
46 Correct 8 ms 14652 KB n=500
47 Correct 9 ms 14548 KB n=500
48 Correct 8 ms 14548 KB n=500
49 Correct 8 ms 14560 KB n=500
50 Correct 8 ms 14532 KB n=500
51 Correct 8 ms 14676 KB n=500
52 Correct 8 ms 14548 KB n=500
53 Correct 8 ms 14592 KB n=500
54 Correct 8 ms 14548 KB n=500
55 Correct 7 ms 14548 KB n=278
56 Correct 9 ms 14548 KB n=500
57 Correct 9 ms 14572 KB n=500
58 Correct 9 ms 14676 KB n=500
59 Correct 10 ms 14932 KB n=2000
60 Correct 10 ms 15060 KB n=2000
61 Correct 12 ms 14932 KB n=2000
62 Correct 11 ms 14988 KB n=2000
63 Correct 10 ms 14932 KB n=2000
64 Correct 10 ms 14932 KB n=2000
65 Correct 10 ms 14964 KB n=2000
66 Correct 10 ms 14928 KB n=2000
67 Correct 12 ms 14868 KB n=2000
68 Correct 10 ms 15016 KB n=2000
69 Correct 10 ms 14932 KB n=2000
70 Correct 10 ms 14864 KB n=2000
71 Correct 10 ms 14932 KB n=2000
72 Correct 10 ms 14932 KB n=2000
73 Correct 10 ms 14932 KB n=2000
74 Correct 10 ms 14804 KB n=1844
75 Correct 10 ms 14844 KB n=2000
76 Correct 11 ms 14956 KB n=2000
77 Correct 10 ms 14932 KB n=2000
78 Correct 10 ms 14932 KB n=2000
79 Correct 10 ms 14964 KB n=2000
80 Correct 10 ms 15048 KB n=2000
81 Correct 11 ms 14928 KB n=2000
82 Correct 11 ms 14932 KB n=2000
83 Correct 9 ms 14932 KB n=2000
84 Correct 10 ms 14932 KB n=2000
85 Correct 10 ms 14944 KB n=2000
86 Correct 10 ms 14992 KB n=2000
87 Correct 10 ms 14932 KB n=2000
88 Correct 9 ms 15060 KB n=2000
89 Correct 9 ms 15056 KB n=2000
90 Correct 10 ms 15060 KB n=2000
91 Correct 11 ms 14964 KB n=2000
92 Correct 720 ms 60844 KB n=200000
93 Correct 991 ms 71552 KB n=200000
94 Correct 560 ms 76036 KB n=200000
95 Correct 799 ms 65076 KB n=200000
96 Correct 771 ms 64892 KB n=200000
97 Correct 997 ms 70304 KB n=200000
98 Correct 677 ms 64876 KB n=200000
99 Correct 777 ms 65100 KB n=200000
100 Correct 669 ms 64964 KB n=200000
101 Correct 429 ms 77700 KB n=200000
102 Correct 373 ms 66124 KB n=200000
103 Correct 352 ms 66156 KB n=200000
104 Correct 374 ms 66160 KB n=200000
105 Correct 379 ms 66636 KB n=200000
106 Correct 368 ms 66508 KB n=200000
107 Correct 366 ms 66644 KB n=200000
108 Correct 667 ms 65076 KB n=200000
109 Correct 740 ms 64984 KB n=200000
110 Correct 633 ms 65040 KB n=200000
111 Correct 657 ms 64348 KB n=200000
112 Correct 500 ms 76484 KB n=200000
113 Correct 960 ms 70272 KB n=200000
114 Correct 632 ms 64588 KB n=200000
115 Correct 1290 ms 67660 KB n=200000
116 Correct 624 ms 65088 KB n=200000
117 Correct 454 ms 76832 KB n=200000
118 Correct 1158 ms 68772 KB n=200000
119 Correct 611 ms 65088 KB n=200000
120 Correct 399 ms 76632 KB n=200000
121 Correct 380 ms 76748 KB n=200000
122 Correct 401 ms 77080 KB n=200000
123 Correct 404 ms 66344 KB n=200000
124 Correct 155 ms 29664 KB n=25264