hdu3966Aragorn's Story【树链剖分+树状数组】

来源:互联网 发布:c语言题库及详解答案 编辑:程序博客网 时间:2024/06/05 15:39

Problem Description
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
Hint
1.The number of enemies may be negative.2.Huge input, be careful.
 

实在是受不了邝斌那种非得用单点更新的函数写区间更新的写法,太不好理解了,自己在这个基础上改了,mark一下,以后比赛的时候要打印

一个小错误是如果初始值不加入树中,最后求再加进去的时候下标不要换成p[u]!所以说,自己写示例是多么重要

/************hdu39662016.2.21014MS9792K2876 BG++************/#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=50005;struct edge{    int to,next;}edge[maxn*2];int head[maxn],tot;int top[maxn];//表示此节点所在重链的顶端节点int fa[maxn];//父节点int deep[maxn];//单纯深度,不包括点权和边权int num[maxn];//以此节点为根的子树节点数int p[maxn];//此点对应的位置int fp[maxn];//与上相反int son[maxn];//重儿子int pos;int c[maxn],n;void init(){    tot=0;    memset(head,-1,sizeof(head));    pos=1;//树状数组的    memset(son,-1,sizeof(son));    memset(c,0,sizeof(c));}void addedge(int u,int v){    edge[tot].to=v;edge[tot].next=head[u];head[u]=tot++;}void dfs1(int u,int pre,int d)//fa deep num son{    deep[u]=d;    fa[u]=pre;    num[u]=1;    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].to;        if(v!=pre)        {            dfs1(v,u,d+1);            num[u]+=num[v];            if(son[u]==-1||num[v]>num[son[u]]) son[u]=v;        }    }}void getpos(int u,int sp)//top p{    top[u]=sp;    p[u]=pos++;    fp[p[u]]=u;    if(son[u]==-1) return;    getpos(son[u],sp);    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].to;        if(v!=son[u]&&v!=fa[u]) getpos(v,v);    }}int lowbit(int x){    return x&(-x);}int sum(int i){    int s=0;    while(i<=n)    {        s+=c[i];        i+=lowbit(i);    }    return s;}void add(int i,int val){    while(i>0)    {        c[i]+=val;        i-=lowbit(i);    }}void change(int u,int v,int val){    int f1=top[u],f2=top[v];    while(f1!=f2)    {        if(deep[f1]<deep[f2])        {            swap(f1,f2);            swap(u,v);        }        add(p[u],val);        add(p[f1]-1,-val);        u=fa[f1];        f1=top[u];    }    if(deep[u]>deep[v]) swap(u,v);    add(p[u]-1,-val);    add(p[v],val);}int a[maxn];int main(){   // freopen("cin.txt","r",stdin);    int M,P;    while(~scanf("%d%d%d",&n,&M,&P))    {        int u,v;        int C1,C2,K;        char op[2];        init();        for(int i=1;i<=n;i++) scanf("%d",&a[i]);        while(M--)        {            scanf("%d%d",&u,&v);            addedge(u,v);addedge(v,u);        }        dfs1(1,0,0);        getpos(1,1);        while(P--)        {            scanf("%s",op);            if(op[0]=='Q')            {                scanf("%d",&u);              //  printf("sum=%d  ",sum(p[u]));                printf("%d\n",sum(p[u])+a[u]);            }            else            {                scanf("%d%d%d",&C1,&C2,&K);                if(op[0]=='D') K=-K;                change(C1,C2,K);            }        }    }    return 0;}


0 0