HDU3966 Aragorn's Story 树链剖分

裸地不能在裸的树链剖分了,col忘记清空,狂wa一小时。

本来想学LINK CUT TREE的,考试接踵,做题状态全无。== ==暂时顺延了


Aragorn's Story

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


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
2011 Multi-University Training Contest 13 - Host by HIT

#include
#include
#include
#include
#include

using namespace std;

#define INF 0x3ffffff
#define MAXN 100010

struct node
{
    int to,next;
}edge[9999999];

int head[MAXN],en;
int belong[MAXN],idx[MAXN],qid[MAXN],fi[MAXN];
int top[MAXN],len[MAXN];
int dep[MAXN],fat[MAXN],size[MAXN];
int Q[MAXN],vis[MAXN];
int n,m,k,cnt;
int a[MAXN],f[MAXN];

void add(int u,int v)
{
    edge[en].to=v;
    edge[en].next=head[u];
    head[u]=en++;
}

void split()
{
    int l,r;
    memset(dep,-1,sizeof(dep));
    l=0;
    dep[Q[r=1]=1]=0;
    fat[1]=-1;
    while(lsize[p])
                    p=edge[y].to;
            }
        if(p==-1)
        {
            idx[x]=len[++cnt]=1;
            belong[top[cnt]=x]=cnt;
        }
        else
        {
            idx[x]=++len[belong[x]=belong[p]];
            top[belong[x]]=x;
        }
        vis[x]=true;
    }
}

void getqid()
{
    fi[1]=1;
    for(int i=2;i<=cnt;i++)
        fi[i]=fi[i-1]+len[i-1];
    for(int i=1;i<=n;i++)
    {
        int blo=belong[i];
        qid[i]=fi[blo]+len[blo]-idx[i];
        f[qid[i]]=i;
    }
}

void update(int l,int r,int root,int L,int R,int d);
void change(int x,int y,int d)
{
    while(belong[x]!=belong[y])
    {
        if(dep[top[belong[x]]]dep[y]) swap(x,y);
    update(1,n,1,qid[x],qid[y],d); //其他数据结构维护区间值
}



#define lson l,m,root<<1
#define rson m+1,r,root<<1|1

int node[MAXN<<2],col[MAXN<<2];

void push_up(int root)
{
    node[root]=max(node[root<<1],node[root<<1|1]);
}

void push_down(int root,int m)
{
    if(col[root])
    {
        col[root<<1|1]+=col[root];
        col[root<<1]+=col[root];
        node[root<<1]=node[root<<1]+(m-(m>>1))*col[root];
        node[root<<1|1]=node[root<<1|1]+(m>>1)*col[root];
        col[root]=0;
    }

}

void build(int l,int r,int root)
{
    col[root]=0;
    if(l==r)
    {
        node[root]=a[f[l]];
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(root);
}

void update(int l,int r,int root,int L,int R,int d)
{
    if(l>=L&&r<=R)
    {
        node[root]+=(r-l+1)*d;
        col[root]+=d;
        return ;
    }
    int m=(l+r)>>1;
    push_down(root,r-l+1);
    if(L<=m) update(lson,L,R,d);
    if(R>m)  update(rson,L,R,d);
    push_up(root);
}

int query(int l,int r,int root,int a)
{
    if(l==r) return node[root];
    int m=(l+r)>>1;
    push_down(root,r-l+1);
    int ret=0;
    if(a<=m)
        ret= query(lson,a);
    else
        ret= query(rson,a);
    push_up(root);
    return ret;
}

int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        int u,v,x;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(head,-1,sizeof(head));en=0;
        for(int i=0;i


你可能感兴趣的:(数据结构,树链剖分)