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): 12874    Accepted Submission(s): 3434


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 x y w表示将从x到y的路径上的所有点都增加w权值

②D x y w表示将从x到y的路径上的所有点都减少w权值

③Q x 表示询问点x当前的权值。


思路:


树链剖分的模板题。没有难度,初学入门推荐选择。


Ac代码:


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int cnt,n,m,q;
vector<int>mp[55000];
int son[55000];
int depth[55000];
int fa[55000];
int size[55000];
int dfn[55000];
int Top[55000];
int val[55000];
/***************************/
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
int tree[55000*10];
int flag[55000*10];
void pushdown(int l,int r,int rt)//向下维护树内数据
{
    if(flag[rt])//如果贪婪标记不是0(说明需要向下进行覆盖区间(或点)的值)
    {
        int m=(l+r)/2;
        flag[rt*2]+=flag[rt];
        flag[rt*2+1]+=flag[rt];
        tree[rt*2]+=(m-l+1)*flag[rt];//千万理解如何覆盖的区间值(对应线段树的图片理解(m-l)+1)是什么意识.
        tree[rt*2+1]+=(r-(m+1)+1)*flag[rt];
        flag[rt]=0;
    }
}
void pushup(int rt)
{
    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R)//覆盖的是区间~
    {
        tree[rt]+=c*((r-l)+1);//覆盖当前点的值
        flag[rt]+=c;//同时懒惰标记~!
        return ;
    }
    pushdown(l,r,rt);
    int m=(l+r)/2;
    if(L<=m)
    {
        update(L,R,c,lson);
    }
    if(m<R)
    {
        update(L,R,c,rson);
    }
    pushup(rt);
}
int Query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return tree[rt];
    }
    else
    {
        pushdown(l,r,rt);
        int m=(l+r)>>1;
        int ans=0;
        if(L<=m)
        {
            ans+=Query(L,R,lson);
        }
        if(m<R)
        {
            ans+=Query(L,R,rson);
        }
        pushup(rt);
        return ans;
    }
}
void build( int l ,int r , int rt )
{
    flag[rt]=0;
    if( l == r )
    {
        flag[rt]=0;
        tree[rt]=0;
        return ;
    }
    else
    {
        int m = (l+r)>>1 ;
        build(lson) ;
        build(rson) ;
        pushup(rt) ;
        return ;
    }
}
/***************************/
void Dfs(int u,int from,int d)
{
    depth[u]=d;size[u]=1;fa[u]=from;
//    if(fa[u]==-1)fa[u]=1;
    int maxn=0;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==from)continue;
        Dfs(v,u,d+1);
        size[u]+=size[v];
        if(son[u]==-1||size[v]>maxn)
        {
            maxn=size[v];
            son[u]=v;
        }
    }
}
void Dfs2(int u,int from,int top)
{
    Top[u]=top;dfn[u]=++cnt;
    if(son[u]!=-1)
    {
        Dfs2(son[u],u,top);
    }
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==from||v==son[u])continue;
        Dfs2(v,u,v);
    }
}
void Slove(int x,int y,int change)
{
    int fx=Top[x];
    int fy=Top[y];
    while(fx!=fy)
    {
        if(depth[fx]<depth[fy])
        {
            swap(fx,fy);
            swap(x,y);
        }
        update(dfn[fx],dfn[x],change,1,n,1);
        x=fa[fx];fx=Top[x];
    }
    if(depth[x]>depth[y])
    {
        swap(x,y);
    }
    update(dfn[x],dfn[y],change,1,n,1);
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&q))
    {
        cnt=0;

        memset(son,-1,sizeof(son));
        for(int i=1;i<=n;i++)scanf("%d",&val[i]);
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=1;i<=m;i++)
        {
            int x,y;scanf("%d%d",&x,&y);
            mp[x].push_back(y);
            mp[y].push_back(x);
        }
        Dfs(1,-1,1);
        Dfs2(1,-1,1);
        build(1,n,1);
        for(int i=1;i<=n;i++)update(dfn[i],dfn[i],val[i],1,n,1);
        while(q--)
        {
            char s[5];
            scanf("%s",s);
            if(s[0]=='Q')
            {
                int x;
                scanf("%d",&x);
                printf("%d\n",Query(dfn[x],dfn[x],1,n,1));
            }
            else
            {
                int x,y,w;
                scanf("%d%d%d",&x,&y,&w);
                if(s[0]=='I')Slove(x,y,w);
                else Slove(x,y,-w);
            }
        }
    }
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值