HDU 3966 (树链剖分对点权值,模板)

来源:互联网 发布:首届书法艺术网络大赛 编辑:程序博客网 时间:2024/05/17 01:45

Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M edges connect them. It is guaranteed that for any two camps, there is one and only one path connect them. At first Aragorn know the number of enemies in every camp. But the enemy is cunning , they will increase or decrease the number of soldiers in camps. Every time the enemy change the number of soldiers, they will set two camps C1 and C2. Then, for C1, C2 and all camps on the path from C1 to C2, they will increase or decrease K soldiers to these camps. Now Aragorn wants to know the number of soldiers in some particular camps real-time.
Input
Multiple test cases, process to the end of input. 

For each case, The first line contains three integers N, M, P which means there will be N(1 ≤ N ≤ 50000) camps, M(M = N-1) edges and P(1 ≤ P ≤ 100000) operations. The number of camps starts from 1. 

The next line contains N integers A1, A2, ...AN(0 ≤ Ai ≤ 1000), means at first in camp-i has Ai enemies. 

The next M lines contains two integers u and v for each, denotes that there is an edge connects camp-u and camp-v. 

The next P lines will start with a capital letter 'I', 'D' or 'Q' for each line. 

'I', followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, increase K soldiers to these camps. 

'D', followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, decrease K soldiers to these camps. 

'Q', followed by one integer C, which is a query and means Aragorn wants to know the number of enemies in camp C at that time.
Output
For each query, you need to output the actually number of enemies in the specified camp.
Sample Input
3 2 51 2 32 12 3I 1 3 5Q 2D 1 2 2Q 1 Q 3
Sample Output
748


代码:


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <queue>#define mem(p,k) memset(p,k,sizeof(p));#define rep(a,b,c) for(int a=b;a<c;a++)#define pb push_back#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define inf 0x6fffffff#define ll long longusing namespace std;const int N=50010;struct node{    int to,next;}e[N*2];int edge,root,id,n,m,q;int first[N],siz[N],dep[N],fa[N],son[N],tp[N],c[N],d[N],w[N];int tree[N<<2];void add(int a,int b){    e[++edge].to=b;    e[edge].next=first[a];    first[a]=edge;}void dfs(int x){    siz[x]=1;son[x]=0;    for(int i=first[x]; ~i ;i = e[i].next){        if(e[i].to==fa[x])continue;        fa[e[i].to]=x;        dep[e[i].to]=dep[x]+1;        dfs(e[i].to);        if(siz[e[i].to]>siz[son[x]]){            son[x]=e[i].to;        }        siz[x]+=siz[e[i].to];    }}void build_tree(int x,int top){    w[x]=++id;    tp[x]=top;    if(son[x]){        build_tree(son[x],top);    }    for(int i=first[x]; ~i ;i = e[i].next){        if(e[i].to==fa[x] || e[i].to==son[x])continue;        build_tree(e[i].to,e[i].to);    }}void build(int l,int r,int rt){    if(l==r){        tree[rt]=d[l];return;    }    tree[rt]=0;    int m=(l+r)>>1;    build(lson);    build(rson);}void pushdown(int rt){    if(tree[rt]){        tree[rt<<1]+=tree[rt];        tree[rt<<1|1]+=tree[rt];        tree[rt]=0;    }}void update(int L,int R,int c,int l,int r,int rt){    if(L<=l && R>=r){        tree[rt]+=c;return;    }    int m=(l+r)>>1;    pushdown(rt);    if(L<=m)update(L,R,c,lson);    if(R>m) update(L,R,c,rson);}int query(int L,int R,int l,int r,int rt){    if(L<=l && R>=r){        return tree[rt];    }    int m=(l+r)>>1;    pushdown(rt);    if(L<=m) return query(L,R,lson);    if(R>m)  return query(L,R,rson);}void Update(int x,int y,int z){    int fx=tp[x],fy=tp[y];    while(fx!=fy){        if(dep[fx]<dep[fy]){            swap(fx,fy);swap(x,y);        }        update(w[fx],w[x],z,1,id,1);        x=fa[fx];        fx=tp[x];    }    if(dep[x]<dep[y]){            swap(x,y);    }    update(w[y],w[x],z,1,id,1);}void init(){    int x,y;    edge=0;    id=0;    root=n/2+1;    fa[root]=dep[root]=tp[root]=0;    mem(first,-1);    for(int i=1;i<=n;i++)scanf("%d",c+i);    for(int i=1;i<=m;i++){        scanf("%d%d",&x,&y);        add(x,y);        add(y,x);    }    dfs(root);    build_tree(root,root);    for(int i=1;i<=n;i++){        d[w[i]]=c[i];    }    build(1,id,1);}void solve(){    char s[11];    int x,y,z;    while(q--){        scanf("%s",s);        if(s[0]=='Q'){            scanf("%d",&x);            printf("%d\n",query(w[x],w[x],1,id,1));        }        else{            scanf("%d%d%d",&x,&y,&z);            if(s[0]=='D')z=-z;            Update(x,y,z);        }    }}int main(){    while(~scanf("%d%d%d",&n,&m,&q)){        init();        solve();    }    return 0;}/*5 4 1001 1 1 1 11 22 32 44 5*/


0 0
原创粉丝点击