1. 程式人生 > >hdu 3966 Aragorn's Story

hdu 3966 Aragorn's Story

ret bsp panel change it is find rom multi ini

Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12673 Accepted Submission(s): 3394


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 5 1 2 3 2 1 2 3 I 1 3 5 Q 2 D 1 2 2 Q 1 Q 3

Sample Output 7 4 8 Hint 1.The number of enemies may be negative. 2.Huge input, be careful.

Source 2011 Multi-University Training Contest 13 - Host by HIT 樹剖裸題 然而。。。 技術分享

這一切的禍根是:TM我輸入順序反了!我TM竟然還把樣例過了!

當我發現這一切的時候整個人都Splay了

啊啊啊我TM是個智障!

啊啊啊我TM是個智障!!

啊啊啊我TM是個智障!!!

啊啊啊我TM是個智障!!!!

啊啊啊我TM是個智障!!!!!

啊啊啊我TM是個智障!!!!!!

啊啊啊我TM是個智障!!!!!!!

啊啊啊我TM是個智障!!!!!!!!

啊啊啊我TM是個智障!!!!!!!!!

啊啊啊我TM是個智障!!!!!!!!!!

屠龍寶刀點擊就送

#include <cstring>
#include <cstdio>
#define N 50005
struct Segment
{
    int l,r,sum,flag;
}tr[N*4];
int n,m,p,tdis[N],belong[N],top[N],siz[N],dad[N],dep[N],next[N*2],to[N*2],head[N],cnt,dfn[N],tim;
inline void init()
{
    cnt=tim=0;
    memset(to,0,sizeof(to));
    memset(tr,0,sizeof(tr));
    memset(top,0,sizeof(top));
    memset(siz,0,sizeof(siz));
    memset(dfn,0,sizeof(dfn));
    memset(dep,0,sizeof(dep));
    memset(next,0,sizeof(next));
    memset(head,0,sizeof(head));
    memset(tdis,0,sizeof(tdis));
    memset(belong,0,sizeof(belong));
}
inline void ins(int u,int v)
{
    next[++cnt]=head[u];to[cnt]=v;head[u]=cnt;
    next[++cnt]=head[v];to[cnt]=u;head[v]=cnt;
}
void dfs1(int x)
{
    dep[x]=dep[dad[x]]+1;
    siz[x]=1;
    for(int i=head[x];i;i=next[i])
    {
        if(dad[x]!=to[i])
        {
            dad[to[i]]=x;
            dfs1(to[i]);
            siz[x]+=siz[to[i]];
        }
    }
}
void dfs2(int x)
{
    int pos=0;
    if(!top[x]) top[x]=x;
    dfn[++tim]=x;belong[x]=tim;
    for(int i=head[x];i;i=next[i])
        if(dad[x]!=to[i]&&siz[pos]<siz[to[i]]) pos=to[i];
    if(pos) top[pos]=top[x],dfs2(pos);
    for(int i=head[x];i;i=next[i])
        if(dad[x]!=to[i]&&to[i]!=pos) dfs2(to[i]);
}
inline void pushup(int k) {tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum;}
void build(int k,int l,int r)
{
    tr[k].l=l;tr[k].r=r;
    if(l==r)
    {
        tr[k].sum=tdis[dfn[l]];
        return;
    }
    int mid=(l+r)>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid+1,r);
    pushup(k);
}
inline void swap(int &m,int &n)
{
    int tmp=n;
    n=m;
    m=tmp;
}
inline void pushdown(int k)
{
    tr[k<<1].flag+=tr[k].flag;
    tr[k<<1|1].flag+=tr[k].flag;
    tr[k<<1].sum+=tr[k].flag*(tr[k<<1].r-tr[k<<1].l+1);
    tr[k<<1|1].sum+=tr[k].flag*(tr[k<<1|1].r-tr[k<<1|1].l+1);
    tr[k].flag=0;
}
int query(int k,int t)
{
    if(tr[k].l==tr[k].r) return tr[k].sum;
    if(tr[k].flag) pushdown(k);
    int mid=(tr[k].l+tr[k].r)>>1;
    if(t<=mid) return query(k<<1,t);
    else return query(k<<1|1,t);
}
void Tmodify(int k,int l,int r,int v)
{
    if(tr[k].l==l&&tr[k].r==r)
    {
        tr[k].sum+=v*(r-l+1);
        tr[k].flag+=v;
        return;
    }
    if(tr[k].flag) pushdown(k);
    int mid=(tr[k].l+tr[k].r)>>1;
    if(l>mid) Tmodify(k<<1|1,l,r,v);
    else if(r<=mid) Tmodify(k<<1,l,r,v);
    else Tmodify(k<<1,l,mid,v),Tmodify(k<<1|1,mid+1,r,v);
    pushup(k);
}
inline void Cmodify(int x,int y,int v)
{
    for(;top[x]!=top[y];x=dad[top[x]])
    {
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        Tmodify(1,belong[top[x]],belong[x],v);
    }
    if(dep[x]<dep[y]) swap(x,y);
    Tmodify(1,belong[y],belong[x],v);
}
int main()
{
    for(;scanf("%d%d%d",&n,&m,&p)!=EOF;)
    {
        init();
        for(int i=1;i<=n;++i) scanf("%d",&tdis[i]);
        for(int x,y;m--;)
        {
            scanf("%d%d",&x,&y);
            ins(x,y);
        }
        dfs1(1);
        dfs2(1);
        char opt[5];
        build(1,1,n);
        for(int x,y,z;p--;)
        {
            scanf("%s",opt);
            if(opt[0]==I)
            {
                scanf("%d%d%d",&x,&y,&z);
                Cmodify(x,y,z);
            }
            else if(opt[0]==D)
            {
                scanf("%d%d%d",&x,&y,&z);
                Cmodify(x,y,-z);
            }
            else
            {
                scanf("%d",&x);
                printf("%d\n",query(1,belong[x]));
            } 
        }
    }
    return 0;
}

hdu 3966 Aragorn's Story