【做题记录】HAOI 2015 树上操作

有一棵点数为 N 的树,以点 1 为根,且树有点权。然后有 M 个操作,分为三种:

  • 操作 1 :把某个节点 x 的点权增加 a 。
  • 操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 a 。
  • 操作 3 :询问某个节点 x 到根的路径中所有点的点权和。

做法:树链剖分+线段树,板子题

#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cctype>
typedef long long LL;
typedef unsigned long long ULL;
namespace FastIo{
    typedef __uint128_t L;
    static char buf[100000],*p1=buf,*p2=buf,fw[100000],*pw=fw;
    #define gc p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++
    inline void pc(const char &ch){
    	if(pw-fw==100000)fwrite(fw,1,100000,stdout),pw=fw;
    	*pw++=ch;
	}
    #define fsh fwrite(fw,1,pw-fw,stdout),pw=fw
	struct FastMod{
        FastMod(ULL b):b(b),m(ULL((L(1)<<64)/b)){}
        ULL reduce(ULL a){
            ULL q=(ULL)((L(m)*a)>>64);
            ULL r=a-q*b;
            return r>=b?r-b:r;
        }
        ULL b,m;
    }HPOP(10);
    struct QIO{
    	char ch;
    	int st[40];
		bool pd;
		template<class T>inline void read(T &x){
    		x=0,ch=gc,pd=false;
    		while(!isdigit(ch)){pd=ch=='-'?true:false;ch=gc;}
    		while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=gc;}
    		if(pd)x=-x;
		}
		template<class T>inline void write(T a){
			if(a<0)a=-a,pc('-');
			do{st[++st[0]]=HPOP.reduce(a);a/=10;}while(a);
			while(st[0])pc(st[st[0]--]^48);
			pc('\n');
		}
	}qrw;
}
using namespace FastIo;
#include<algorithm>
#define NUMBER1 100000
#define P(A) A=-~A
#define mid (l+r>>1)
#define ls(a) a<<1
#define rs(a) a<<1|1
#define sz (r-l+1)
#define lson l,mid,ls(rt)
#define rson mid+1,r,rs(rt)
#define fione(begin,end) for(register int i=begin;i<=end;P(i))
#define tt for(register int i=head[u];~i;i=e[i].next)
struct EDGE{int next,to;}e[(NUMBER1<<2)+5];
int head[NUMBER1+5],tot(0),n,a[NUMBER1+5],b[NUMBER1+5],fa[NUMBER1+5],size[NUMBER1+5],depth[NUMBER1+5],top[NUMBER1+5],son[NUMBER1+5],id[NUMBER1+5],cnt(0);
struct Segment{
	inline void push_up(const int &rt){tree[rt]=tree[(ls(rt))]+tree[rs(rt)];}
	inline void change(int l,int r,int rt,LL date){tree[rt]+=(LL)date*sz,lazy[rt]+=date;}
	inline void push_down(int l,int r,int rt){
		change(lson,lazy[rt]);
		change(rson,lazy[rt]);
		lazy[rt]=0;
	}
	void build(int l,int r,int rt){
		if(l==r)return tree[rt]=b[l],void();
		build(lson);build(rson);
		push_up(rt);
	}
	LL intervalsum(int l,int r,int rt,int x,int y){
		if(x<=l&&r<=y)return tree[rt];
		push_down(l,r,rt);
		LL res(0);
		if(x<=mid)res+=intervalsum(lson,x,y);
		if(mid<y)res+=intervalsum(rson,x,y);
		return res;
	}
	void intervaland(int l,int r,int rt,int x,int y,LL date){
		if(x<=l&&r<=y)return tree[rt]+=(LL)sz*date,lazy[rt]+=date,void();
		push_down(l,r,rt);
		if(x<=mid)intervaland(lson,x,y,date);
		if(mid<y)intervaland(rson,x,y,date);
		push_up(rt);
	}
	void pointand(int l,int r,int rt,int p,int date){
		if(l==r)return tree[rt]+=date,void();
		push_down(l,r,rt);
		if(p<=mid)pointand(lson,p,date);
		else pointand(rson,p,date);
		push_up(rt);
	}
	LL tree[(NUMBER1<<2)+5],lazy[(NUMBER1<<2)+5];
}seg;
inline void add(const int &u,const int &v){e[++tot].next=head[u];head[u]=tot,e[tot].to=v;}
void dfs1(int u,int fath,int deep){
	depth[u]=deep,fa[u]=fath,size[u]=1;
	int ms(-1);
	tt{
		if(e[i].to==fath)continue;
		dfs1(e[i].to,u,deep+1);
		size[u]+=size[e[i].to];
		if(size[e[i].to]>ms)ms=size[e[i].to],son[u]=e[i].to;
	}
}
void dfs2(int u,int tf){
	id[u]=++cnt;
	b[cnt]=a[u],top[u]=tf;
	if(!son[u])return;
	dfs2(son[u],tf);
	tt{
		if(e[i].to==fa[u]||e[i].to==son[u])continue;
		dfs2(e[i].to,e[i].to);
	}
}
inline LL treesum(int x,int y){
	LL ans(0);
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])std::swap(x,y);
		ans+=seg.intervalsum(1,n,1,id[top[x]],id[x]);
		x=fa[top[x]];
	}
	if(depth[x]>depth[y])std::swap(x,y);
	ans+=seg.intervalsum(1,n,1,id[x],id[y]);
	return ans;
}
signed main(){
	memset(head,-1,sizeof(head));
	int m,k,x,y;
	qrw.read(n);
	qrw.read(m);
	fione(1,n)qrw.read(a[i]);
	fione(1,n-1){
		qrw.read(x);qrw.read(y);
		add(x,y);add(y,x);;
	}
	dfs1(1,0,1);
	dfs2(1,1);
	seg.build(1,n,1);
	while(m--){
		qrw.read(k);
		qrw.read(x);
		switch(k){
			case 1:
				qrw.read(y);
				seg.pointand(1,n,1,id[x],y);
				break;
			case 2:
				qrw.read(y);
				seg.intervaland(1,n,1,id[x],id[x]+size[x]-1,y);
				break;
			case 3:qrw.write(treesum(x,1));break;
		}
	}
	fsh;
    exit(0);
    return 0;
}
posted @ 2023-05-14 14:58  SHOJYS  阅读(16)  评论(0编辑  收藏  举报