답안 #651305

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
651305 2022-10-18T08:14:42 Z alvingogo Birthday gift (IZhO18_treearray) C++14
100 / 100
1883 ms 69128 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
using namespace std;

struct no{
	vector<int> ch;
	int as[20]={0};
	int dep=-1;
};
vector<no> v;
void dfs(int r,int f){
	v[r].as[0]=f;
	v[r].dep=v[f].dep+1;
	for(auto h:v[r].ch){
		if(h!=f){
			dfs(h,r);
		}
	}
}
int lca(int x,int y){
	if(v[x].dep<v[y].dep){
		swap(x,y);
	}
	for(int i=19;i>=0;i--){
		if(v[v[x].as[i]].dep>=v[y].dep){
			x=v[x].as[i];
		}
	}
	if(x==y){
		return x;
	}
	for(int i=19;i>=0;i--){
		if(v[x].as[i]!=v[y].as[i]){
			x=v[x].as[i];
			y=v[y].as[i];
		}
	}
	return v[x].as[0];
}
int main(){
    AquA;
	int n,m,q;
	cin >> n >> m >> q;
	v.resize(n);
	for(int i=1;i<n;i++){
		int a,b;
		cin >> a >> b;
		a--;
		b--;
		v[a].ch.push_back(b);
		v[b].ch.push_back(a);
	}
	dfs(0,0);
	for(int i=1;i<20;i++){
		for(int j=0;j<n;j++){
			v[j].as[i]=v[v[j].as[i-1]].as[i-1];
		}
	}
	vector<int> g(m);
	multiset<pair<int,pair<int,int> > > s;
	for(int i=0;i<m;i++){
		cin >> g[i];
		g[i]--;
		s.insert({g[i],{i,i}});
	}
	vector<int> y(m);
	for(int i=1;i<m;i++){
		y[i]=lca(g[i-1],g[i]);
		s.insert({y[i],{i-1,i}});
	}
	for(int i=0;i<q;i++){
		int t;
		cin >> t;
		if(t==1){
			int p,k;
			cin >> p >> k;
			k--;
			p--;
			s.erase({g[p],{p,p}});
			if(p){
				s.erase({y[p],{p-1,p}});
			}
			if(p+1<m){
				s.erase({y[p+1],{p,p+1}});
			}
			g[p]=k;
			s.insert({g[p],{p,p}});
			if(p){
				y[p]=lca(g[p],g[p-1]);
				s.insert({y[p],{p-1,p}});
			}
			if(p+1<m){
				y[p+1]=lca(g[p],g[p+1]);
				s.insert({y[p+1],{p,p+1}});
			}
		}
		else{
			int l,r,k;
			cin >> l >> r >> k;
			k--;
			l--;
			r--;
			auto h=s.lower_bound({k,{l,-1}});
			if(h==s.end() || (*h).sc.sc>r || (*h).fs!=k){
				cout << "-1 -1\n";
			}
			else{
				cout << (*h).sc.fs+1 << " " << (*h).sc.sc+1 << "\n";
			}
		}
	}
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 340 KB n=500
29 Correct 1 ms 468 KB n=500
30 Correct 2 ms 340 KB n=500
31 Correct 2 ms 340 KB n=500
32 Correct 1 ms 340 KB n=500
33 Correct 2 ms 340 KB n=500
34 Correct 2 ms 468 KB n=500
35 Correct 1 ms 468 KB n=500
36 Correct 1 ms 456 KB n=500
37 Correct 1 ms 468 KB n=500
38 Correct 1 ms 468 KB n=500
39 Correct 1 ms 468 KB n=500
40 Correct 1 ms 456 KB n=500
41 Correct 1 ms 468 KB n=500
42 Correct 2 ms 452 KB n=500
43 Correct 2 ms 456 KB n=500
44 Correct 2 ms 468 KB n=500
45 Correct 1 ms 468 KB n=500
46 Correct 2 ms 468 KB n=500
47 Correct 2 ms 468 KB n=500
48 Correct 2 ms 468 KB n=500
49 Correct 2 ms 468 KB n=500
50 Correct 1 ms 468 KB n=500
51 Correct 1 ms 468 KB n=500
52 Correct 2 ms 468 KB n=500
53 Correct 1 ms 468 KB n=500
54 Correct 2 ms 456 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 2 ms 468 KB n=500
57 Correct 1 ms 456 KB n=500
58 Correct 1 ms 468 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 340 KB n=500
29 Correct 1 ms 468 KB n=500
30 Correct 2 ms 340 KB n=500
31 Correct 2 ms 340 KB n=500
32 Correct 1 ms 340 KB n=500
33 Correct 2 ms 340 KB n=500
34 Correct 2 ms 468 KB n=500
35 Correct 1 ms 468 KB n=500
36 Correct 1 ms 456 KB n=500
37 Correct 1 ms 468 KB n=500
38 Correct 1 ms 468 KB n=500
39 Correct 1 ms 468 KB n=500
40 Correct 1 ms 456 KB n=500
41 Correct 1 ms 468 KB n=500
42 Correct 2 ms 452 KB n=500
43 Correct 2 ms 456 KB n=500
44 Correct 2 ms 468 KB n=500
45 Correct 1 ms 468 KB n=500
46 Correct 2 ms 468 KB n=500
47 Correct 2 ms 468 KB n=500
48 Correct 2 ms 468 KB n=500
49 Correct 2 ms 468 KB n=500
50 Correct 1 ms 468 KB n=500
51 Correct 1 ms 468 KB n=500
52 Correct 2 ms 468 KB n=500
53 Correct 1 ms 468 KB n=500
54 Correct 2 ms 456 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 2 ms 468 KB n=500
57 Correct 1 ms 456 KB n=500
58 Correct 1 ms 468 KB n=500
59 Correct 5 ms 896 KB n=2000
60 Correct 7 ms 972 KB n=2000
61 Correct 6 ms 852 KB n=2000
62 Correct 5 ms 900 KB n=2000
63 Correct 5 ms 904 KB n=2000
64 Correct 5 ms 852 KB n=2000
65 Correct 5 ms 852 KB n=2000
66 Correct 5 ms 852 KB n=2000
67 Correct 5 ms 852 KB n=2000
68 Correct 6 ms 852 KB n=2000
69 Correct 4 ms 900 KB n=2000
70 Correct 4 ms 848 KB n=2000
71 Correct 4 ms 852 KB n=2000
72 Correct 4 ms 900 KB n=2000
73 Correct 4 ms 848 KB n=2000
74 Correct 4 ms 852 KB n=1844
75 Correct 5 ms 852 KB n=2000
76 Correct 5 ms 852 KB n=2000
77 Correct 5 ms 852 KB n=2000
78 Correct 5 ms 852 KB n=2000
79 Correct 5 ms 852 KB n=2000
80 Correct 6 ms 976 KB n=2000
81 Correct 8 ms 852 KB n=2000
82 Correct 6 ms 852 KB n=2000
83 Correct 6 ms 852 KB n=2000
84 Correct 5 ms 852 KB n=2000
85 Correct 6 ms 852 KB n=2000
86 Correct 6 ms 840 KB n=2000
87 Correct 5 ms 852 KB n=2000
88 Correct 4 ms 980 KB n=2000
89 Correct 5 ms 972 KB n=2000
90 Correct 6 ms 980 KB n=2000
91 Correct 3 ms 840 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 1 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 1 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 340 KB n=500
29 Correct 1 ms 468 KB n=500
30 Correct 2 ms 340 KB n=500
31 Correct 2 ms 340 KB n=500
32 Correct 1 ms 340 KB n=500
33 Correct 2 ms 340 KB n=500
34 Correct 2 ms 468 KB n=500
35 Correct 1 ms 468 KB n=500
36 Correct 1 ms 456 KB n=500
37 Correct 1 ms 468 KB n=500
38 Correct 1 ms 468 KB n=500
39 Correct 1 ms 468 KB n=500
40 Correct 1 ms 456 KB n=500
41 Correct 1 ms 468 KB n=500
42 Correct 2 ms 452 KB n=500
43 Correct 2 ms 456 KB n=500
44 Correct 2 ms 468 KB n=500
45 Correct 1 ms 468 KB n=500
46 Correct 2 ms 468 KB n=500
47 Correct 2 ms 468 KB n=500
48 Correct 2 ms 468 KB n=500
49 Correct 2 ms 468 KB n=500
50 Correct 1 ms 468 KB n=500
51 Correct 1 ms 468 KB n=500
52 Correct 2 ms 468 KB n=500
53 Correct 1 ms 468 KB n=500
54 Correct 2 ms 456 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 2 ms 468 KB n=500
57 Correct 1 ms 456 KB n=500
58 Correct 1 ms 468 KB n=500
59 Correct 5 ms 896 KB n=2000
60 Correct 7 ms 972 KB n=2000
61 Correct 6 ms 852 KB n=2000
62 Correct 5 ms 900 KB n=2000
63 Correct 5 ms 904 KB n=2000
64 Correct 5 ms 852 KB n=2000
65 Correct 5 ms 852 KB n=2000
66 Correct 5 ms 852 KB n=2000
67 Correct 5 ms 852 KB n=2000
68 Correct 6 ms 852 KB n=2000
69 Correct 4 ms 900 KB n=2000
70 Correct 4 ms 848 KB n=2000
71 Correct 4 ms 852 KB n=2000
72 Correct 4 ms 900 KB n=2000
73 Correct 4 ms 848 KB n=2000
74 Correct 4 ms 852 KB n=1844
75 Correct 5 ms 852 KB n=2000
76 Correct 5 ms 852 KB n=2000
77 Correct 5 ms 852 KB n=2000
78 Correct 5 ms 852 KB n=2000
79 Correct 5 ms 852 KB n=2000
80 Correct 6 ms 976 KB n=2000
81 Correct 8 ms 852 KB n=2000
82 Correct 6 ms 852 KB n=2000
83 Correct 6 ms 852 KB n=2000
84 Correct 5 ms 852 KB n=2000
85 Correct 6 ms 852 KB n=2000
86 Correct 6 ms 840 KB n=2000
87 Correct 5 ms 852 KB n=2000
88 Correct 4 ms 980 KB n=2000
89 Correct 5 ms 972 KB n=2000
90 Correct 6 ms 980 KB n=2000
91 Correct 3 ms 840 KB n=2000
92 Correct 1053 ms 63000 KB n=200000
93 Correct 1794 ms 66408 KB n=200000
94 Correct 1883 ms 68496 KB n=200000
95 Correct 999 ms 62744 KB n=200000
96 Correct 1020 ms 62864 KB n=200000
97 Correct 1772 ms 65688 KB n=200000
98 Correct 933 ms 62888 KB n=200000
99 Correct 1148 ms 62852 KB n=200000
100 Correct 962 ms 62936 KB n=200000
101 Correct 1774 ms 69128 KB n=200000
102 Correct 590 ms 63728 KB n=200000
103 Correct 571 ms 63740 KB n=200000
104 Correct 584 ms 63892 KB n=200000
105 Correct 554 ms 64048 KB n=200000
106 Correct 567 ms 64092 KB n=200000
107 Correct 551 ms 64200 KB n=200000
108 Correct 1114 ms 62728 KB n=200000
109 Correct 1094 ms 62612 KB n=200000
110 Correct 1158 ms 62576 KB n=200000
111 Correct 908 ms 62104 KB n=200000
112 Correct 1798 ms 68552 KB n=200000
113 Correct 1798 ms 65348 KB n=200000
114 Correct 940 ms 62176 KB n=200000
115 Correct 1750 ms 64076 KB n=200000
116 Correct 921 ms 62836 KB n=200000
117 Correct 1702 ms 68668 KB n=200000
118 Correct 1730 ms 64788 KB n=200000
119 Correct 916 ms 62704 KB n=200000
120 Correct 1619 ms 68164 KB n=200000
121 Correct 1602 ms 68024 KB n=200000
122 Correct 1688 ms 68404 KB n=200000
123 Correct 545 ms 63856 KB n=200000
124 Correct 337 ms 18824 KB n=25264