답안 #892355

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
892355 2023-12-25T08:59:15 Z LCJLY Cat Exercise (JOI23_ho_t4) C++14
100 / 100
900 ms 118500 KB
#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();
	}	
}



		


		
		
	
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 70748 KB Output is correct
2 Correct 25 ms 70840 KB Output is correct
3 Correct 21 ms 70816 KB Output is correct
4 Correct 22 ms 70744 KB Output is correct
5 Correct 21 ms 70736 KB Output is correct
6 Correct 26 ms 70748 KB Output is correct
7 Correct 23 ms 70768 KB Output is correct
8 Correct 21 ms 70736 KB Output is correct
9 Correct 22 ms 70736 KB Output is correct
10 Correct 21 ms 70736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 70748 KB Output is correct
2 Correct 25 ms 70840 KB Output is correct
3 Correct 21 ms 70816 KB Output is correct
4 Correct 22 ms 70744 KB Output is correct
5 Correct 21 ms 70736 KB Output is correct
6 Correct 26 ms 70748 KB Output is correct
7 Correct 23 ms 70768 KB Output is correct
8 Correct 21 ms 70736 KB Output is correct
9 Correct 22 ms 70736 KB Output is correct
10 Correct 21 ms 70736 KB Output is correct
11 Correct 22 ms 70748 KB Output is correct
12 Correct 22 ms 70736 KB Output is correct
13 Correct 22 ms 70748 KB Output is correct
14 Correct 22 ms 70748 KB Output is correct
15 Correct 22 ms 70736 KB Output is correct
16 Correct 22 ms 70748 KB Output is correct
17 Correct 22 ms 70852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 70748 KB Output is correct
2 Correct 25 ms 70840 KB Output is correct
3 Correct 21 ms 70816 KB Output is correct
4 Correct 22 ms 70744 KB Output is correct
5 Correct 21 ms 70736 KB Output is correct
6 Correct 26 ms 70748 KB Output is correct
7 Correct 23 ms 70768 KB Output is correct
8 Correct 21 ms 70736 KB Output is correct
9 Correct 22 ms 70736 KB Output is correct
10 Correct 21 ms 70736 KB Output is correct
11 Correct 22 ms 70748 KB Output is correct
12 Correct 22 ms 70736 KB Output is correct
13 Correct 22 ms 70748 KB Output is correct
14 Correct 22 ms 70748 KB Output is correct
15 Correct 22 ms 70736 KB Output is correct
16 Correct 22 ms 70748 KB Output is correct
17 Correct 22 ms 70852 KB Output is correct
18 Correct 31 ms 72028 KB Output is correct
19 Correct 31 ms 72016 KB Output is correct
20 Correct 32 ms 71772 KB Output is correct
21 Correct 31 ms 71504 KB Output is correct
22 Correct 34 ms 71508 KB Output is correct
23 Correct 31 ms 71512 KB Output is correct
24 Correct 31 ms 71516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 70748 KB Output is correct
2 Correct 25 ms 70840 KB Output is correct
3 Correct 21 ms 70816 KB Output is correct
4 Correct 22 ms 70744 KB Output is correct
5 Correct 21 ms 70736 KB Output is correct
6 Correct 26 ms 70748 KB Output is correct
7 Correct 23 ms 70768 KB Output is correct
8 Correct 21 ms 70736 KB Output is correct
9 Correct 22 ms 70736 KB Output is correct
10 Correct 21 ms 70736 KB Output is correct
11 Correct 22 ms 70748 KB Output is correct
12 Correct 22 ms 70736 KB Output is correct
13 Correct 22 ms 70748 KB Output is correct
14 Correct 22 ms 70748 KB Output is correct
15 Correct 22 ms 70736 KB Output is correct
16 Correct 22 ms 70748 KB Output is correct
17 Correct 22 ms 70852 KB Output is correct
18 Correct 31 ms 72028 KB Output is correct
19 Correct 31 ms 72016 KB Output is correct
20 Correct 32 ms 71772 KB Output is correct
21 Correct 31 ms 71504 KB Output is correct
22 Correct 34 ms 71508 KB Output is correct
23 Correct 31 ms 71512 KB Output is correct
24 Correct 31 ms 71516 KB Output is correct
25 Correct 24 ms 70736 KB Output is correct
26 Correct 32 ms 71756 KB Output is correct
27 Correct 33 ms 71644 KB Output is correct
28 Correct 33 ms 71856 KB Output is correct
29 Correct 32 ms 71628 KB Output is correct
30 Correct 32 ms 71248 KB Output is correct
31 Correct 34 ms 71252 KB Output is correct
32 Correct 34 ms 71248 KB Output is correct
33 Correct 32 ms 71248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 70748 KB Output is correct
2 Correct 25 ms 70840 KB Output is correct
3 Correct 21 ms 70816 KB Output is correct
4 Correct 22 ms 70744 KB Output is correct
5 Correct 21 ms 70736 KB Output is correct
6 Correct 26 ms 70748 KB Output is correct
7 Correct 23 ms 70768 KB Output is correct
8 Correct 21 ms 70736 KB Output is correct
9 Correct 22 ms 70736 KB Output is correct
10 Correct 21 ms 70736 KB Output is correct
11 Correct 22 ms 70748 KB Output is correct
12 Correct 22 ms 70736 KB Output is correct
13 Correct 22 ms 70748 KB Output is correct
14 Correct 22 ms 70748 KB Output is correct
15 Correct 22 ms 70736 KB Output is correct
16 Correct 22 ms 70748 KB Output is correct
17 Correct 22 ms 70852 KB Output is correct
18 Correct 31 ms 72028 KB Output is correct
19 Correct 31 ms 72016 KB Output is correct
20 Correct 32 ms 71772 KB Output is correct
21 Correct 31 ms 71504 KB Output is correct
22 Correct 34 ms 71508 KB Output is correct
23 Correct 31 ms 71512 KB Output is correct
24 Correct 31 ms 71516 KB Output is correct
25 Correct 514 ms 118500 KB Output is correct
26 Correct 554 ms 115496 KB Output is correct
27 Correct 521 ms 115692 KB Output is correct
28 Correct 498 ms 99712 KB Output is correct
29 Correct 518 ms 99716 KB Output is correct
30 Correct 520 ms 99712 KB Output is correct
31 Correct 500 ms 99708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 70744 KB Output is correct
2 Correct 22 ms 70756 KB Output is correct
3 Correct 586 ms 91020 KB Output is correct
4 Correct 585 ms 91440 KB Output is correct
5 Correct 561 ms 90904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 70748 KB Output is correct
2 Correct 25 ms 70840 KB Output is correct
3 Correct 21 ms 70816 KB Output is correct
4 Correct 22 ms 70744 KB Output is correct
5 Correct 21 ms 70736 KB Output is correct
6 Correct 26 ms 70748 KB Output is correct
7 Correct 23 ms 70768 KB Output is correct
8 Correct 21 ms 70736 KB Output is correct
9 Correct 22 ms 70736 KB Output is correct
10 Correct 21 ms 70736 KB Output is correct
11 Correct 22 ms 70748 KB Output is correct
12 Correct 22 ms 70736 KB Output is correct
13 Correct 22 ms 70748 KB Output is correct
14 Correct 22 ms 70748 KB Output is correct
15 Correct 22 ms 70736 KB Output is correct
16 Correct 22 ms 70748 KB Output is correct
17 Correct 22 ms 70852 KB Output is correct
18 Correct 31 ms 72028 KB Output is correct
19 Correct 31 ms 72016 KB Output is correct
20 Correct 32 ms 71772 KB Output is correct
21 Correct 31 ms 71504 KB Output is correct
22 Correct 34 ms 71508 KB Output is correct
23 Correct 31 ms 71512 KB Output is correct
24 Correct 31 ms 71516 KB Output is correct
25 Correct 24 ms 70736 KB Output is correct
26 Correct 32 ms 71756 KB Output is correct
27 Correct 33 ms 71644 KB Output is correct
28 Correct 33 ms 71856 KB Output is correct
29 Correct 32 ms 71628 KB Output is correct
30 Correct 32 ms 71248 KB Output is correct
31 Correct 34 ms 71252 KB Output is correct
32 Correct 34 ms 71248 KB Output is correct
33 Correct 32 ms 71248 KB Output is correct
34 Correct 514 ms 118500 KB Output is correct
35 Correct 554 ms 115496 KB Output is correct
36 Correct 521 ms 115692 KB Output is correct
37 Correct 498 ms 99712 KB Output is correct
38 Correct 518 ms 99716 KB Output is correct
39 Correct 520 ms 99712 KB Output is correct
40 Correct 500 ms 99708 KB Output is correct
41 Correct 21 ms 70744 KB Output is correct
42 Correct 22 ms 70756 KB Output is correct
43 Correct 586 ms 91020 KB Output is correct
44 Correct 585 ms 91440 KB Output is correct
45 Correct 561 ms 90904 KB Output is correct
46 Correct 873 ms 109696 KB Output is correct
47 Correct 849 ms 109648 KB Output is correct
48 Correct 875 ms 109604 KB Output is correct
49 Correct 900 ms 109652 KB Output is correct
50 Correct 822 ms 88652 KB Output is correct
51 Correct 811 ms 88912 KB Output is correct
52 Correct 823 ms 88540 KB Output is correct
53 Correct 797 ms 88788 KB Output is correct