Submission #1005346

# Submission time Handle Problem Language Result Execution time Memory
1005346 2024-06-22T10:44:09 Z amirhoseinfar1385 Nestabilnost (COI23_nestabilnost) C++17
100 / 100
1466 ms 220672 KB
#include<bits/stdc++.h>
using namespace std;
const long long maxn=300100+40,lg=20,kaf=(1<<lg);
vector<long long>adj[maxn];
long long sz[maxn],all[maxn],wa[maxn],n,inf=1e17,inf2=1e17;
int te=0;

struct segment{
	struct node{
		long long mina,lazy,vis,aval,asl;
		node(){
			mina=0;
			lazy=0;
			vis=0;
			aval=0;
			asl=inf;
		}
	};
	long long fen[maxn];
	node seg[(1<<(lg+1))];
	void ins(long long i,long long w){
		seg[kaf+i].mina=w;
		seg[kaf+i].aval=w;
		fen[i]=w;
	}
	void build(){
		for(long long i=kaf-1;i>=1;i--){
			seg[i].mina=min(seg[(i<<1)].mina,seg[(i<<1)^1].mina);
			seg[i].aval=seg[i].mina;
		}
	}
	void calc(long long i){
		seg[i].vis=1;
		if(i>=kaf){
			seg[i].mina+=seg[i].lazy;
			seg[i].lazy=0;
			return ;
		}
		seg[i].mina=min(seg[(i<<1)].mina+seg[(i<<1)].lazy,seg[(i<<1)^1].lazy+seg[(i<<1)^1].mina);
	}
	void calcasl(int i){
		seg[i].vis=1;
		if(i>=kaf){
			seg[i].mina+=seg[i].lazy;
			seg[i].lazy=0;
			seg[i].mina=min(seg[i].mina,seg[i].asl+seg[i].aval);
			seg[i].asl=inf;
			return ;
		}
		seg[(i<<1)].vis=seg[(i<<1)^1].vis=1;
		seg[(i<<1)].lazy+=seg[i].lazy;
		seg[(i<<1)^1].lazy+=seg[i].lazy;
		seg[i].lazy=0;
		calc(i);
		seg[i].mina=min(seg[i].mina,seg[i].asl+seg[i].aval);
	}
	void shift(long long i){
		if(seg[i].asl==inf){
			seg[i].vis=1;
			if(i>=kaf){
				return ;
			}
			seg[(i<<1)].vis=seg[(i<<1)^1].vis=1;
			seg[(i<<1)].lazy+=seg[i].lazy;
			seg[(i<<1)^1].lazy+=seg[i].lazy;
			seg[i].lazy=0;
		}else{
			seg[i].vis=seg[(i<<1)].vis=seg[(i<<1)^1].vis=1;
			if(seg[(i<<1)].asl+seg[(i<<1)].lazy>=seg[i].asl){
				seg[(i<<1)].asl=seg[i].asl;
				calcasl((i<<1));
			}
			if(seg[(i<<1)^1].asl+seg[(i<<1)^1].lazy>=seg[i].asl){
				seg[(i<<1)^1].asl=seg[i].asl;
				calcasl((i<<1)^1);
			}
			seg[i].asl=inf;
			seg[(i<<1)].lazy+=seg[i].lazy;
			seg[(i<<1)^1].lazy+=seg[i].lazy;
			seg[i].lazy=0;
		}
	}
	void push(long long i,long long l,long long r,long long tl,long long tr,long long w){
		if(l>r||l>tr||r<tl||tl>tr){
			return ;
		}
		if(l>=tl&&r<=tr){
			shift(i);
			calc(i);
			seg[i].asl=w;
			calcasl(i);
			shift(i);
			calc(i);
			return ;
		}
		shift(i);
		long long m=(l+r)>>1;
		push((i<<1),l,m,tl,tr,w);
		push((i<<1)^1,m+1,r,tl,tr,w);
		calc(i);
	}
	void upd(long long i,long long l,long long r,long long tl,long long tr,long long w){
		if(l>r||l>tr||r<tl||tl>tr){
			return ;
		}
		if(l>=tl&&r<=tr){
			shift(i);
			calc(i);
			seg[i].lazy+=w;
			shift(i);
			calc(i);
			return ;
		}
		shift(i);
		long long m=(l+r)>>1;
		upd((i<<1),l,m,tl,tr,w);
		upd((i<<1)^1,m+1,r,tl,tr,w);
		calc(i);
	}
	long long getmin(long long i,long long l,long long r,long long tl,long long tr){
		if(l>r||l>tr||r<tl||tl>tr){
			return inf;
		}
		shift(i);
		calc(i);
		if(l>=tl&&r<=tr){
			return seg[i].mina+seg[i].lazy;
		}
		long long m=(l+r)>>1;
		return min(getmin((i<<1),l,m,tl,tr),getmin((i<<1)^1,m+1,r,tl,tr));
	}
	void clear(long long i=1,int l=0,int r=kaf-1,int tl=1,int tr=n+10){
		if(l>r||l>tr||r<tl||tl>tr||seg[i].vis==0){
			return ;
		}
		if(l>=tl&&r<=tr){
			seg[i].vis=0;
			seg[i].mina=seg[i].aval;
			seg[i].lazy=0;
			seg[i].asl=inf;
		}else{
			shift(i);
		}
		if(l==r){
			if(!(l>=tl&&r<=tr)){
				calc(i);
			}
			return ;
		}
		int m=(l+r)>>1;
		clear((i<<1),l,m,tl,tr);
		clear((i<<1)^1,m+1,r,tl,tr);
		if(!(l>=tl&&r<=tr)){
			calc(i);
		}
	}
}seg;
 
bool cmp(long long a,long long b){
	return sz[a]>sz[b];
}
 
void predfs(long long u,long long par=-1){
	if(par!=-1){
		sort(adj[u].begin(),adj[u].end());
		adj[u].erase(lower_bound(adj[u].begin(),adj[u].end(),par));
	}
	sz[u]=1;
	for(auto x:adj[u]){
		predfs(x,u);
		sz[u]+=sz[x];
	}
	sort(adj[u].begin(),adj[u].end(),cmp);
}
 
void vorod(){
	cin>>n;
	for(long long i=1;i<=n;i++){
		cin>>all[i];
	}
	for(long long i=1;i<=n;i++){
		cin>>wa[i];
	}
	for(long long i=0;i<n-1;i++){
		long long u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
}
 
void pre(){
	predfs(1);
	for(long long i=1;i<=n;i++){
		seg.ins(i,wa[i]);
	}
	seg.build();
}
set<long long>allind[maxn];
long long mainres=0;
 
long long dfs(long long u){
	long long ret=0;
	vector<pair<pair<long long,long long>,long long>>alle;
	for(long long i=1;i<(long long)adj[u].size();i++){
		long long v=adj[u][i];
		long long re=dfs(v);
		if(all[v]!=0&&all[v]-1!=all[u]){
			mainres+=re;
			seg.clear();
			continue;
		}
		allind[v].insert(all[u]+1);
		allind[v].insert(all[u]+2);
		allind[v].insert(1);
		deque<long long>tof;
		for(auto x:allind[v]){
			tof.push_back(x);
		}
		tof.push_back(n+1);
		if(all[v]==all[u]+1){
			seg.upd(1,0,kaf-1,1,all[v],inf2);
		}else{
			seg.upd(1,0,kaf-1,1,all[u],inf2);
			seg.upd(1,0,kaf-1,all[u]+2,n,inf2);
		}
		for(long long i=0;i<(long long)tof.size()-1;i++){
			if(tof[i+1]==tof[i]){
				continue;
			}
			long long w=seg.getmin(1,0,kaf-1,tof[i],tof[i])-wa[tof[i]];
			w=min(re,w);
			if(tof[i]<=all[v]){
				w=re;
			}
			alle.push_back(make_pair(make_pair(tof[i],tof[i+1]-1),w));
		}
		seg.clear();
	}
	if((int)adj[u].size()==0){
		allind[u].insert(1);
		return seg.getmin(1,0,kaf-1,all[u]+1,n);
	}
	long long v=adj[u][0];
	long long re=dfs(v);
	//wrf: 
	//for(int i=1;i<=n;i++){
	//	if(seg.fen[i]>re+wa[i]){
	//		seg.fen[i]=re+wa[i];
	//	}
	//}
	seg.push(1,0,kaf-1,1,n,re);
	if(all[v]!=0&&all[v]-1!=all[u]){
		mainres+=re;
		seg.clear();
	}else{
		swap(allind[u],allind[v]);
		allind[u].insert(all[u]+1);
		allind[u].insert(all[u]+2);
		if(all[v]==all[u]+1){
			seg.clear(1,0,kaf-1,1,all[v]);
			seg.upd(1,0,kaf-1,1,all[v],re);
		}else{
			seg.clear(1,0,kaf-1,1,all[u]);
			seg.clear(1,0,kaf-1,all[u]+2,n);
			seg.upd(1,0,kaf-1,1,all[u],re);
			seg.upd(1,0,kaf-1,all[u]+2,n,re);
		}
	}
	for(auto x:alle){
		allind[u].insert(x.first.first);
		allind[u].insert(x.first.second+1);
		seg.upd(1,0,kaf-1,x.first.first,x.first.second,x.second);
	}
	ret+=seg.getmin(1,0,kaf-1,all[u]+1,n);
	return ret;
}
 
void solve(){
	mainres+=dfs(1);
	cout<<mainres<<"\n";
}
 
 
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
//	freopen("inp.txt","r",stdin);
//	freopen("out.txt","w",stdout);
	vorod();
	pre();
	solve();
}
# Verdict Execution time Memory Grader output
1 Correct 28 ms 112476 KB Output is correct
2 Correct 31 ms 112672 KB Output is correct
3 Correct 30 ms 112500 KB Output is correct
4 Correct 33 ms 112572 KB Output is correct
5 Correct 29 ms 112472 KB Output is correct
6 Correct 30 ms 112472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1123 ms 211636 KB Output is correct
2 Correct 805 ms 211688 KB Output is correct
3 Correct 870 ms 211572 KB Output is correct
4 Correct 1111 ms 220672 KB Output is correct
5 Correct 866 ms 211536 KB Output is correct
6 Correct 902 ms 211796 KB Output is correct
7 Correct 928 ms 211600 KB Output is correct
8 Correct 900 ms 211844 KB Output is correct
9 Correct 1034 ms 214096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 110940 KB Output is correct
2 Correct 19 ms 110924 KB Output is correct
3 Correct 17 ms 110940 KB Output is correct
4 Correct 17 ms 111028 KB Output is correct
5 Correct 16 ms 110940 KB Output is correct
6 Correct 17 ms 110936 KB Output is correct
7 Correct 17 ms 110940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 112476 KB Output is correct
2 Correct 31 ms 112672 KB Output is correct
3 Correct 30 ms 112500 KB Output is correct
4 Correct 33 ms 112572 KB Output is correct
5 Correct 29 ms 112472 KB Output is correct
6 Correct 30 ms 112472 KB Output is correct
7 Correct 17 ms 110940 KB Output is correct
8 Correct 19 ms 110924 KB Output is correct
9 Correct 17 ms 110940 KB Output is correct
10 Correct 17 ms 111028 KB Output is correct
11 Correct 16 ms 110940 KB Output is correct
12 Correct 17 ms 110936 KB Output is correct
13 Correct 17 ms 110940 KB Output is correct
14 Correct 41 ms 111720 KB Output is correct
15 Correct 34 ms 111544 KB Output is correct
16 Correct 35 ms 111696 KB Output is correct
17 Correct 34 ms 111452 KB Output is correct
18 Correct 39 ms 111696 KB Output is correct
19 Correct 39 ms 111888 KB Output is correct
20 Correct 37 ms 111452 KB Output is correct
21 Correct 31 ms 111460 KB Output is correct
22 Correct 30 ms 111452 KB Output is correct
23 Correct 28 ms 111452 KB Output is correct
24 Correct 34 ms 111700 KB Output is correct
25 Correct 37 ms 111696 KB Output is correct
26 Correct 34 ms 111700 KB Output is correct
27 Correct 34 ms 111956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 112476 KB Output is correct
2 Correct 31 ms 112672 KB Output is correct
3 Correct 30 ms 112500 KB Output is correct
4 Correct 33 ms 112572 KB Output is correct
5 Correct 29 ms 112472 KB Output is correct
6 Correct 30 ms 112472 KB Output is correct
7 Correct 1123 ms 211636 KB Output is correct
8 Correct 805 ms 211688 KB Output is correct
9 Correct 870 ms 211572 KB Output is correct
10 Correct 1111 ms 220672 KB Output is correct
11 Correct 866 ms 211536 KB Output is correct
12 Correct 902 ms 211796 KB Output is correct
13 Correct 928 ms 211600 KB Output is correct
14 Correct 900 ms 211844 KB Output is correct
15 Correct 1034 ms 214096 KB Output is correct
16 Correct 17 ms 110940 KB Output is correct
17 Correct 19 ms 110924 KB Output is correct
18 Correct 17 ms 110940 KB Output is correct
19 Correct 17 ms 111028 KB Output is correct
20 Correct 16 ms 110940 KB Output is correct
21 Correct 17 ms 110936 KB Output is correct
22 Correct 17 ms 110940 KB Output is correct
23 Correct 41 ms 111720 KB Output is correct
24 Correct 34 ms 111544 KB Output is correct
25 Correct 35 ms 111696 KB Output is correct
26 Correct 34 ms 111452 KB Output is correct
27 Correct 39 ms 111696 KB Output is correct
28 Correct 39 ms 111888 KB Output is correct
29 Correct 37 ms 111452 KB Output is correct
30 Correct 31 ms 111460 KB Output is correct
31 Correct 30 ms 111452 KB Output is correct
32 Correct 28 ms 111452 KB Output is correct
33 Correct 34 ms 111700 KB Output is correct
34 Correct 37 ms 111696 KB Output is correct
35 Correct 34 ms 111700 KB Output is correct
36 Correct 34 ms 111956 KB Output is correct
37 Correct 1221 ms 139924 KB Output is correct
38 Correct 1173 ms 153512 KB Output is correct
39 Correct 1293 ms 154196 KB Output is correct
40 Correct 1466 ms 154196 KB Output is correct
41 Correct 1273 ms 150664 KB Output is correct
42 Correct 1257 ms 153916 KB Output is correct
43 Correct 1204 ms 154960 KB Output is correct
44 Correct 1155 ms 160972 KB Output is correct
45 Correct 1218 ms 181584 KB Output is correct
46 Correct 933 ms 150476 KB Output is correct
47 Correct 985 ms 151336 KB Output is correct
48 Correct 995 ms 151440 KB Output is correct
49 Correct 948 ms 150924 KB Output is correct
50 Correct 1151 ms 167252 KB Output is correct
51 Correct 1195 ms 168840 KB Output is correct
52 Correct 1263 ms 165728 KB Output is correct
53 Correct 1360 ms 177496 KB Output is correct