zoukankan      html  css  js  c++  java
  • 动态dp模板

    noip居然考了这个毒瘤玩意,虽然有不用ddp的方法,但是还是简单学一下,QAQ

    对于dp的转移可以用矩阵乘法的方式来维护,只不过修改了一些矩阵乘法中的运算类型,本质还是一样的,由于矩阵有结合律,所以可以用个线段树等数据结构维护,对于树上的,我们用dfs序或者树链剖分就可以转化为序列上的问题。

    对于树上的用树剖的话是O(()×nlog2n)O((矩阵乘法复杂度) imes nlog^2n)的,所以我们有时可以用LCTLCT来优化到O(()×nlogn)O((矩阵乘法复杂度) imes nlogn),但是LCTLCT常数巨大,不一定有树剖跑的快,所以还可以用全局平衡二叉树优化,这里不再多说。

    树剖模板【IN-Luogu】(过不去加强版的,要一个lognlogn的才过的去)

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define ll int
    using namespace std;
    const int N=1e5+10;
    const int M=1e5+15;
    int n,m;ll P[N];
    struct ss{
    	int to,last;
    	ss(){}
    	ss(int a,int b):to(a),last(b){}
    }g[N<<1];
    int head[N],cnt;
    void add(int a,int b){
    	g[++cnt]=ss(b,head[a]);head[a]=cnt;
    	g[++cnt]=ss(a,head[b]);head[b]=cnt;
    }
    
    int pos[M],rf[M],top[M],f[M],son[M],sze[M],ed[M];
    int tim;
    void dfs1(int a){
    	sze[a]=1;
    	for(int i=head[a];i;i=g[i].last){
    		if(g[i].to==f[a]) continue;
    		f[g[i].to]=a;
    		dfs1(g[i].to);
    		sze[a]+=sze[g[i].to];
    		if(!son[a]||sze[son[a]]<sze[g[i].to])
    		son[a]=g[i].to;
    	}
    }
    
    void dfs2(int a,int b){
    	top[a]=b;rf[pos[a]=++tim]=a;
    	if(!son[a]){
    		ed[b]=tim;
    		return;
    	}
    	dfs2(son[a],b);
    	for(int i=head[a];i;i=g[i].last){
    		if(g[i].to==f[a]||g[i].to==son[a]) continue;
    		dfs2(g[i].to,g[i].to);
    	}
    }
    ll dp[2][M];
    void init(int a){
    	dp[0][a]=0;
    	dp[1][a]=max(0,P[a]);
    	for(int i=head[a];i;i=g[i].last){
    		if(g[i].to==f[a]) continue;
    		init(g[i].to);
    		dp[0][a]+=max(dp[0][g[i].to],dp[1][g[i].to]);
    		dp[1][a]+=dp[0][g[i].to];
    	}
    }
    struct matrix{
    	ll v[2][2];
    	void clear(){memset(v,0,sizeof(v));}
    }ls;
    matrix operator *(const matrix &a,const matrix &b){
    	ls.v[0][0]=max(a.v[0][0]+b.v[0][0],a.v[0][1]+b.v[1][0]);
    	ls.v[0][1]=max(a.v[0][0]+b.v[0][1],a.v[0][1]+b.v[1][1]);
    	ls.v[1][0]=max(a.v[1][0]+b.v[0][0],a.v[1][1]+b.v[1][0]);
    	ls.v[1][1]=max(a.v[1][0]+b.v[0][1],a.v[1][1]+b.v[1][1]);
    	return ls;
    }
    /*
    matrix operator *(const matrix &a,const matrix &b){
    		matrix ls;
    		for(int i=0;i<=1;i++){
    			for(int j=0;j<=1;j++){
    				ls.v[i][j]=0;
    				for(int k=0;k<=1;k++){
    					ls.v[i][j]=max(ls.v[i][j],a.v[i][k]+b.v[k][j]);
    				}
    			}
    		}
    		return ls;
    	}
    */
    
    struct Segment_tree{
    	matrix S[M<<2],rec[M];
    	void pushup(int o){
    		S[o]=S[o<<1]*S[o<<1|1];
    	}
    	void build(int o,int l,int r){
    		if(l==r){
    			int a=rf[l];
    			ll f0=0,f1=P[a];
    			for(int i=head[a];i;i=g[i].last){
    				if(g[i].to==f[a]||g[i].to==son[a]) continue;
    				f0+=max(dp[0][g[i].to],dp[1][g[i].to]);
    				f1+=dp[0][g[i].to];
    			}
    			S[o].v[0][0]=S[o].v[0][1]=f0;
    			S[o].v[1][0]=f1;S[o].v[1][1]=0;
    			rec[l]=S[o];
    			return;
    		}
    		int mid=l+r>>1;
    		build(o<<1,l,mid);
    		build(o<<1|1,mid+1,r);
    		pushup(o);
    	}
    	void update(int o,int l,int r,int p){
    		if(l==r){
    			S[o]=rec[l];
    			return;
    		}
    		int mid=l+r>>1;
    		if(p<=mid) update(o<<1,l,mid,p);
    		else update(o<<1|1,mid+1,r,p);
    		pushup(o);
    	}
    	matrix query(int o,int l,int r,int L,int R){
    		if(L<=l&&r<=R) return S[o];
    		int mid=l+r>>1;
    		if(R<=mid) return query(o<<1,l,mid,L,R);
    		else if(L>mid) return query(o<<1|1,mid+1,r,L,R);
    		else return query(o<<1,l,mid,L,R)*query(o<<1|1,mid+1,r,L,R);
    	}
    	matrix query_chain(int o){
    		return query(1,1,n,pos[o],ed[o]);
    	}
    	void calc(int p,ll tp){
    		int id=pos[p];
    		rec[id].v[1][0]+=tp-P[p];
    		P[p]=tp;
    		matrix bef,now;
    		while(p){
    			bef=query_chain(top[p]);
    			update(1,1,n,pos[p]);
    			now=query_chain(top[p]);
    			p=f[top[p]];
    			if(!p) break;
    			id=pos[p];
    			rec[id].v[0][0]+=max(now.v[0][0],now.v[1][0])-max(bef.v[0][0],bef.v[1][0]);
    			rec[id].v[0][1]=rec[id].v[0][0];
    			rec[id].v[1][0]+=now.v[0][0]-bef.v[0][0];
    		}
    	}
    }Seg;
    void solve(int p,ll t){
    	Seg.calc(p,t);
    	matrix now=Seg.query_chain(1);
    	printf("%d
    ",max(now.v[0][0],now.v[1][0]));
    }
    void Init_bef(){
    	dfs1(1);
    	dfs2(1,1);
    	init(1);
    	Seg.build(1,1,n);
    }
    int a,b;
    int main(){
    	scanf("%d%d",&n,&m);
    	for(int i=1;i<=n;i++){
    		scanf("%d",&P[i]);
    	}
    	for(int i=1;i<n;i++){
    		scanf("%d%d",&a,&b);
    		add(a,b);
    	}
    	Init_bef();
    	while(m--){
    		scanf("%d%d",&a,&b);
    		solve(a,b);
    	}
    	return 0;
    }
    
    

    准备练习相关题目:


    乘风破浪会有时,直挂云帆济沧海

    加油努力!

  • 相关阅读:
    后台管理导航栏时间提醒代码。
    关于XML(一)。
    MySQL存储过程之细节
    MySQL存储过程之函数及元数据
    MySQL存储过程之安全策略
    MySQL存储过程之游标
    MySQL存储过程之异常处理
    MySQL存储过程之流程控制
    MySQL存储过程之新sql语句--变量
    MySQL存储过程之参数和复合语句
  • 原文地址:https://www.cnblogs.com/VictoryCzt/p/10053384.html
Copyright © 2011-2022 走看看