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): 2251    Accepted Submission(s): 614


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

题意:给一棵树,树上的节点有权值,

给三种操作I x y k 在x到y的路径上每个点增加k

d x y k 在x到y的路径上每个节点减少k

q x 查询x节点的值

很明显的树链剖分,

树链剖分的内容查看:http://blog.sina.com.cn/s/blog_7a1746820100wp67.html



#pragma comment(linker,"/STACK:100000000,100000000")
#include
#include
#include
#include
#include
using namespace std;
#define maxn 70007
#define lc u<<1
#define rc u<<1|1
#define ll long long
struct Node{
    int l,r,lz;
};
Node node[maxn<<2];
struct Edge{
    int v,next;
};
Edge edge[maxn*4];
int cnt,head[maxn];
void add_edge(int u,int v){
    edge[cnt].v = v, edge[cnt].next=head[u];
    head[u] = cnt++;
    edge[cnt].v = u, edge[cnt].next=head[v];
    head[v] = cnt++;
}
int num[maxn],res[maxn],flag,check[maxn];
int fa[maxn],son[maxn],id[maxn],height[maxn],top[maxn];
void dfs1(int u,int f){
    if(check[u]) return ;
    check[u] =1;
    height[u] = height[f]+1;
    fa[u] = f;
    for(int i = head[u];i != -1; i=edge[i].next){
        if(check[edge[i].v]!=0)continue;
        dfs1(edge[i].v,u);
        if(height[edge[i].v] > height[son[u]]) son[u]=edge[i].v;
    }
}
void dfs2(int u,int t){
    if(check[u]) return ;
    check[u] = 1;
    top[u] = t;
    id[u] = flag++;
    res[id[u]]=num[u];
    if(son[u] != 0) dfs2(son[u],t);
    for(int i=head[u];i!=-1;i=edge[i].next){
        dfs2(edge[i].v,edge[i].v);
    }
}
void build(int u,int l,int r){
    node[u].l = l,node[u].r = r;
    node[u].lz = 0;
    if(l == r) {
        node[u].lz = res[l];
        return ;
    }
    int mid=(l+r)/2;
    build(lc,l,mid);
    build(rc,mid+1,r);
}
void push(int u){
    node[lc].lz+=node[u].lz;
    node[rc].lz+=node[u].lz;
    node[u].lz=0;
}
void add(int u,int l,int r,int d){
    if(node[u].l == l && node[u].r == r){
        node[u].lz+=d;
        return ;
    }
    int mid = (node[u].l+node[u].r)/2;
    push(u);
    if(mid < l) add(rc,l,r,d);
    else if(mid>=r)add(lc,l,r,d);
    else add(lc,l,mid,d),add(rc,mid+1,r,d);
}
int getsum(int u,int p){
    if(node[u].l == p && node[u].r==p)
        return node[u].lz;
    push(u);
    int mid=(node[u].l+node[u].r)/2;
    if(mid v) swap(u,v);
                add(1,u,v,k);
            }
        }
    }
    return 0;
}


你可能感兴趣的:(##ACM-ICPC编程题,##数据结构,ACM之数据结构)