• luogu3178 [HAOI2015]树上操作


    裸题

    #include <iostream>
    #include <cstdio>
    using namespace std;
    typedef long long ll;
    int n, m, ecnt, hea[100005], dep[100005], top[100005], idx[100005], cnt;
    int fa[100005], siz[100005], son[100005], uu, vv;
    ll w[100005], wt[100005], ww;
    struct Edge{
    	int too, nxt;
    }edge[200005];
    void add_edge(int fro, int too){
    	edge[++ecnt].nxt = hea[fro];
    	edge[ecnt].too = too;
    	hea[fro] = ecnt;
    }
    void dfs1(int x, int f){
    	dep[x] = dep[f] + 1;
    	fa[x] = f;
    	siz[x] = 1;
    	int maxSon=-1;
    	for(int i=hea[x]; i; i=edge[i].nxt){
    		int t=edge[i].too;
    		if(t==f)	continue;
    		dfs1(t, x);
    		siz[x] += siz[t];
    		if(siz[t]>maxSon){
    			maxSon = siz[t];
    			son[x] = t;
    		}
    	}
    }
    void dfs2(int x, int topf){
    	top[x] = topf;
    	idx[x] = ++cnt;
    	wt[cnt] = w[x];
    	if(!son[x])	return ;
    	dfs2(son[x], topf);
    	for(int i=hea[x]; i; i=edge[i].nxt){
    		int t=edge[i].too;
    		if(t==fa[x] || t==son[x])	continue;
    		dfs2(t, t);
    	}
    }
    struct SGT{
    	ll sum[400005];
    	ll tag[400005];
    	void build(int o, int l, int r){
    		if(l==r)	sum[o] = wt[l];
    		else{
    			int mid=(l+r)>>1;
    			int lson=o<<1;
    			int rson=lson|1;
    			if(l<=mid)	build(lson, l, mid);
    			if(mid<r)	build(rson, mid+1, r);
    			sum[o] = sum[lson] + sum[rson];
    		}
    	}
    	void pushDown(int o, int l, int r, int lson, int rson, int mid){
    		tag[lson] += tag[o];
    		tag[rson] += tag[o];
    		sum[lson] += tag[o] * (mid-l+1);
    		sum[rson] += tag[o] * (r-mid);
    		tag[o] = 0;
    	}
    	void update(int o, int l, int r, int x, int y, ll k){
    		if(l>=x && r<=y){
    			sum[o] += (r-l+1) * k;
    			tag[o] += k;
    		}
    		else{
    			int mid=(l+r)>>1;
    			int lson=o<<1;
    			int rson=lson|1;
    			if(tag[o])	pushDown(o, l, r, lson, rson, mid);
    			if(x<=mid)	update(lson, l, mid, x, y, k);
    			if(mid<y)	update(rson, mid+1, r, x, y, k);
    			sum[o] = sum[lson] + sum[rson];
    		}
    	}
    	ll query(int o, int l, int r, int x, int y){
    		if(l>=x && r<=y)	return sum[o];
    		else{
    			int mid=(l+r)>>1;
    			int lson=o<<1;
    			int rson=lson|1;
    			ll ans=0;
    			if(tag[o])	pushDown(o, l, r, lson, rson, mid);
    			if(x<=mid)	ans += query(lson, l, mid, x, y);
    			if(mid<y)	ans += query(rson, mid+1, r, x, y);
    			return ans;
    		}
    	}
    }sgt;
    ll queryRange(int uu, int vv){
    	ll ans=0;
    	while(top[uu]!=top[vv]){
    		if(dep[top[uu]]<dep[top[vv]])	swap(uu, vv);
    		ans += sgt.query(1, 1, n, idx[top[uu]], idx[uu]);
    		uu = fa[top[uu]];
    	}
    	if(dep[uu]>dep[vv])	swap(uu, vv);
    	ans += sgt.query(1, 1, n, idx[uu], idx[vv]);
    	return ans;
    }
    signed main(){
    	cin>>n>>m;
    	for(int i=1; i<=n; i++)
    		scanf("%lld", &w[i]);
    	for(int i=1; i<n; i++){
    		scanf("%d %d", &uu, &vv);
    		add_edge(uu, vv);
    		add_edge(vv, uu);
    	}
    	dep[1] = 1;
    	dfs1(1, 0);
    	dfs2(1, 1);
    	sgt.build(1, 1, n);
    	while(m--){
    		scanf("%d", &uu);
    		if(uu==1){
    			scanf("%d %lld", &vv, &ww);
    			sgt.update(1, 1, n, idx[vv], idx[vv], ww);
    		}
    		if(uu==2){
    			scanf("%d %lld", &vv, &ww);
    			sgt.update(1, 1, n, idx[vv], idx[vv]+siz[vv]-1, ww);
    		}
    		if(uu==3){
    			scanf("%d", &vv);
    			printf("%lld
    ", queryRange(1, vv));
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    java中Array/List/Map/Object与Json互相转换详解
    推荐几款开源的js日期控件
    12款优秀的 JavaScript 日历和时间选择控件
    12款优秀的 JavaScript 日历和时间选择控件
    StringTokenizer(字符串分隔解析类型)
    StringTokenizer(字符串分隔解析类型)
    javascript中的undefined 和 not defined
    javascript中的undefined 和 not defined
    6.静态函数库设计
    5. Linux应用程序地址布局
  • 原文地址:https://www.cnblogs.com/poorpool/p/8178910.html
Copyright © 2020-2023  润新知