답안 #651209

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
651209 2022-10-18T02:49:16 Z peter940324 Birthday gift (IZhO18_treearray) C++17
100 / 100
827 ms 64424 KB
#include<bits/stdc++.h>
using namespace std;

#define double long double
#define IO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define pii pair<int,int>
#define ff first
#define ss second
#define mp make_pair

#define pb push_back
#define pf push_front
#define all(x) x.begin(),x.end()

#define de(x) cout << #x << ' ' << x << '\n';
template<class T> void prt(T l,T r){
  while(l!=r) cout << *l << ' ',l++;
  cout << '\n';
}

const int N=2e5+5,M=1e9+7,INF=1e9;

vector<int> G[N];
int dep[N],fa[N],jump[N];
void dfs(int u){
	if(dep[jump[fa[u]]]-dep[fa[u]]==dep[jump[jump[fa[u]]]]-dep[jump[fa[u]]]) jump[u]=jump[jump[fa[u]]];
	else jump[u]=fa[u];
	for(auto v:G[u]){
		if(v==fa[u]) continue;
		fa[v]=u;
		dep[v]=dep[u]+1;
		dfs(v);
	}
}
int lca(int u,int v){
	if(dep[u]<dep[v]) swap(u,v);
	while(dep[u]>dep[v]){
		if(dep[jump[u]]>=dep[v]) u=jump[u];
		else u=fa[u];
	}
	while(u!=v){
		if(jump[u]!=jump[v]){
			u=jump[u];
			v=jump[v];
		}else{
			u=fa[u];
			v=fa[v];
		}
	}
	return u;
}

/*
multiset<pii> s[4*N];
void ins(int l,int r,int cur,int ind,int val){
	if(r-l<=1){
		s[cur].insert(mp(val,ind));
		return;
	}
	s[cur].insert(mp(val,ind));
	int mid=(l+r)/2;
	if(ind<mid) ins(l,mid,cur*2,ind,val);
	else ins(mid,r,cur*2+1,ind,val);
}
void del(int l,int r,int cur,int ind,int val){
	s[cur].erase(s[cur].lower_bound(mp(val,ind)));
	if(r-l<=1){
		return;
	}
	int mid=(l+r)/2;
	if(ind<mid) del(l,mid,cur*2,ind,val);
	else del(mid,r,cur*2+1,ind,val);
}
int fi(int l,int r,int cur,int ql,int qr,int val){
	if(ql>=r||qr<=l) return 0;
	if(ql<=l&&qr>=r){
		auto it=s[cur].lower_bound(mp(val,0));
		if(it->ff==val) return it->ss;
		else return 0;
	}
	int mid=(l+r)/2;
	return max(fi(l,mid,cur*2,ql,qr,val),fi(mid,r,cur*2+1,ql,qr,val));
}
*/

multiset<int> s[N];

signed main(){
  IO
	int n,m,q;cin >> n >> m >> q;
	for(int i=0,u,v;i<n-1;i++){
		cin >> u >> v;
		G[u].pb(v);
		G[v].pb(u);
	}
	fa[1]=jump[1]=dep[1]=1;
	dfs(1);
	vector<int> a(m+1),lc(m+1);
	for(int i=1;i<=m;i++) cin >> a[i],s[a[i]].insert(i);
	for(int i=1;i<m;i++){
		lc[i]=lca(a[i],a[i+1]);
		//ins(1,m,1,i,lc[i]);
		//ins(1,m,1,i,a[i]);
		s[lc[i]].insert(i);
	}
	int t,pos,l,r,v;
	while(q--){
		cin >> t;
		if(t==1){
			cin >> pos >> v;
			if(pos!=1){
				//del(1,m,1,pos-1,lc[pos-1]);
				s[lc[pos-1]].erase(s[lc[pos-1]].lower_bound(pos-1));
			}
			if(pos!=m){
				//del(1,m,1,pos,lc[pos]);
				//del(1,m,1,pos,a[pos]);
				s[lc[pos]].erase(s[lc[pos]].lower_bound(pos));
			}
			s[a[pos]].erase(s[a[pos]].lower_bound(pos));
			a[pos]=v;
			s[a[pos]].insert(pos);
			if(pos!=1){
				lc[pos-1]=lca(a[pos-1],a[pos]);
				//ins(1,m,1,pos-1,lc[pos-1]);
				s[lc[pos-1]].insert(pos-1);
			}
			if(pos!=m){
				lc[pos]=lca(a[pos],a[pos+1]);
				//ins(1,m,1,pos,lc[pos]);
				//ins(1,m,1,pos,a[pos]);
				s[lc[pos]].insert(pos);
			}
		}else{
			cin >> l >> r >> v;
			auto it=s[v].lower_bound(l);
			int x=0;
			if(it!=s[v].end()&&(*it)<r) x=*it;
			if(a[r]==v) x=r;
			if(x){
				if(a[x]==v) cout << x << ' ' << x << '\n';
				else cout << x << ' ' << x+1 << '\n';
			}else cout << "-1 -1\n";
		}
	}
  return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB n=5
2 Correct 8 ms 14420 KB n=100
3 Correct 8 ms 14420 KB n=100
4 Correct 8 ms 14436 KB n=100
5 Correct 8 ms 14312 KB n=100
6 Correct 8 ms 14420 KB n=100
7 Correct 8 ms 14420 KB n=100
8 Correct 8 ms 14388 KB n=100
9 Correct 8 ms 14412 KB n=100
10 Correct 8 ms 14420 KB n=100
11 Correct 8 ms 14420 KB n=100
12 Correct 8 ms 14420 KB n=100
13 Correct 8 ms 14420 KB n=100
14 Correct 10 ms 14420 KB n=100
15 Correct 11 ms 14420 KB n=100
16 Correct 9 ms 14420 KB n=100
17 Correct 7 ms 14420 KB n=100
18 Correct 8 ms 14420 KB n=100
19 Correct 8 ms 14420 KB n=100
20 Correct 8 ms 14420 KB n=100
21 Correct 8 ms 14420 KB n=100
22 Correct 10 ms 14420 KB n=100
23 Correct 9 ms 14420 KB n=100
24 Correct 9 ms 14348 KB n=100
25 Correct 8 ms 14420 KB n=100
26 Correct 8 ms 14420 KB n=12
27 Correct 8 ms 14420 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB n=5
2 Correct 8 ms 14420 KB n=100
3 Correct 8 ms 14420 KB n=100
4 Correct 8 ms 14436 KB n=100
5 Correct 8 ms 14312 KB n=100
6 Correct 8 ms 14420 KB n=100
7 Correct 8 ms 14420 KB n=100
8 Correct 8 ms 14388 KB n=100
9 Correct 8 ms 14412 KB n=100
10 Correct 8 ms 14420 KB n=100
11 Correct 8 ms 14420 KB n=100
12 Correct 8 ms 14420 KB n=100
13 Correct 8 ms 14420 KB n=100
14 Correct 10 ms 14420 KB n=100
15 Correct 11 ms 14420 KB n=100
16 Correct 9 ms 14420 KB n=100
17 Correct 7 ms 14420 KB n=100
18 Correct 8 ms 14420 KB n=100
19 Correct 8 ms 14420 KB n=100
20 Correct 8 ms 14420 KB n=100
21 Correct 8 ms 14420 KB n=100
22 Correct 10 ms 14420 KB n=100
23 Correct 9 ms 14420 KB n=100
24 Correct 9 ms 14348 KB n=100
25 Correct 8 ms 14420 KB n=100
26 Correct 8 ms 14420 KB n=12
27 Correct 8 ms 14420 KB n=100
28 Correct 9 ms 14420 KB n=500
29 Correct 9 ms 14420 KB n=500
30 Correct 9 ms 14448 KB n=500
31 Correct 8 ms 14452 KB n=500
32 Correct 8 ms 14420 KB n=500
33 Correct 10 ms 14392 KB n=500
34 Correct 8 ms 14420 KB n=500
35 Correct 8 ms 14520 KB n=500
36 Correct 8 ms 14548 KB n=500
37 Correct 9 ms 14420 KB n=500
38 Correct 8 ms 14500 KB n=500
39 Correct 8 ms 14508 KB n=500
40 Correct 8 ms 14420 KB n=500
41 Correct 10 ms 14420 KB n=500
42 Correct 9 ms 14420 KB n=500
43 Correct 9 ms 14420 KB n=500
44 Correct 9 ms 14504 KB n=500
45 Correct 11 ms 14420 KB n=500
46 Correct 9 ms 14528 KB n=500
47 Correct 10 ms 14524 KB n=500
48 Correct 8 ms 14420 KB n=500
49 Correct 8 ms 14420 KB n=500
50 Correct 8 ms 14420 KB n=500
51 Correct 8 ms 14420 KB n=500
52 Correct 9 ms 14420 KB n=500
53 Correct 9 ms 14420 KB n=500
54 Correct 9 ms 14420 KB n=500
55 Correct 7 ms 14420 KB n=278
56 Correct 9 ms 14456 KB n=500
57 Correct 8 ms 14420 KB n=500
58 Correct 9 ms 14420 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB n=5
2 Correct 8 ms 14420 KB n=100
3 Correct 8 ms 14420 KB n=100
4 Correct 8 ms 14436 KB n=100
5 Correct 8 ms 14312 KB n=100
6 Correct 8 ms 14420 KB n=100
7 Correct 8 ms 14420 KB n=100
8 Correct 8 ms 14388 KB n=100
9 Correct 8 ms 14412 KB n=100
10 Correct 8 ms 14420 KB n=100
11 Correct 8 ms 14420 KB n=100
12 Correct 8 ms 14420 KB n=100
13 Correct 8 ms 14420 KB n=100
14 Correct 10 ms 14420 KB n=100
15 Correct 11 ms 14420 KB n=100
16 Correct 9 ms 14420 KB n=100
17 Correct 7 ms 14420 KB n=100
18 Correct 8 ms 14420 KB n=100
19 Correct 8 ms 14420 KB n=100
20 Correct 8 ms 14420 KB n=100
21 Correct 8 ms 14420 KB n=100
22 Correct 10 ms 14420 KB n=100
23 Correct 9 ms 14420 KB n=100
24 Correct 9 ms 14348 KB n=100
25 Correct 8 ms 14420 KB n=100
26 Correct 8 ms 14420 KB n=12
27 Correct 8 ms 14420 KB n=100
28 Correct 9 ms 14420 KB n=500
29 Correct 9 ms 14420 KB n=500
30 Correct 9 ms 14448 KB n=500
31 Correct 8 ms 14452 KB n=500
32 Correct 8 ms 14420 KB n=500
33 Correct 10 ms 14392 KB n=500
34 Correct 8 ms 14420 KB n=500
35 Correct 8 ms 14520 KB n=500
36 Correct 8 ms 14548 KB n=500
37 Correct 9 ms 14420 KB n=500
38 Correct 8 ms 14500 KB n=500
39 Correct 8 ms 14508 KB n=500
40 Correct 8 ms 14420 KB n=500
41 Correct 10 ms 14420 KB n=500
42 Correct 9 ms 14420 KB n=500
43 Correct 9 ms 14420 KB n=500
44 Correct 9 ms 14504 KB n=500
45 Correct 11 ms 14420 KB n=500
46 Correct 9 ms 14528 KB n=500
47 Correct 10 ms 14524 KB n=500
48 Correct 8 ms 14420 KB n=500
49 Correct 8 ms 14420 KB n=500
50 Correct 8 ms 14420 KB n=500
51 Correct 8 ms 14420 KB n=500
52 Correct 9 ms 14420 KB n=500
53 Correct 9 ms 14420 KB n=500
54 Correct 9 ms 14420 KB n=500
55 Correct 7 ms 14420 KB n=278
56 Correct 9 ms 14456 KB n=500
57 Correct 8 ms 14420 KB n=500
58 Correct 9 ms 14420 KB n=500
59 Correct 10 ms 14676 KB n=2000
60 Correct 10 ms 14752 KB n=2000
61 Correct 11 ms 14676 KB n=2000
62 Correct 11 ms 14676 KB n=2000
63 Correct 10 ms 14648 KB n=2000
64 Correct 11 ms 14804 KB n=2000
65 Correct 10 ms 14676 KB n=2000
66 Correct 11 ms 14804 KB n=2000
67 Correct 11 ms 14676 KB n=2000
68 Correct 11 ms 14676 KB n=2000
69 Correct 10 ms 14676 KB n=2000
70 Correct 9 ms 14728 KB n=2000
71 Correct 10 ms 14676 KB n=2000
72 Correct 10 ms 14676 KB n=2000
73 Correct 11 ms 14676 KB n=2000
74 Correct 12 ms 14716 KB n=1844
75 Correct 10 ms 14676 KB n=2000
76 Correct 11 ms 14676 KB n=2000
77 Correct 10 ms 14676 KB n=2000
78 Correct 10 ms 14676 KB n=2000
79 Correct 12 ms 14676 KB n=2000
80 Correct 10 ms 14820 KB n=2000
81 Correct 10 ms 14676 KB n=2000
82 Correct 13 ms 14676 KB n=2000
83 Correct 12 ms 14800 KB n=2000
84 Correct 10 ms 14736 KB n=2000
85 Correct 11 ms 14764 KB n=2000
86 Correct 12 ms 14636 KB n=2000
87 Correct 10 ms 14732 KB n=2000
88 Correct 10 ms 14804 KB n=2000
89 Correct 12 ms 14848 KB n=2000
90 Correct 13 ms 14804 KB n=2000
91 Correct 10 ms 14676 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB n=5
2 Correct 8 ms 14420 KB n=100
3 Correct 8 ms 14420 KB n=100
4 Correct 8 ms 14436 KB n=100
5 Correct 8 ms 14312 KB n=100
6 Correct 8 ms 14420 KB n=100
7 Correct 8 ms 14420 KB n=100
8 Correct 8 ms 14388 KB n=100
9 Correct 8 ms 14412 KB n=100
10 Correct 8 ms 14420 KB n=100
11 Correct 8 ms 14420 KB n=100
12 Correct 8 ms 14420 KB n=100
13 Correct 8 ms 14420 KB n=100
14 Correct 10 ms 14420 KB n=100
15 Correct 11 ms 14420 KB n=100
16 Correct 9 ms 14420 KB n=100
17 Correct 7 ms 14420 KB n=100
18 Correct 8 ms 14420 KB n=100
19 Correct 8 ms 14420 KB n=100
20 Correct 8 ms 14420 KB n=100
21 Correct 8 ms 14420 KB n=100
22 Correct 10 ms 14420 KB n=100
23 Correct 9 ms 14420 KB n=100
24 Correct 9 ms 14348 KB n=100
25 Correct 8 ms 14420 KB n=100
26 Correct 8 ms 14420 KB n=12
27 Correct 8 ms 14420 KB n=100
28 Correct 9 ms 14420 KB n=500
29 Correct 9 ms 14420 KB n=500
30 Correct 9 ms 14448 KB n=500
31 Correct 8 ms 14452 KB n=500
32 Correct 8 ms 14420 KB n=500
33 Correct 10 ms 14392 KB n=500
34 Correct 8 ms 14420 KB n=500
35 Correct 8 ms 14520 KB n=500
36 Correct 8 ms 14548 KB n=500
37 Correct 9 ms 14420 KB n=500
38 Correct 8 ms 14500 KB n=500
39 Correct 8 ms 14508 KB n=500
40 Correct 8 ms 14420 KB n=500
41 Correct 10 ms 14420 KB n=500
42 Correct 9 ms 14420 KB n=500
43 Correct 9 ms 14420 KB n=500
44 Correct 9 ms 14504 KB n=500
45 Correct 11 ms 14420 KB n=500
46 Correct 9 ms 14528 KB n=500
47 Correct 10 ms 14524 KB n=500
48 Correct 8 ms 14420 KB n=500
49 Correct 8 ms 14420 KB n=500
50 Correct 8 ms 14420 KB n=500
51 Correct 8 ms 14420 KB n=500
52 Correct 9 ms 14420 KB n=500
53 Correct 9 ms 14420 KB n=500
54 Correct 9 ms 14420 KB n=500
55 Correct 7 ms 14420 KB n=278
56 Correct 9 ms 14456 KB n=500
57 Correct 8 ms 14420 KB n=500
58 Correct 9 ms 14420 KB n=500
59 Correct 10 ms 14676 KB n=2000
60 Correct 10 ms 14752 KB n=2000
61 Correct 11 ms 14676 KB n=2000
62 Correct 11 ms 14676 KB n=2000
63 Correct 10 ms 14648 KB n=2000
64 Correct 11 ms 14804 KB n=2000
65 Correct 10 ms 14676 KB n=2000
66 Correct 11 ms 14804 KB n=2000
67 Correct 11 ms 14676 KB n=2000
68 Correct 11 ms 14676 KB n=2000
69 Correct 10 ms 14676 KB n=2000
70 Correct 9 ms 14728 KB n=2000
71 Correct 10 ms 14676 KB n=2000
72 Correct 10 ms 14676 KB n=2000
73 Correct 11 ms 14676 KB n=2000
74 Correct 12 ms 14716 KB n=1844
75 Correct 10 ms 14676 KB n=2000
76 Correct 11 ms 14676 KB n=2000
77 Correct 10 ms 14676 KB n=2000
78 Correct 10 ms 14676 KB n=2000
79 Correct 12 ms 14676 KB n=2000
80 Correct 10 ms 14820 KB n=2000
81 Correct 10 ms 14676 KB n=2000
82 Correct 13 ms 14676 KB n=2000
83 Correct 12 ms 14800 KB n=2000
84 Correct 10 ms 14736 KB n=2000
85 Correct 11 ms 14764 KB n=2000
86 Correct 12 ms 14636 KB n=2000
87 Correct 10 ms 14732 KB n=2000
88 Correct 10 ms 14804 KB n=2000
89 Correct 12 ms 14848 KB n=2000
90 Correct 13 ms 14804 KB n=2000
91 Correct 10 ms 14676 KB n=2000
92 Correct 545 ms 51712 KB n=200000
93 Correct 726 ms 58304 KB n=200000
94 Correct 637 ms 62764 KB n=200000
95 Correct 522 ms 51612 KB n=200000
96 Correct 530 ms 51596 KB n=200000
97 Correct 704 ms 57036 KB n=200000
98 Correct 549 ms 51648 KB n=200000
99 Correct 671 ms 51736 KB n=200000
100 Correct 518 ms 51660 KB n=200000
101 Correct 648 ms 64424 KB n=200000
102 Correct 339 ms 52888 KB n=200000
103 Correct 341 ms 52864 KB n=200000
104 Correct 358 ms 52812 KB n=200000
105 Correct 319 ms 53300 KB n=200000
106 Correct 353 ms 53192 KB n=200000
107 Correct 347 ms 53196 KB n=200000
108 Correct 635 ms 51684 KB n=200000
109 Correct 655 ms 51660 KB n=200000
110 Correct 648 ms 51776 KB n=200000
111 Correct 710 ms 51228 KB n=200000
112 Correct 627 ms 63068 KB n=200000
113 Correct 643 ms 57060 KB n=200000
114 Correct 550 ms 51180 KB n=200000
115 Correct 827 ms 54348 KB n=200000
116 Correct 474 ms 51804 KB n=200000
117 Correct 641 ms 63684 KB n=200000
118 Correct 713 ms 55644 KB n=200000
119 Correct 481 ms 51868 KB n=200000
120 Correct 630 ms 63472 KB n=200000
121 Correct 600 ms 63448 KB n=200000
122 Correct 585 ms 63752 KB n=200000
123 Correct 342 ms 53180 KB n=200000
124 Correct 154 ms 28208 KB n=25264