P4719 【模板】“动态 DP“&动态树分治

19 篇文章 0 订阅
4 篇文章 0 订阅

咕咕咕掉。。。。
什么玩意啊。。。
自闭ing

#include<bits/stdc++.h>
#define MAXN 100005
using namespace std;

int n,m,h[MAXN],tot,a[MAXN];
int f[MAXN],dep[MAXN],dfn[MAXN],dex,sz[MAXN],ch[MAXN],tp[MAXN],id[MAXN];
int ycl[MAXN][2],ans[2];

struct node{
	int from,to,next;
}e[MAXN << 1];

struct Ma{
	int o[2][2];
}g[MAXN],sum[MAXN * 5];

Ma operator * (Ma x , Ma y){
	Ma z;
	z.o[0][0] = max(x.o[0][0] + y.o[0][0] , x.o[0][1] + y.o[1][0]);
	z.o[0][1] = max(x.o[0][0] + y.o[0][1] , x.o[0][1] + y.o[1][1]);
	z.o[1][0] = max(x.o[1][0] + y.o[0][0] , x.o[1][1] + y.o[1][0]);
	z.o[1][1] = max(x.o[1][0] + y.o[0][1] , x.o[1][1] + y.o[1][1]);
	return z;
}

void add(int x , int y){
	tot++;
	e[tot].from = x;
	e[tot].to = y;
	e[tot].next = h[x];
	h[x] = tot;
}

void dfs(int now , int fa){
	f[now] = fa;
	sz[now] = 1;
	dep[now] = dep[fa] + 1;
	ycl[now][1] = a[now];
	for(int i = h[now] ; i != (-1) ; i = e[i].next){
		if(e[i].to == fa)continue;
		dfs(e[i].to , now);
		ycl[now][1] = ycl[now][1] + ycl[e[i].to][0];
		ycl[now][0] = ycl[now][0] + max(ycl[e[i].to][0] , ycl[e[i].to][1]);
		sz[now] = sz[now] + sz[e[i].to];
		if(sz[ch[now]] < sz[e[i].to])ch[now] = e[i].to;
	}
}

void dfs2(int now , int fa){
	dfn[now] = ++dex;
	id[dex] = now;
	if(ch[now]){
		tp[ch[now]] = tp[now];
		dfs2(ch[now] , now);
	}
	g[dfn[now]].o[0][1] = a[now];
	g[dfn[now]].o[1][1] = -99999999;
	for(int i = h[now] ; i != (-1) ; i = e[i].next){
		if(e[i].to == fa)continue;
		if(e[i].to == ch[now])continue;
		tp[e[i].to] = e[i].to;
		g[dfn[now]].o[0][0] = g[dfn[now]].o[0][0] + max(ycl[e[i].to][0] , ycl[e[i].to][1]);
		g[dfn[now]].o[1][0] = g[dfn[now]].o[1][0] + max(ycl[e[i].to][0] , ycl[e[i].to][1]);
		g[dfn[now]].o[0][1] = g[dfn[now]].o[0][1] + ycl[e[i].to][0];
		dfs2(e[i].to , now);
	}
}

void build(int rt , int l , int r){
	if(l == r){
		sum[rt] = g[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(rt << 1 , l , mid);
	build((rt << 1) | 1 , mid + 1 , r);
	sum[rt] = sum[(rt << 1) | 1] * sum[rt << 1];
	return;
}

void init(){
	memset(ycl , 0 , sizeof(ycl));
	memset(g , 0 , sizeof(g));
	memset(h , -1 , sizeof(h));
	tot = dex = 0;
	cin>>n>>m;int x,y;
	for(int i = 1 ; i <= n ; i++)cin>>a[i];
	for(int i = 1 ; i < n ; i++){
		cin>>x>>y;
		add(x , y) , add(y , x);
	}
	tp[1] = 1;
	dfs(1 , 1);
	dfs2(1 , 1);
	build(1 , 1 , n);
}

void update(int rt , int l , int r , int x , int y){
	if(x < l || x > r)return;
	if(l == r){
		sum[rt].o[0][1] = sum[rt].o[0][1] - a[id[l]];
		sum[rt].o[0][1] = sum[rt].o[0][1] + y;
		a[id[l]] = y;
		return;
	}
	int mid = (l + r) >> 1;
	update(rt << 1 , l , mid , x , y);
	update((rt << 1) | 1 , mid + 1 , r , x , y);
	sum[rt] = sum[(rt << 1) | 1] * sum[rt << 1];
	return;
}

void update2(int rt , int l , int r , int x){
	if(x < l || x > r)return;
	if(l == r){
		sum[rt].o[0][0] = ans[0];
		sum[rt].o[1][0] = ans[0];
		sum[rt].o[0][1] = ans[1] + a[id[l]];
		return;
	}
	int mid = (l + r) >> 1;
	update2(rt << 1 , l , mid , x);
	update2((rt << 1) | 1 , mid + 1 , r , x);
	sum[rt] = sum[(rt << 1) | 1] * sum[rt << 1];
	return;
}

void que(int rt , int l , int r , int x , int y){
	if(r < x || l > y)return;
	if(x <= l && r <= y){
		int zz1 = ans[0] , zz2 = ans[1];
		ans[0] = max(sum[rt].o[0][0] + zz1 , sum[rt].o[0][1] + zz2);
		ans[1] = max(sum[rt].o[1][0] + zz1 , sum[rt].o[1][1] + zz2);
		return;
	}
	int mid = (l + r) >> 1;
	que(rt << 1 , l , mid , x , y);
	que((rt << 1) | 1 , mid + 1 , r , x , y);
	return;
}

void sp_update(int x , int y){
	int now = x;
	update(1 , 1 , n , dfn[x] , y);
	while(tp[now] != 1){
		ans[0] = ans[1] = 0;
		que(1 , 1 , n , dfn[tp[x]] , dfn[x]);
		update2(1 , 1 , n , dfn[f[tp[x]]]);
		now = f[tp[x]];
	}
}

void solve(){
	int x,y;
	while(m--){
		cin>>x>>y;
		
	}
}

int main(){
	init();
	solve();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值