hdu 3966(树链剖分)

Aragorn's Story

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


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.
 


 //树链剖分

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include 
using namespace std;
const int N=5e5+5;

int num[N],index,ans;//点值 线段树下标 深度

vectorG[N];
/*--------------树链剖分部分--------------------*/

int Sz[N],dth[N],son[N],top[N],fa[N],lgt[N],flgt[N];
/*
    Sz[i] 以i节点为根的子树节点数
    dth[i] 节点i的深度
    sun[i] 节点i的重儿子
    top[i] i所在链顶端的节点
    fa[i] i节点的父亲
    lgt[i] i节点对应在线段树的编号
    flgt[i] 编号为i的对应节点
*/

void dfs1(int u,int f)
{
    int tol=1;
    for(auto it:G[u])
    {
        if(it!=f)
        {
            dth[it]=dth[u]+1,fa[it]=u;
            dfs1(it,u);
            tol+=Sz[it];
            if(Sz[son[u]]>1;
    if(l==r)seg[rt].val=num[flgt[l]];
    else build(lson),build(rson);
}

void update(int rt,int l,int r,int k)
{
    if(seg[rt].l==l&&seg[rt].r==r)
    {
        seg[rt].val+=k,seg[rt].mark+=k;
        return;
    }
    pushdown(rt);
    int mid=(seg[rt].l+seg[rt].r)>>1;
    if(r<=mid)update(2*rt,l,r,k);
    else if(l>mid)update(2*rt+1,l,r,k);
    else update(lson,k),update(rson,k);
}

void query(int rt,int k)
{
    if(seg[rt].l==seg[rt].r&&seg[rt].l==k)
    {
        ans=seg[rt].val;
        return;
    }
    pushdown(rt);
    int mid=(seg[rt].l+seg[rt].r)>>1;
    if(k<=mid)query(2*rt,k);
    else if(k>mid)query(2*rt+1,k);
}

void change(int u,int v,int k)
{
    int tp1=top[u],tp2=top[v];
    while(tp1!=tp2)
    {
        if(dth[tp1]dth[v])swap(u,v);
    update(1,lgt[u],lgt[v],k);
}

int main()
{
    int n,m,p,u,v;
    while(~scanf("%d%d%d",&n,&m,&p))
    {
        for(int i=0;i<=n;i++) //初始化
        {
            G[i].clear();
            lgt[i]=flgt[i]=Sz[i]=son[i]=dth[i]=fa[i]=top[i]=0;
        }
        for(int i=1;i<=n;i++)scanf("%d",num+i);
        for(int i=1;i


你可能感兴趣的:(数据结构)