HDU 3966 [树链剖分]

来源:互联网 发布:斑马网络 上汽撤资 编辑:程序博客网 时间:2024/06/05 08:31

Aragorn’s Story

问题描述

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.

输入

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.

输出

For each query, you need to output the actually number of enemies in the specified camp.

样例输入

3 2 5
1 2 3
2 1
2 3
I 1 3 5
Q 2
D 1 2 2
Q 1
Q 3

样例输出

7
4
8

提示

1.The number of enemies may be negative.
2.Huge input, be careful.

基本树剖,区间修改点查询,比较容易想到,也可以用树状数组做,复杂度O(nlogn),但线段树应用更广泛,复杂度O(nlog2n)

代码样本

#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<set>#include<vector>using namespace std;#define L(u) (u<<1)#define R(u) ((u<<1)|1)#define maxn 50005struct tre{    int l,r;}nod[maxn*4];int sum[maxn*4],ad[maxn*4],tov[maxn*2],nexti[maxn*2],head[maxn],a[maxn],size[maxn],fa[maxn],son[maxn],dfn[maxn],ma[maxn],top[maxn],d[maxn],num,m,n,p,c1,c2,w;char op[5];int dfs1(int u){    size[u]=1;    int maxson=0,tmp=0;    int v=head[u];    while(v)    {        if(tov[v]!=fa[u])        {            d[tov[v]]=d[u]+1;            fa[tov[v]]=u;            tmp=dfs1(tov[v]);            size[u]+=tmp;            if(tmp>maxson)            {                maxson=tmp;                son[u]=tov[v];            }        }        v=nexti[v];    }    return size[u];}void dfs2(int u,int tp){    dfn[u]=++num;    top[u]=tp;    ma[num]=u;    if(son[u])dfs2(son[u],tp);    int v=head[u];    while(v)    {        if(tov[v]!=fa[u]&&tov[v]!=son[u])        dfs2(tov[v],tov[v]);        v=nexti[v];    }}void pushup(int u){ sum[u]=sum[L(u)]+sum[R(u)]; }void pushdown(int u){    sum[L(u)]+=ad[u]*(nod[L(u)].r-nod[L(u)].l+1);    ad[L(u)]+=ad[u];    sum[R(u)]+=ad[u]*(nod[R(u)].r-nod[R(u)].l+1);    ad[R(u)]+=ad[u];    ad[u]=0;}void build(int u,int le,int ri){    nod[u].l=le,nod[u].r=ri;    if(le==ri)    {        sum[u]=a[ma[le]];        return;    }    int mid=(le+ri)/2;    build(L(u),le,mid);    build(R(u),mid+1,ri);    pushup(u);}void update(int u,int le,int ri,int val){    if(nod[u].l>=le&&nod[u].r<=ri)    {        sum[u]+=val*(nod[u].r-nod[u].l+1);        ad[u]+=val;        return;    }    if(ad[u]) pushdown(u);    int mid=(nod[u].l+nod[u].r)/2;    if(ri<=mid)update(L(u),le,ri,val);    else if(le>mid)update(R(u),le,ri,val);    else    {        update(L(u),le,mid,val);        update(R(u),mid+1,ri,val);    }}int query(int u,int ai){    if(nod[u].l==nod[u].r)        return sum[u];    if(ad[u])pushdown(u);    int mid=(nod[u].l+nod[u].r)/2;    if(ai<=mid) return query(L(u),ai);    else return query(R(u),ai);}void youth(int c1,int c2,int val){    int tp1=top[c1],tp2=top[c2];    while(tp1!=tp2)    {        if(d[tp1]<d[tp2])        {            swap(tp1,tp2);            swap(c1,c2);        }        update(1,dfn[tp1],dfn[c1],val);        c1=fa[tp1];        tp1=top[c1];    }    if(d[c1]<d[c2])        swap(c1,c2);    update(1,dfn[c2],dfn[c1],val);}int main(){    while(scanf("%d%d%d",&n,&m,&p)==3)    {        num=0;        memset(head,0,sizeof(head));        memset(son,0,sizeof(son));        memset(fa,0,sizeof(fa));        memset(ad,0,sizeof(ad));//一定要清空ad!        for(int i=1;i<=n;i++)            scanf("%d",&a[i]);        int a1,b1;        for(int i=1;i<=m;i++)        {            scanf("%d%d",&a1,&b1);            tov[i]=b1;            tov[i+m]=a1;            nexti[i]=head[a1];            nexti[i+m]=head[b1];            head[a1]=i;            head[b1]=i+m;        }        d[1]=1;        dfs1(1);        dfs2(1,1);        build(1,1,n);        while(p--)        {            scanf(" %s ",op);            if(op[0]=='I'||op[0]=='D')            {                scanf(" %d%d%d",&c1,&c2,&w);                if(op[0]=='D')                    w=-w;                youth(c1,c2,w);            }            else            {                scanf("%d",&c1);                printf("%d\n",query(1,dfn[c1]));            }        }    }    return 0;}
0 0