HDU3966 Aragorn's Story(树链剖分+线段树)

Aragorn’s Story

传送门1
传送门2
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(1N50000) camps, M(M=N1) edges and P(1P100000) operations. The number of camps starts from 1.

The next line contains N integers A1,A2,...AN(0Ai1000) , 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(0K1000) , 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(0K1000) , 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.

题意

给一棵树,并给定各个点权的值,然后有3种操作:
I C1 C2 K: 把C1与C2的路径上的所有点权值加上K
D C1 C2 K: 把C1与C2的路径上的所有点权值减去K
Q C: 查询节点编号为C的权值

分析

树链剖分+线段树.

CODE

#include<cstdio>
#include<memory.h>
#define N 50005

inline int max(int x,int y) {return x>y?x:y;}
int n,m,Q;
int head[N],to[N<<1],Next[N<<1],tot;

int dep[N],fa[N],sz[N],top[N],son[N],tot2;
int sgID[N],Rank[N],num[N];

void addedge(int u,int v) {
    to[tot]=v,Next[tot]=head[u],head[u]=tot++;
    to[tot]=u,Next[tot]=head[v],head[v]=tot++;
}

void dfs1(int x,int f,int d) {
    fa[x]=f;
    dep[x]=d;
    sz[x]=1;
    for(int i=head[x]; ~i; i=Next[i]) {
        int y=to[i];
        if(y!=f) {
            dfs1(y,x,d+1);
            sz[x]+=sz[y];
            if(son[x]==-1||sz[y]>sz[son[x]])
                son[x]=y;
        }
    }
}

void dfs2(int x,int tp) {
    top[x]=tp;
    sgID[x]=++tot2;
    Rank[tot2]=x;
    if(son[x]==-1) return;
    dfs2(son[x],tp);
    for(int i=head[x]; ~i; i=Next[i]) {
        int y=to[i];
        if(y!=son[x]&&y!=fa[x])
            dfs2(y,y);
    }
}
struct SegMent {
    int sum[N<<2],col[N<<2];
    void UP(int p) {
        sum[p]=max(sum[p<<1],sum[p<<1|1]);
    }
    void Down(int p,int m) {
        if(col[p]) {
            col[p<<1]+=col[p];
            col[p<<1|1]+=col[p];
            sum[p<<1]+=(m-(m>>1))*col[p];
            sum[p<<1|1]+=(m>>1)*col[p];
            col[p]=0;
        }
    }

    void Build(int l,int r,int p) {
        col[p]=0;
        if(l==r) {
            sum[p]=num[Rank[l]];
            return;
        }
        int mid=(l+r)>>1;
        Build(l,mid,p<<1);
        Build(mid+1,r,p<<1|1);
        UP(p);
    }

    void Update(int L,int R,int v,int l,int r,int p) {
        if(L<=l&&R>=r) {
            col[p]+=v;
            sum[p]+=v*(r-l+1);
            return;
        }
        Down(p,r-l+1);
        int mid=(l+r)>>1;
        if(L<=mid)Update(L,R,v,l,mid,p<<1);
        if(R>mid)Update(L,R,v,mid+1,r,p<<1|1);
        UP(p);
    }

    int Query(int l,int r,int p,int val) {
        if(l==r)return sum[p];
        Down(p,r-l+1);
        int mid=(l+r)>>1;
        int ret=0;
        if(val<=mid)ret=Query(l,mid,p<<1,val);
        else ret=Query(mid+1,r,p<<1|1,val);
        UP(p);
        return ret;
    }

} T;
void Change(int x,int y,int val) {
    while(top[x]!=top[y]) {
        if(dep[top[x]]<dep[top[y]]) {
            T.Update(sgID[top[y]],sgID[y],val,1,n,1);
            y=fa[top[y]];
        } else {
            T.Update(sgID[top[x]],sgID[x],val,1,n,1);
            x=fa[top[x]];
        }
    }
    if(dep[x]>dep[y])T.Update(sgID[y],sgID[x],val,1,n,1);
    else T.Update(sgID[x],sgID[y],val,1,n,1);
}

int main() {
    char oper[5];
    int a,b,c;
    while(~scanf("%d%d%d",&n,&m,&Q)) {
        memset(head,-1,sizeof head);
        memset(son,-1,sizeof son);
        tot2=0;
        tot=0;
        for(int i=1; i<=n; i++)
            scanf("%d",&num[i]);
        for(int i=1; i<=m; i++) {
            scanf("%d%d",&a,&b);
            addedge(a,b);
        }
        dfs1(1,0,0);
        dfs2(1,1);
        T.Build(1,n,1);
        while(Q--) {
            scanf("%s",oper);
            if(oper[0]=='Q') {
                scanf("%d",&a);
                printf("%d\n",T.Query(1,n,1,sgID[a]));
            } else {
                scanf("%d%d%d",&a,&b,&c);
                if(oper[0]=='D') c=-c;
                Change(a,b,c);
            }
        }
    }
    return 0;
}
发布了34 篇原创文章 · 获赞 44 · 访问量 5078

猜你喜欢

转载自blog.csdn.net/zzyyyl/article/details/78422391