HDU3966 Aragorn's Story

HDU3966 Aragorn's Story

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


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
 
 
----------------------------------------------------------------------------------------------------------------------------- 
题目大意:一颗树上,每个点有权值,定义三种操作:I操作表示从a到b节点之间的节点都加上一个值,D操作表示从a到b节点之间的节点的都减去一个权值,Q操作询问a节点当前的值。
解题思路:简单树链剖分题。做过QTREE系列表示毫无压力。
#pragma comment(linker,"/STACK:100000000,100000000")
#include <stdio.h>
#include <string.h>
#include <vector>
#define clr(a,b) memset(a,b,sizeof(a))
#define L(a) (a<<1)
#define R(a) (a<<1|1)
using namespace std;

const int N=100005,M=7*N,inf=0x3f3f3f3f;
int n,m,q,cnum,pnum;
vector<int>gra[N];
int son[N],fa[N],top[N],siz[N],val[N],dep[N];
int cp[N],cd[N],csz[N],pn[N],cid[M];
int le[M],ri[M],det[M];

void dfs_1(int s,int f,int d){
    siz[s]=1;top[s]=s;fa[s]=f;dep[s]=d;
    int maxx=0,pos=-1,len=gra[s].size();
    for(int i=0;i<len;i++){
        int e=gra[s][i];if(e==f)continue;
        dfs_1(e,s,d+1);siz[s]+=siz[e];
        if(siz[e]>maxx){maxx=siz[e];pos=e;}
    }son[s]=pos;
}

void build(int p,int rt,int l,int r){
    int root=p+rt,mid=(l+r)>>1;
    le[root]=l;ri[root]=r;
    det[root]=0;
    if(l==r){
        det[root]=val[cid[p+l]];return ;
    }
    build(p,L(rt),l,mid);
    build(p,R(rt),mid+1,r);
}

void update(int p,int rt,int l,int r,int v){
    int root=p+rt,mid=(le[root]+ri[root])>>1;
    if(le[root]==l&&ri[root]==r){
        det[root]+=v;return ;
    }
    if(det[root]){
        update(p,L(rt),le[root],mid,det[root]);
        update(p,R(rt),mid+1,ri[root],det[root]);
        det[root]=0;
    }
    if(r<=mid)update(p,L(rt),l,r,v);
    else if(l>mid)update(p,R(rt),l,r,v);
    else{
        update(p,L(rt),l,mid,v);
        update(p,R(rt),mid+1,r,v);
    }
}

int query(int p,int rt,int x){
    int root=p+rt,l=le[root],r=ri[root];
    int mid=(l+r)>>1;
    if(l==x&&r==x)return det[root];
    if(x<=mid)return query(p,L(rt),x)+det[root];
    else return query(p,R(rt),x)+det[root];
}

void dfs_2(int s,int d){
    if(top[s]==s){
        cp[s]=++cnum;pn[cnum]=pnum;
    }
    cp[s]=cp[top[s]];int k=cp[s],len=gra[s].size();
    cd[s]=d;cid[pn[k]+d]=s;csz[k]=d;
    if(~son[s]){
        top[son[s]]=top[s];dfs_2(son[s],d+1);
    }
    else{
        pnum+=6*d;build(pn[k],1,1,d);
    }
    for(int i=0;i<len;i++){
        int e=gra[s][i];
        if(e!=fa[s]&&e!=son[s])dfs_2(e,1);
    }
}

int main(){
//    freopen("/home/axorb/in","r",stdin);
    while(scanf("%d%d%d",&n,&m,&q)==3){
        for(int i=1;i<=n;i++){
            scanf("%d",&val[i]);gra[i].clear();
        }
        for(int i=1;i<n;i++){
            int a,b;scanf("%d%d",&a,&b);
            gra[a].push_back(b);gra[b].push_back(a);
        }
        cnum=pnum=0;dfs_1(1,-1,1);dfs_2(1,1);
        while(q--){
            char s[10];scanf("%s",s);int a,b,c;
            if(s[0]!='Q'){
                scanf("%d%d%d",&a,&b,&c);
                if(s[0]=='D')c=-c;
                int flag=0;
                while(top[a]!=top[b]){
                    int &x=dep[top[a]]>dep[top[b]]?a:b;
                        update(pn[cp[x]],1,1,cd[x],c);x=fa[top[x]];
                }
                int x=a,y=b;if(cd[x]>cd[y])swap(x,y);
                update(pn[cp[x]],1,cd[x],cd[y],c);
            }
            else{
                scanf("%d",&a);
                printf("%d\n",query(pn[cp[a]],1,cd[a]));
            }
        }
    }
}

  

也许有挫折,但这些,怎能挡住湘北前进的步伐
原文地址:https://www.cnblogs.com/Fatedayt/p/2587018.html