码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3966 Aragorn's Story

时间:2014-08-16 23:48:41      阅读:443      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

Aragorn‘s Story

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

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
 
Recommend
We have carefully selected several similar problems for you:  3964 3965 3962 3963 3967 
 
 
 
树状数组 + 树链剖分~~
 
 
 
#pragma comment(linker, "/STACK:1024000000,1024000000")  
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

#define lson rt<<1
#define rson rt<<1|1

typedef long long LL;
const int N = 50005;

int n,m,q;
int tim;
int num[N],size[N],top[N],son[N];
int dep[N],tid[N],rnk[N],fa[N];
int eh[N],to[2*N],nxt[2*N],edge;

void addedge(int u,int v)
{
    to[edge]=v;nxt[edge]=eh[u];eh[u]=edge++;
    to[edge]=u;nxt[edge]=eh[v];eh[v]=edge++;
}
void dfs1(int u,int father,int d)
{
    dep[u]=d;
    fa[u]=father;
    size[u]=1;
    for(int i = eh[u] ; ~i ;i = nxt[i] )
    {
        int v=to[i];
        if( v != father ){
            dfs1( v, u ,d+1 );
            size[u] += size[v];
            if( son[u] == -1 || size[v] >size[son[u]] )
                son[u]=v;
        }
    }
}
void dfs2(int u,int tp)
{
    top[u]=tp;
    tid[u] = ++tim;
    rnk[ tid[u] ]=u;
    if( son[u] == -1 )return ;
    dfs2( son[u],tp);
    for(int i=eh[u];~i;i=nxt[i])
    {
        int v=to[i];
        if( v != son[u] && v != fa[u] )
            dfs2(v,v);
    }
}

LL c[N];

int lowbit(int x){ return x&-x; }

void add(int pos,int key)
{
    while(pos <= n + 3)
    {
        c[pos] += key;
        pos += lowbit(pos);
    }
}

void update(int u,int v,int val)
{

    int f1=top[u],f2=top[v];
    while(f1 != f2){
        if(dep[f1] < dep[f2]){
            swap(f1,f2);
            swap(u,v);
        }
        add(tid[f1],val);
        add(tid[u]+1,-val);
        u=fa[f1];
        f1=top[u];
    }
    if( dep[u] > dep[v] )swap(u,v);
    add(tid[u],val);
    add(tid[v]+1,-val);

}

LL query(int pos){

    LL res=0;
    while(pos>0){
        res += c[pos];
        pos -= lowbit(pos);
    }
    return res;
}


void init()
{
    memset(c,0,sizeof(c));
    memset(eh,-1,sizeof(eh));
    memset(son,-1,sizeof(son));
    tim=1;                    //树状数组坐标从1开始!!
    edge=0;         
}

int main(void)
{
    char op[5];
    int u,v,c;
    
    freopen("in.txt","r",stdin);

    while(~scanf("%d%d%d",&n,&m,&q))
    {
        init();
        for(int i=1;i<=n;++i)
            scanf("%d",&num[i]);

        for(int i=1;i<=m;++i){
            scanf("%d%d",&u,&v);
            addedge(u,v);
        }
        dfs1(1,0,0);
        dfs2(1,1);
        for(int i=1;i<=n;++i){
            add(tid[i],num[i]);
            add(tid[i]+1,-num[i]);
        }
        while(q--){
            scanf("%s",op);
            if( op[0] == Q){
                scanf("%d",&u);
                printf("%lld\n",query(tid[u]));
                continue;
            }
            scanf("%d%d%d",&u,&v,&c);
            if(op[0]==D)c=-c;
            update(u,v,c);
        }
    }
    return 0;
}

 

 
 

HDU 3966 Aragorn's Story,布布扣,bubuko.com

HDU 3966 Aragorn's Story

标签:des   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/YRETSIM/p/3917082.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!