답안 #651200

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
651200 2022-10-18T02:35:26 Z peter940324 Birthday gift (IZhO18_treearray) C++17
56 / 100
1074 ms 262144 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));
}

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];
	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]);
	}
	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]);
			}
			if(pos!=m){
				del(1,m,1,pos,lc[pos]);
				del(1,m,1,pos,a[pos]);
			}
			a[pos]=v;
			if(pos!=1){
				lc[pos-1]=lca(a[pos-1],a[pos]);
				ins(1,m,1,pos-1,lc[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]);
			}
		}else{
			cin >> l >> r >> v;
			int x=fi(1,m,1,l,r,v);
			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 24 ms 42580 KB n=5
2 Correct 27 ms 42632 KB n=100
3 Correct 34 ms 42668 KB n=100
4 Correct 26 ms 42580 KB n=100
5 Correct 28 ms 42684 KB n=100
6 Correct 28 ms 42652 KB n=100
7 Correct 27 ms 42580 KB n=100
8 Correct 27 ms 42592 KB n=100
9 Correct 23 ms 42600 KB n=100
10 Correct 23 ms 42684 KB n=100
11 Correct 24 ms 42640 KB n=100
12 Correct 23 ms 42608 KB n=100
13 Correct 27 ms 42572 KB n=100
14 Correct 22 ms 42580 KB n=100
15 Correct 24 ms 42664 KB n=100
16 Correct 22 ms 42660 KB n=100
17 Correct 26 ms 42580 KB n=100
18 Correct 24 ms 42656 KB n=100
19 Correct 29 ms 42580 KB n=100
20 Correct 21 ms 42580 KB n=100
21 Correct 24 ms 42580 KB n=100
22 Correct 22 ms 42656 KB n=100
23 Correct 23 ms 42672 KB n=100
24 Correct 27 ms 42596 KB n=100
25 Correct 23 ms 42668 KB n=100
26 Correct 23 ms 42572 KB n=12
27 Correct 22 ms 42620 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 42580 KB n=5
2 Correct 27 ms 42632 KB n=100
3 Correct 34 ms 42668 KB n=100
4 Correct 26 ms 42580 KB n=100
5 Correct 28 ms 42684 KB n=100
6 Correct 28 ms 42652 KB n=100
7 Correct 27 ms 42580 KB n=100
8 Correct 27 ms 42592 KB n=100
9 Correct 23 ms 42600 KB n=100
10 Correct 23 ms 42684 KB n=100
11 Correct 24 ms 42640 KB n=100
12 Correct 23 ms 42608 KB n=100
13 Correct 27 ms 42572 KB n=100
14 Correct 22 ms 42580 KB n=100
15 Correct 24 ms 42664 KB n=100
16 Correct 22 ms 42660 KB n=100
17 Correct 26 ms 42580 KB n=100
18 Correct 24 ms 42656 KB n=100
19 Correct 29 ms 42580 KB n=100
20 Correct 21 ms 42580 KB n=100
21 Correct 24 ms 42580 KB n=100
22 Correct 22 ms 42656 KB n=100
23 Correct 23 ms 42672 KB n=100
24 Correct 27 ms 42596 KB n=100
25 Correct 23 ms 42668 KB n=100
26 Correct 23 ms 42572 KB n=12
27 Correct 22 ms 42620 KB n=100
28 Correct 25 ms 43028 KB n=500
29 Correct 26 ms 43116 KB n=500
30 Correct 26 ms 43120 KB n=500
31 Correct 26 ms 43112 KB n=500
32 Correct 25 ms 42992 KB n=500
33 Correct 26 ms 43048 KB n=500
34 Correct 24 ms 43124 KB n=500
35 Correct 28 ms 43092 KB n=500
36 Correct 23 ms 43036 KB n=500
37 Correct 24 ms 43012 KB n=500
38 Correct 24 ms 43036 KB n=500
39 Correct 22 ms 43088 KB n=500
40 Correct 22 ms 43084 KB n=500
41 Correct 24 ms 43028 KB n=500
42 Correct 26 ms 43004 KB n=500
43 Correct 32 ms 42988 KB n=500
44 Correct 28 ms 43108 KB n=500
45 Correct 25 ms 43092 KB n=500
46 Correct 24 ms 43092 KB n=500
47 Correct 26 ms 43084 KB n=500
48 Correct 26 ms 43000 KB n=500
49 Correct 30 ms 43084 KB n=500
50 Correct 25 ms 43092 KB n=500
51 Correct 26 ms 43072 KB n=500
52 Correct 24 ms 43092 KB n=500
53 Correct 25 ms 43080 KB n=500
54 Correct 27 ms 43140 KB n=500
55 Correct 24 ms 43052 KB n=278
56 Correct 27 ms 43036 KB n=500
57 Correct 27 ms 43112 KB n=500
58 Correct 23 ms 43092 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 42580 KB n=5
2 Correct 27 ms 42632 KB n=100
3 Correct 34 ms 42668 KB n=100
4 Correct 26 ms 42580 KB n=100
5 Correct 28 ms 42684 KB n=100
6 Correct 28 ms 42652 KB n=100
7 Correct 27 ms 42580 KB n=100
8 Correct 27 ms 42592 KB n=100
9 Correct 23 ms 42600 KB n=100
10 Correct 23 ms 42684 KB n=100
11 Correct 24 ms 42640 KB n=100
12 Correct 23 ms 42608 KB n=100
13 Correct 27 ms 42572 KB n=100
14 Correct 22 ms 42580 KB n=100
15 Correct 24 ms 42664 KB n=100
16 Correct 22 ms 42660 KB n=100
17 Correct 26 ms 42580 KB n=100
18 Correct 24 ms 42656 KB n=100
19 Correct 29 ms 42580 KB n=100
20 Correct 21 ms 42580 KB n=100
21 Correct 24 ms 42580 KB n=100
22 Correct 22 ms 42656 KB n=100
23 Correct 23 ms 42672 KB n=100
24 Correct 27 ms 42596 KB n=100
25 Correct 23 ms 42668 KB n=100
26 Correct 23 ms 42572 KB n=12
27 Correct 22 ms 42620 KB n=100
28 Correct 25 ms 43028 KB n=500
29 Correct 26 ms 43116 KB n=500
30 Correct 26 ms 43120 KB n=500
31 Correct 26 ms 43112 KB n=500
32 Correct 25 ms 42992 KB n=500
33 Correct 26 ms 43048 KB n=500
34 Correct 24 ms 43124 KB n=500
35 Correct 28 ms 43092 KB n=500
36 Correct 23 ms 43036 KB n=500
37 Correct 24 ms 43012 KB n=500
38 Correct 24 ms 43036 KB n=500
39 Correct 22 ms 43088 KB n=500
40 Correct 22 ms 43084 KB n=500
41 Correct 24 ms 43028 KB n=500
42 Correct 26 ms 43004 KB n=500
43 Correct 32 ms 42988 KB n=500
44 Correct 28 ms 43108 KB n=500
45 Correct 25 ms 43092 KB n=500
46 Correct 24 ms 43092 KB n=500
47 Correct 26 ms 43084 KB n=500
48 Correct 26 ms 43000 KB n=500
49 Correct 30 ms 43084 KB n=500
50 Correct 25 ms 43092 KB n=500
51 Correct 26 ms 43072 KB n=500
52 Correct 24 ms 43092 KB n=500
53 Correct 25 ms 43080 KB n=500
54 Correct 27 ms 43140 KB n=500
55 Correct 24 ms 43052 KB n=278
56 Correct 27 ms 43036 KB n=500
57 Correct 27 ms 43112 KB n=500
58 Correct 23 ms 43092 KB n=500
59 Correct 39 ms 44948 KB n=2000
60 Correct 46 ms 45116 KB n=2000
61 Correct 45 ms 45076 KB n=2000
62 Correct 43 ms 45020 KB n=2000
63 Correct 41 ms 45032 KB n=2000
64 Correct 42 ms 45004 KB n=2000
65 Correct 39 ms 44928 KB n=2000
66 Correct 42 ms 45004 KB n=2000
67 Correct 41 ms 45012 KB n=2000
68 Correct 42 ms 44992 KB n=2000
69 Correct 33 ms 44992 KB n=2000
70 Correct 36 ms 45004 KB n=2000
71 Correct 34 ms 45016 KB n=2000
72 Correct 34 ms 44932 KB n=2000
73 Correct 36 ms 44908 KB n=2000
74 Correct 48 ms 44932 KB n=1844
75 Correct 43 ms 44904 KB n=2000
76 Correct 54 ms 44980 KB n=2000
77 Correct 41 ms 45004 KB n=2000
78 Correct 41 ms 45004 KB n=2000
79 Correct 40 ms 44944 KB n=2000
80 Correct 44 ms 45096 KB n=2000
81 Correct 42 ms 45044 KB n=2000
82 Correct 42 ms 45004 KB n=2000
83 Correct 43 ms 45012 KB n=2000
84 Correct 53 ms 44952 KB n=2000
85 Correct 42 ms 45044 KB n=2000
86 Correct 47 ms 45004 KB n=2000
87 Correct 55 ms 44988 KB n=2000
88 Correct 42 ms 45016 KB n=2000
89 Correct 43 ms 45016 KB n=2000
90 Correct 43 ms 45080 KB n=2000
91 Correct 34 ms 45020 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 42580 KB n=5
2 Correct 27 ms 42632 KB n=100
3 Correct 34 ms 42668 KB n=100
4 Correct 26 ms 42580 KB n=100
5 Correct 28 ms 42684 KB n=100
6 Correct 28 ms 42652 KB n=100
7 Correct 27 ms 42580 KB n=100
8 Correct 27 ms 42592 KB n=100
9 Correct 23 ms 42600 KB n=100
10 Correct 23 ms 42684 KB n=100
11 Correct 24 ms 42640 KB n=100
12 Correct 23 ms 42608 KB n=100
13 Correct 27 ms 42572 KB n=100
14 Correct 22 ms 42580 KB n=100
15 Correct 24 ms 42664 KB n=100
16 Correct 22 ms 42660 KB n=100
17 Correct 26 ms 42580 KB n=100
18 Correct 24 ms 42656 KB n=100
19 Correct 29 ms 42580 KB n=100
20 Correct 21 ms 42580 KB n=100
21 Correct 24 ms 42580 KB n=100
22 Correct 22 ms 42656 KB n=100
23 Correct 23 ms 42672 KB n=100
24 Correct 27 ms 42596 KB n=100
25 Correct 23 ms 42668 KB n=100
26 Correct 23 ms 42572 KB n=12
27 Correct 22 ms 42620 KB n=100
28 Correct 25 ms 43028 KB n=500
29 Correct 26 ms 43116 KB n=500
30 Correct 26 ms 43120 KB n=500
31 Correct 26 ms 43112 KB n=500
32 Correct 25 ms 42992 KB n=500
33 Correct 26 ms 43048 KB n=500
34 Correct 24 ms 43124 KB n=500
35 Correct 28 ms 43092 KB n=500
36 Correct 23 ms 43036 KB n=500
37 Correct 24 ms 43012 KB n=500
38 Correct 24 ms 43036 KB n=500
39 Correct 22 ms 43088 KB n=500
40 Correct 22 ms 43084 KB n=500
41 Correct 24 ms 43028 KB n=500
42 Correct 26 ms 43004 KB n=500
43 Correct 32 ms 42988 KB n=500
44 Correct 28 ms 43108 KB n=500
45 Correct 25 ms 43092 KB n=500
46 Correct 24 ms 43092 KB n=500
47 Correct 26 ms 43084 KB n=500
48 Correct 26 ms 43000 KB n=500
49 Correct 30 ms 43084 KB n=500
50 Correct 25 ms 43092 KB n=500
51 Correct 26 ms 43072 KB n=500
52 Correct 24 ms 43092 KB n=500
53 Correct 25 ms 43080 KB n=500
54 Correct 27 ms 43140 KB n=500
55 Correct 24 ms 43052 KB n=278
56 Correct 27 ms 43036 KB n=500
57 Correct 27 ms 43112 KB n=500
58 Correct 23 ms 43092 KB n=500
59 Correct 39 ms 44948 KB n=2000
60 Correct 46 ms 45116 KB n=2000
61 Correct 45 ms 45076 KB n=2000
62 Correct 43 ms 45020 KB n=2000
63 Correct 41 ms 45032 KB n=2000
64 Correct 42 ms 45004 KB n=2000
65 Correct 39 ms 44928 KB n=2000
66 Correct 42 ms 45004 KB n=2000
67 Correct 41 ms 45012 KB n=2000
68 Correct 42 ms 44992 KB n=2000
69 Correct 33 ms 44992 KB n=2000
70 Correct 36 ms 45004 KB n=2000
71 Correct 34 ms 45016 KB n=2000
72 Correct 34 ms 44932 KB n=2000
73 Correct 36 ms 44908 KB n=2000
74 Correct 48 ms 44932 KB n=1844
75 Correct 43 ms 44904 KB n=2000
76 Correct 54 ms 44980 KB n=2000
77 Correct 41 ms 45004 KB n=2000
78 Correct 41 ms 45004 KB n=2000
79 Correct 40 ms 44944 KB n=2000
80 Correct 44 ms 45096 KB n=2000
81 Correct 42 ms 45044 KB n=2000
82 Correct 42 ms 45004 KB n=2000
83 Correct 43 ms 45012 KB n=2000
84 Correct 53 ms 44952 KB n=2000
85 Correct 42 ms 45044 KB n=2000
86 Correct 47 ms 45004 KB n=2000
87 Correct 55 ms 44988 KB n=2000
88 Correct 42 ms 45016 KB n=2000
89 Correct 43 ms 45016 KB n=2000
90 Correct 43 ms 45080 KB n=2000
91 Correct 34 ms 45020 KB n=2000
92 Runtime error 1074 ms 262144 KB Execution killed with signal 9
93 Halted 0 ms 0 KB -