제출 #892355

#제출 시각아이디문제언어결과실행 시간메모리
892355LCJLYCat Exercise (JOI23_ho_t4)C++14
100 / 100
900 ms118500 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl; 
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;

int arr[200005];
vector<int>adj[200005];
int d[200005];
int p[22][200005];
int in[200005];
int out[200005];
int ptr=0;
int pp[200005];

void dfs(int index, int par){
	for(int x=0;x<20;x++){
		p[x+1][index]=p[x][p[x][index]];
	}
	in[index]=++ptr;
	pp[index]=par;
	
	for(auto it:adj[index]){
		if(it==par) continue;
		d[it]=d[index]+1;
		p[0][it]=index;
		dfs(it,index);
	}
	out[index]=ptr;
}

int lca(int a, int b){
	if(d[a]>d[b]) swap(a,b);
	int diff=d[b]-d[a];
	for(int x=0;x<20;x++){
		if(diff&(1<<x)) b=p[x][b];
	}
	if(a==b) return a;
	for(int x=19;x>=0;x--){
		if(p[x][a]!=p[x][b]){
			a=p[x][a];
			b=p[x][b];
		}
	}
	return p[0][a];
}

inline pii combine(const pii a, const pii b){
	if(a.first>b.first){
		return a;
	}
	else return b;
}

struct node{
	int s,e,m;
	node *l,*r;
	pii v;
	int lazyUpd=0;
	
	node(int ss, int ee):s(ss),e(ee),m((s+e)>>1),v({0,0}),lazyUpd(0){
		if(s!=e){
			l=new node(s,m);
			r=new node(m+1,e);
		}
	}
	
	void self_add(int x){
		v.first+=x;
		lazyUpd+=x;
	}
	
	void forceProp(){
		if(s==e) return;
		if(lazyUpd){
			l->self_add(lazyUpd),r->self_add(lazyUpd);
			lazyUpd=0;
		}
	}
	
	void upd(int x, pii y){
		if(s==e){
			v=y;
			return;
		}
		if(x<=m) l->upd(x,y);
		else r->upd(x,y);
		v=combine(l->v,r->v);
	}
	
	void rangeUpd(int x, int y, int c){
		if(x<=s&&y>=e){
			self_add(c);
			return;
		}
		forceProp();
		if(x<=m)l->rangeUpd(x,y,c);
		if(y>m)r->rangeUpd(x,y,c);
		v=combine(l->v,r->v);
	}
	
	pii query(int x, int y){
		if(x<=s&&y>=e){
			return v;
		}
		forceProp();
		if(y<=m)return l->query(x,y);
		if(x>m)return r->query(x,y);
		return combine(l->query(x,m),r->query(m+1,y));
	}
};

int f(int a, int b){
	int hold=lca(a,b);
	return d[a]+d[b]-2*d[hold];
}

int offset=-1e12;

node st(0,200005);

int dp(int index){
	st.rangeUpd(in[index],in[index],offset);
	
	int ans=0;
	
	for(auto it:adj[index]){
		if(it==pp[index]){
			st.rangeUpd(in[index],out[index],offset);
		}
		else{
			st.rangeUpd(in[1],out[1],offset);
			st.rangeUpd(in[it],out[it],-offset);
		}
		
		pii hold=st.query(in[1],out[1]);
		if(hold.first>=0){
			//show3(index,index,it,it,hold.second,hold.second);
			//show(dist,f(hold.second,index));
			ans=max(ans,f(hold.second,index)+dp(hold.second));
		}
		
		if(it==pp[index]){
			st.rangeUpd(in[index],out[index],-offset);
		}
		else{
			st.rangeUpd(in[1],out[1],-offset);
			st.rangeUpd(in[it],out[it],offset);
		}
	}
	st.rangeUpd(in[index],in[index],-offset);
	//show2(index,index,ans,ans);
	return ans;
}

void solve(){	
	int n;
	cin >> n;
	
	for(int x=1;x<=n;x++){
		cin >> arr[x];
	}
	
	int temp,temp2;
	for(int x=0;x<n-1;x++){
		cin >> temp >> temp2;
		adj[temp].push_back(temp2);
		adj[temp2].push_back(temp);
	}
	
	dfs(1,-1);
	for(int x=1;x<=n;x++){
		st.upd(in[x],{arr[x],x});
	}

	int last=st.query(1,n).second;
	cout << dp(last);
	
}

int32_t main(){										
	ios::sync_with_stdio(0);	
	cin.tie(0);
	//freopen("in.txt", "r", stdin);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}	
}



		


		
		
	
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...