Submission #887381

# Submission time Handle Problem Language Result Execution time Memory
887381 2023-12-14T11:17:56 Z pcc Birthday gift (IZhO18_treearray) C++14
56 / 100
2429 ms 262144 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
#define mid ((l+r)>>1)

const int mxn = 4e5+10;
int n,m,q;
pii dfn[mxn];
pii sparse[mxn][20];
int dep[mxn];
vector<int> tree[mxn];
vector<int> eul;
int arr[mxn];
int ppp = 0;
multiset<pair<int,pii>> segtree[mxn*3];

void dfs(int now,int par){
	dfn[now].fs = eul.size();
	eul.push_back(now);
	for(auto nxt:tree[now]){
		if(nxt == par)continue;
		dep[nxt] = dep[now]+1;
		dfs(nxt,now);
		eul.push_back(now);
	}
	dfn[now].sc = eul.size();
	eul.push_back(now);
}

void build(){
	for(int i = 0;i<eul.size();i++){
		sparse[i][0] = make_pair(dep[eul[i]],eul[i]);
	}
	for(int i = 1;i<20;i++){
		for(int j = 0;j+(1<<i)<=eul.size();j++)sparse[j][i] = min(sparse[j][i-1],sparse[j+(1<<(i-1))][i-1]);
	}
	vector<int>().swap(eul);
	return;
}
int anc(int l,int r){
	l = dfn[l].fs;
	r = dfn[r].fs;
	if(l>r)swap(l,r);
	int d = r-l+1;
	int h = __lg(d);
	return min(sparse[l][h],sparse[r-(1<<h)+1][h]).sc;
}

void maketree(int now,int l,int r){
	if(l == r){
		segtree[now].insert(make_pair(arr[l],make_pair(l,l)));
		return;
	}
	maketree(now*2+1,l,mid);
	maketree(now*2+2,mid+1,r);
	for(auto &i:segtree[now*2+1])segtree[now].insert(i);
	for(auto &i:segtree[now*2+2])segtree[now].insert(i);
	segtree[now].insert({anc(arr[mid],arr[mid+1]),{mid,mid+1}});
	//cout<<now<<":"<<l<<' '<<r<<":";for(auto &i:segtree[now])cout<<i.fs<<',';cout<<endl;
	return;
}
void modify(int now,int l,int r,int p,int v){
	segtree[now].erase(segtree[now].find({arr[p],{p,p}}));
	segtree[now].insert({v,{p,p}});
	if(p != l){
		segtree[now].erase(segtree[now].find({anc(arr[p-1],arr[p]),{p-1,p}}));
		segtree[now].insert({anc(arr[p-1],v),{p-1,p}});
	}
	if(p != r){
		segtree[now].erase(segtree[now].find({anc(arr[p],arr[p+1]),{p,p+1}}));
		segtree[now].insert({anc(arr[p+1],v),{p,p+1}});
	}
	if(l == r)return;
	if(mid>=p)modify(now*2+1,l,mid,p,v);
	else modify(now*2+2,mid+1,r,p,v);
	return;
}

pii getval(int now,int l,int r,int s,int e,int tar){
	if(l>=s&&e>=r){
		auto it = segtree[now].lower_bound({tar,{0,0}});
		if(it != segtree[now].end()&&it->fs == tar){
			return it->sc;
		}
		else return {-1,-1};
	}
	if(mid>=e)return getval(now*2+1,l,mid,s,e,tar);
	else if(mid<s)return getval(now*2+2,mid+1,r,s,e,tar);
	else{
		if(anc(arr[mid],arr[mid+1]) == tar)return {mid,mid+1};
		auto re = getval(now*2+1,l,mid,s,e,tar);
		if(re.fs != -1)return re;
		else return getval(now*2+2,mid+1,r,s,e,tar);
	}
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>m>>q;
	for(int i = 1;i<n;i++){
		int a,b;
		cin>>a>>b;
		tree[a].push_back(b);
		tree[b].push_back(a);
	}
	for(int i = 1;i<=m;i++){
		cin>>arr[i];
	}
	dfs(1,1);
	build();
	maketree(0,1,m);
	//for(auto &i:segtree[0])cout<<i.fs<<' '<<i.sc.fs<<','<<i.sc.sc<<';';
	while(q--){
		int tp;
		cin>>tp;
		if(tp == 1){
			int p,v;
			cin>>p>>v;
			modify(0,1,m,p,v);
			arr[p] = v;
		}
		else{
			int l,r,v;
			cin>>l>>r>>v;
			auto re = getval(0,1,m,l,r,v);
			cout<<re.fs<<' '<<re.sc<<'\n';
		}
	}
	return 0;
}

Compilation message

treearray.cpp: In function 'void build()':
treearray.cpp:37:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |  for(int i = 0;i<eul.size();i++){
      |                ~^~~~~~~~~~~
treearray.cpp:41:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |   for(int j = 0;j+(1<<i)<=eul.size();j++)sparse[j][i] = min(sparse[j][i-1],sparse[j+(1<<(i-1))][i-1]);
      |                 ~~~~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 19 ms 72280 KB n=5
2 Correct 14 ms 72540 KB n=100
3 Correct 14 ms 72540 KB n=100
4 Correct 16 ms 72536 KB n=100
5 Correct 15 ms 72540 KB n=100
6 Correct 14 ms 72536 KB n=100
7 Correct 14 ms 72540 KB n=100
8 Correct 14 ms 72540 KB n=100
9 Correct 14 ms 72540 KB n=100
10 Correct 15 ms 72540 KB n=100
11 Correct 16 ms 72536 KB n=100
12 Correct 14 ms 72536 KB n=100
13 Correct 15 ms 72540 KB n=100
14 Correct 14 ms 72540 KB n=100
15 Correct 14 ms 72280 KB n=100
16 Correct 14 ms 72540 KB n=100
17 Correct 14 ms 72540 KB n=100
18 Correct 14 ms 72540 KB n=100
19 Correct 14 ms 72540 KB n=100
20 Correct 14 ms 72536 KB n=100
21 Correct 14 ms 72540 KB n=100
22 Correct 15 ms 72360 KB n=100
23 Correct 14 ms 72540 KB n=100
24 Correct 15 ms 72540 KB n=100
25 Correct 15 ms 72536 KB n=100
26 Correct 14 ms 72284 KB n=12
27 Correct 14 ms 72540 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 19 ms 72280 KB n=5
2 Correct 14 ms 72540 KB n=100
3 Correct 14 ms 72540 KB n=100
4 Correct 16 ms 72536 KB n=100
5 Correct 15 ms 72540 KB n=100
6 Correct 14 ms 72536 KB n=100
7 Correct 14 ms 72540 KB n=100
8 Correct 14 ms 72540 KB n=100
9 Correct 14 ms 72540 KB n=100
10 Correct 15 ms 72540 KB n=100
11 Correct 16 ms 72536 KB n=100
12 Correct 14 ms 72536 KB n=100
13 Correct 15 ms 72540 KB n=100
14 Correct 14 ms 72540 KB n=100
15 Correct 14 ms 72280 KB n=100
16 Correct 14 ms 72540 KB n=100
17 Correct 14 ms 72540 KB n=100
18 Correct 14 ms 72540 KB n=100
19 Correct 14 ms 72540 KB n=100
20 Correct 14 ms 72536 KB n=100
21 Correct 14 ms 72540 KB n=100
22 Correct 15 ms 72360 KB n=100
23 Correct 14 ms 72540 KB n=100
24 Correct 15 ms 72540 KB n=100
25 Correct 15 ms 72536 KB n=100
26 Correct 14 ms 72284 KB n=12
27 Correct 14 ms 72540 KB n=100
28 Correct 17 ms 72792 KB n=500
29 Correct 17 ms 73052 KB n=500
30 Correct 17 ms 72792 KB n=500
31 Correct 18 ms 73052 KB n=500
32 Correct 16 ms 72796 KB n=500
33 Correct 17 ms 72796 KB n=500
34 Correct 16 ms 72796 KB n=500
35 Correct 17 ms 73052 KB n=500
36 Correct 15 ms 72792 KB n=500
37 Correct 15 ms 73052 KB n=500
38 Correct 15 ms 72796 KB n=500
39 Correct 18 ms 72796 KB n=500
40 Correct 16 ms 72900 KB n=500
41 Correct 15 ms 72796 KB n=500
42 Correct 17 ms 72796 KB n=500
43 Correct 16 ms 73040 KB n=500
44 Correct 17 ms 72796 KB n=500
45 Correct 17 ms 72796 KB n=500
46 Correct 17 ms 73060 KB n=500
47 Correct 18 ms 73048 KB n=500
48 Correct 18 ms 72916 KB n=500
49 Correct 17 ms 73052 KB n=500
50 Correct 16 ms 72796 KB n=500
51 Correct 17 ms 73052 KB n=500
52 Correct 17 ms 73052 KB n=500
53 Correct 17 ms 72792 KB n=500
54 Correct 20 ms 73264 KB n=500
55 Correct 15 ms 72796 KB n=278
56 Correct 16 ms 73048 KB n=500
57 Correct 18 ms 73068 KB n=500
58 Correct 15 ms 72796 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 19 ms 72280 KB n=5
2 Correct 14 ms 72540 KB n=100
3 Correct 14 ms 72540 KB n=100
4 Correct 16 ms 72536 KB n=100
5 Correct 15 ms 72540 KB n=100
6 Correct 14 ms 72536 KB n=100
7 Correct 14 ms 72540 KB n=100
8 Correct 14 ms 72540 KB n=100
9 Correct 14 ms 72540 KB n=100
10 Correct 15 ms 72540 KB n=100
11 Correct 16 ms 72536 KB n=100
12 Correct 14 ms 72536 KB n=100
13 Correct 15 ms 72540 KB n=100
14 Correct 14 ms 72540 KB n=100
15 Correct 14 ms 72280 KB n=100
16 Correct 14 ms 72540 KB n=100
17 Correct 14 ms 72540 KB n=100
18 Correct 14 ms 72540 KB n=100
19 Correct 14 ms 72540 KB n=100
20 Correct 14 ms 72536 KB n=100
21 Correct 14 ms 72540 KB n=100
22 Correct 15 ms 72360 KB n=100
23 Correct 14 ms 72540 KB n=100
24 Correct 15 ms 72540 KB n=100
25 Correct 15 ms 72536 KB n=100
26 Correct 14 ms 72284 KB n=12
27 Correct 14 ms 72540 KB n=100
28 Correct 17 ms 72792 KB n=500
29 Correct 17 ms 73052 KB n=500
30 Correct 17 ms 72792 KB n=500
31 Correct 18 ms 73052 KB n=500
32 Correct 16 ms 72796 KB n=500
33 Correct 17 ms 72796 KB n=500
34 Correct 16 ms 72796 KB n=500
35 Correct 17 ms 73052 KB n=500
36 Correct 15 ms 72792 KB n=500
37 Correct 15 ms 73052 KB n=500
38 Correct 15 ms 72796 KB n=500
39 Correct 18 ms 72796 KB n=500
40 Correct 16 ms 72900 KB n=500
41 Correct 15 ms 72796 KB n=500
42 Correct 17 ms 72796 KB n=500
43 Correct 16 ms 73040 KB n=500
44 Correct 17 ms 72796 KB n=500
45 Correct 17 ms 72796 KB n=500
46 Correct 17 ms 73060 KB n=500
47 Correct 18 ms 73048 KB n=500
48 Correct 18 ms 72916 KB n=500
49 Correct 17 ms 73052 KB n=500
50 Correct 16 ms 72796 KB n=500
51 Correct 17 ms 73052 KB n=500
52 Correct 17 ms 73052 KB n=500
53 Correct 17 ms 72792 KB n=500
54 Correct 20 ms 73264 KB n=500
55 Correct 15 ms 72796 KB n=278
56 Correct 16 ms 73048 KB n=500
57 Correct 18 ms 73068 KB n=500
58 Correct 15 ms 72796 KB n=500
59 Correct 27 ms 75100 KB n=2000
60 Correct 29 ms 75356 KB n=2000
61 Correct 31 ms 75100 KB n=2000
62 Correct 29 ms 75096 KB n=2000
63 Correct 27 ms 75096 KB n=2000
64 Correct 30 ms 75208 KB n=2000
65 Correct 27 ms 75100 KB n=2000
66 Correct 29 ms 75356 KB n=2000
67 Correct 28 ms 75100 KB n=2000
68 Correct 32 ms 75588 KB n=2000
69 Correct 21 ms 75100 KB n=2000
70 Correct 20 ms 75100 KB n=2000
71 Correct 21 ms 75100 KB n=2000
72 Correct 21 ms 75096 KB n=2000
73 Correct 22 ms 75192 KB n=2000
74 Correct 25 ms 75100 KB n=1844
75 Correct 21 ms 75096 KB n=2000
76 Correct 28 ms 75096 KB n=2000
77 Correct 29 ms 75100 KB n=2000
78 Correct 27 ms 75096 KB n=2000
79 Correct 31 ms 75096 KB n=2000
80 Correct 36 ms 75360 KB n=2000
81 Correct 33 ms 75296 KB n=2000
82 Correct 27 ms 75100 KB n=2000
83 Correct 29 ms 75356 KB n=2000
84 Correct 29 ms 75352 KB n=2000
85 Correct 33 ms 75100 KB n=2000
86 Correct 34 ms 75484 KB n=2000
87 Correct 27 ms 75096 KB n=2000
88 Correct 31 ms 75356 KB n=2000
89 Correct 28 ms 75352 KB n=2000
90 Correct 28 ms 75352 KB n=2000
91 Correct 21 ms 75140 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 19 ms 72280 KB n=5
2 Correct 14 ms 72540 KB n=100
3 Correct 14 ms 72540 KB n=100
4 Correct 16 ms 72536 KB n=100
5 Correct 15 ms 72540 KB n=100
6 Correct 14 ms 72536 KB n=100
7 Correct 14 ms 72540 KB n=100
8 Correct 14 ms 72540 KB n=100
9 Correct 14 ms 72540 KB n=100
10 Correct 15 ms 72540 KB n=100
11 Correct 16 ms 72536 KB n=100
12 Correct 14 ms 72536 KB n=100
13 Correct 15 ms 72540 KB n=100
14 Correct 14 ms 72540 KB n=100
15 Correct 14 ms 72280 KB n=100
16 Correct 14 ms 72540 KB n=100
17 Correct 14 ms 72540 KB n=100
18 Correct 14 ms 72540 KB n=100
19 Correct 14 ms 72540 KB n=100
20 Correct 14 ms 72536 KB n=100
21 Correct 14 ms 72540 KB n=100
22 Correct 15 ms 72360 KB n=100
23 Correct 14 ms 72540 KB n=100
24 Correct 15 ms 72540 KB n=100
25 Correct 15 ms 72536 KB n=100
26 Correct 14 ms 72284 KB n=12
27 Correct 14 ms 72540 KB n=100
28 Correct 17 ms 72792 KB n=500
29 Correct 17 ms 73052 KB n=500
30 Correct 17 ms 72792 KB n=500
31 Correct 18 ms 73052 KB n=500
32 Correct 16 ms 72796 KB n=500
33 Correct 17 ms 72796 KB n=500
34 Correct 16 ms 72796 KB n=500
35 Correct 17 ms 73052 KB n=500
36 Correct 15 ms 72792 KB n=500
37 Correct 15 ms 73052 KB n=500
38 Correct 15 ms 72796 KB n=500
39 Correct 18 ms 72796 KB n=500
40 Correct 16 ms 72900 KB n=500
41 Correct 15 ms 72796 KB n=500
42 Correct 17 ms 72796 KB n=500
43 Correct 16 ms 73040 KB n=500
44 Correct 17 ms 72796 KB n=500
45 Correct 17 ms 72796 KB n=500
46 Correct 17 ms 73060 KB n=500
47 Correct 18 ms 73048 KB n=500
48 Correct 18 ms 72916 KB n=500
49 Correct 17 ms 73052 KB n=500
50 Correct 16 ms 72796 KB n=500
51 Correct 17 ms 73052 KB n=500
52 Correct 17 ms 73052 KB n=500
53 Correct 17 ms 72792 KB n=500
54 Correct 20 ms 73264 KB n=500
55 Correct 15 ms 72796 KB n=278
56 Correct 16 ms 73048 KB n=500
57 Correct 18 ms 73068 KB n=500
58 Correct 15 ms 72796 KB n=500
59 Correct 27 ms 75100 KB n=2000
60 Correct 29 ms 75356 KB n=2000
61 Correct 31 ms 75100 KB n=2000
62 Correct 29 ms 75096 KB n=2000
63 Correct 27 ms 75096 KB n=2000
64 Correct 30 ms 75208 KB n=2000
65 Correct 27 ms 75100 KB n=2000
66 Correct 29 ms 75356 KB n=2000
67 Correct 28 ms 75100 KB n=2000
68 Correct 32 ms 75588 KB n=2000
69 Correct 21 ms 75100 KB n=2000
70 Correct 20 ms 75100 KB n=2000
71 Correct 21 ms 75100 KB n=2000
72 Correct 21 ms 75096 KB n=2000
73 Correct 22 ms 75192 KB n=2000
74 Correct 25 ms 75100 KB n=1844
75 Correct 21 ms 75096 KB n=2000
76 Correct 28 ms 75096 KB n=2000
77 Correct 29 ms 75100 KB n=2000
78 Correct 27 ms 75096 KB n=2000
79 Correct 31 ms 75096 KB n=2000
80 Correct 36 ms 75360 KB n=2000
81 Correct 33 ms 75296 KB n=2000
82 Correct 27 ms 75100 KB n=2000
83 Correct 29 ms 75356 KB n=2000
84 Correct 29 ms 75352 KB n=2000
85 Correct 33 ms 75100 KB n=2000
86 Correct 34 ms 75484 KB n=2000
87 Correct 27 ms 75096 KB n=2000
88 Correct 31 ms 75356 KB n=2000
89 Correct 28 ms 75352 KB n=2000
90 Correct 28 ms 75352 KB n=2000
91 Correct 21 ms 75140 KB n=2000
92 Runtime error 2429 ms 262144 KB Execution killed with signal 9
93 Halted 0 ms 0 KB -