zoukankan      html  css  js  c++  java
  • HDU 3966 Aragorn's Story

    Aragorn's Story

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

    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
     思路:树链剖分板子。G++ gg  C++  AC
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define MAXN 500000
    using namespace std;
    int n,m,p,sz,tot;
    int w[MAXN];
    int to[MAXN],head[MAXN],net[MAXN];
    int id[MAXN],top[MAXN],dad[MAXN],size[MAXN],deep[MAXN];
    struct nond{
        int l,r;
        int sum,flag;
    }tree[MAXN];
    void add(int u,int v){
        to[++tot]=v;net[tot]=head[u];head[u]=tot;
        to[++tot]=u;net[tot]=head[v];head[v]=tot;
    }
    void up(int now){
        tree[now].sum=tree[now*2].sum+tree[now*2+1].sum;
    }
    void build(int now,int l,int r){
        tree[now].l=l;
        tree[now].r=r;
        if(tree[now].l==tree[now].r)
            return ;
        int mid=(tree[now].l+tree[now].r)/2;
        build(now*2,l,mid);
        build(now*2+1,mid+1,r);
    }
    void down(int now){
        tree[now*2].flag+=tree[now].flag;
        tree[now*2+1].flag+=tree[now].flag;
        tree[now*2].sum+=tree[now].flag*(tree[now*2].r-tree[now*2].l+1);
        tree[now*2+1].sum+=tree[now].flag*(tree[now*2+1].r-tree[now*2+1].l+1);
        tree[now].flag=0;
    }
    void change(int now,int l,int r,int k){
        if(tree[now].l==l&&tree[now].r==r){
            tree[now].sum+=k*(tree[now].r-tree[now].l+1);
            tree[now].flag+=k;
            return ;
        }
        if(tree[now].flag)    down(now);
        int mid=(tree[now].l+tree[now].r)/2;
        if(r<=mid)    change(now*2,l,r,k);
        else if(l>mid)    change(now*2+1,l,r,k);
        else{
            change(now*2,l,mid,k);
            change(now*2+1,mid+1,r,k);
        }
        up(now);
    }
    int query(int now,int x){
        if(tree[now].l==tree[now].r)
            return tree[now].sum;
        if(tree[now].flag)    down(now);
        int mid=(tree[now].l+tree[now].r)/2;
        if(x<=mid)    return query(now*2,x);
        else if(x>mid)    return query(now*2+1,x);
    }
    void dfs(int x){
        size[x]=1;
        deep[x]=deep[dad[x]]+1;
        for(int i=head[x];i;i=net[i])
            if(dad[x]!=to[i]){
                dad[to[i]]=x;
                dfs(to[i]);
                size[x]+=size[to[i]];
            }
    }
    void dfs1(int x){
        int t=0;
        id[x]=++sz;
        if(!top[x])    top[x]=x;
        change(1,sz,sz,w[x]);
        for(int i=head[x];i;i=net[i])
            if(dad[x]!=to[i]&&size[t]<size[to[i]])
                t=to[i];
        if(t){
            top[t]=top[x];
            dfs1(t);
        }
        for(int i=head[x];i;i=net[i])
            if(dad[x]!=to[i]&&t!=to[i])
                dfs1(to[i]);
    }
    void schange(int x,int y,int z){
        while(top[x]!=top[y]){
            if(deep[top[x]]<deep[top[y]])    swap(x,y);
            change(1,id[top[x]],id[x],z);
            x=dad[top[x]];
        }
        if(id[x]>id[y])    swap(x,y);
        change(1,id[x],id[y],z);
    }
    int main(){
        while(scanf("%d%d%d",&n,&m,&p)!=EOF){
            for(int i=1;i<=n;i++)    cin>>w[i];
            for(int i=1;i<=m;i++){
                int u,v;
                scanf("%d%d",&u,&v);
                add(u,v);
            }
            build(1,1,n);dfs(1);dfs1(1);
            for(int i=1;i<=p;i++){
                char opt[10];int x,y,z;
                scanf("%s%d",opt,&x);
                if(opt[0]=='I'){
                    scanf("%d%d",&y,&z);
                    schange(x,y,z);
                } else if(opt[0]=='D'){
                    scanf("%d%d",&y,&z);
                    schange(x,y,-z);
                } 
                else    printf("%d
    ",query(1,id[x]));
            }
            tot=0;sz=0;
            memset(w,0,sizeof(w));
            memset(id,0,sizeof(id));
            memset(to,0,sizeof(to));
            memset(dad,0,sizeof(dad));
            memset(net,0,sizeof(net));
            memset(top,0,sizeof(top));
            memset(deep,0,sizeof(deep));
            memset(head,0,sizeof(head));
            memset(size,0,sizeof(size));
            memset(tree,0,sizeof(tree));
        }
    }
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    activemq 异步和同步接收
    简单的activemq的封装和使用
    activemq 一个不错的ppt
    activemq 的小实验
    activemq api的封装
    观察者设计模式例子
    【转载】自定义类加载器例子
    京东拍拍网 笔试 搞java的去考C++ 苦逼
    java中用equals比较两个内容相同的字符数组
    云端飘 String s=new String("abc")创建了几个对象?
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/8486760.html
Copyright © 2011-2022 走看看