HDU 3966Aragorn's Story(树链剖分)点权更新模板

Aragorn‘s Story

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4434    Accepted Submission(s): 1212

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

一开始提交,爆栈,后在网上看到:

题意:给一棵树,并给定各个点权的值,然后有3种操作:

I C1 C2 K: 把C1与C2的路径上的所有点权值加上K

D C1 C2 K:把C1与C2的路径上的所有点权值减去K

Q C:查询节点编号为C的权值

分析:典型的树链剖分题目,先进行剖分,然后用线段树去维护即可,注意HDU的OJ采用Windows系统,容易爆栈,所以在代码

前面加上:#pragma comment(linker, "/STACK:1024000000,1024000000")进行手动扩栈。

#pragma comment(linker, "/STACK:1024000000,1024000000") //因OJ采用Windows系统,要加入这一行用于 进行手动扩栈,这样就不会引起爆栈
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
const int N = 50015;

vector<int>mapt[N];
int fath[N],deep[N],top[N],num[N],son[N],p[N],pos;

void init(int n)
{
    pos=0;
    //memset(son,-1,sizeof(son));
    for(int i=0;i<=n;i++)
        mapt[i].clear();
}
void dfs(int u,int pre,int d)
{
    fath[u]=pre; num[u]=1; deep[u]=d; son[u]=-1;
    int k=mapt[u].size();
    for(int i=k-1;i>=0;--i)
    {
        int v=mapt[u][i];
        if(v==pre)continue;
        dfs(v,u,d+1);
        num[u]+=num[v];
        if(son[u]==-1||num[son[u]]<num[v])
            son[u]=v;
    }
}
void getpos(int u,int root)
{
    top[u]=root;
    p[u]=++pos;//从1开始
    if(son[u]==-1)
        return ;
    getpos(son[u],root);
    int k=mapt[u].size();
    for(int i=k-1;i>=0;--i)
    {
        int v=mapt[u][i];
        if(son[u]==v||v==fath[u])
            continue;
        getpos(v,v);
    }
}

struct tree
{
    int allnum,addnum;
}root[N*3];
int a[N];

void build(int l,int r,int k)
{
    root[k].addnum=0;
    if(l==r){
        root[k].allnum=a[l]; return ;
    }
    int mid=(l+r)/2;
    build(l,mid,k<<1);
    build(mid+1,r,(k<<1)|1);
    root[k].allnum=root[k<<1].allnum+root[(k<<1)|1].allnum;
}
void upson(int l,int r,int k)
{
    int mid=(l+r)/2;
    if(root[k].addnum){
        root[k<<1].allnum+=(mid-l+1)*root[k].addnum;
        root[k<<1].addnum+=root[k].addnum;

        root[(k<<1)|1].allnum+=(r-mid)*root[k].addnum;
        root[(k<<1)|1].addnum+=root[k].addnum;
        root[k].addnum=0;
    }
}
void update(int l,int r,int k,int L,int R,int c)
{
    if(L<=l&&r<=R)
    {
        root[k].allnum+=(r-l+1)*c; root[k].addnum+=c; return ;
    }
    int mid=(l+r)/2;
    upson(l,r,k);
    if(L<=mid) update(l,mid,k<<1,L,R,c);
    if(mid<R)  update(mid+1,r,(k<<1)|1,L,R,c);
    root[k].allnum=root[k<<1].allnum+root[(k<<1)|1].allnum;
}
int query(int l,int r,int k,int id)
{
    if(l==r)
        return root[k].allnum;
    upson(l,r,k);
    int mid=(l+r)/2;
    if(id<=mid)
        return query(l,mid,k<<1,id);
    else return query(mid+1,r,(k<<1)|1,id);
}

void swp(int &a,int &b)
{
    int tt=a; a=b; b=tt;
}
void Operat(int u,int v,int c)
{
    int fu=top[u], fv=top[v];
    while(fu!=fv)
    {
        if(deep[fu]<deep[fv])
        {
            swp(fu,fv); swp(u,v);
        }
        update(1,pos,1,p[fu],p[u],c);
        u=fath[fu]; fu=top[u];
    }
    if(deep[u]>deep[v])
        swp(u,v);
    update(1,pos,1,p[u],p[v],c);//点权更新与边权更新的区别之一:点p[u],边p[son[u]]
}

int main()
{
    int n,m,q,val[N];
    while(scanf("%d%d%d",&n,&m,&q)>0)
    {
        init(n);
        for(int i=1;i<=n;i++)
            scanf("%d",&val[i]);
        while(m--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            mapt[u].push_back(v);
            mapt[v].push_back(u);
        }
        dfs(1,1,1);
        getpos(1,1);
        for(int i=1;i<=n;i++)
            a[p[i]]=val[i]; //一定要注意转换成在线段树上的对应位置
        pos=n;
        build(1,pos,1);

        while(q--)
        {
            int u,v,c;
            char ch[10];
            scanf("%s%d",ch,&u);
            if(ch[0]=='Q')
                printf("%d\n",query(1,pos,1,p[u]));
            else
            {
                scanf("%d%d",&v,&c);
                if(ch[0]=='D') c=-c;
                Operat(u,v,c);
            }
        }
    }
    return 0;
}

HDU 3966Aragorn's Story(树链剖分)点权更新模板

时间: 2024-08-05 04:52:03

HDU 3966Aragorn's Story(树链剖分)点权更新模板的相关文章

hdu 5242 树链剖分找权值最大的前k条链

http://acm.hdu.edu.cn/showproblem.php?pid=5242 Problem Description It is well known that Keima Katsuragi is The Capturing God because of his exceptional skills and experience in ''capturing'' virtual girls in gal games. He is able to play k games sim

HDU Aragorn&#39;s Story -树链剖分

HDU Aragorn's Story 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 k

hdu3966 树链剖分(区间更新和单点求值)

http://acm.hdu.edu.cn/showproblem.php?pid=3966 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 ene

BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分 - 点权剖分 - 单点权修改)

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分模版题,打的时候注意点就行.做这题的时候,真的傻了,单词拼错检查了一个多小时... 代码如下: 1 //树链剖分 点权修改 修改单节点 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 #include <cstdio> 6 using namespa

poj 2763 树链剖分(单点更新,区间求值)

http://poj.org/problem?id=2763 Description After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional ro

HDU3966 Aragorn&#39;s Story(树链剖分 点权 模版题)

#include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <map> #include <set> #include <vector> #include <cstdio> using namespace std; const int N=50010; s

HYSBZ 2243 树链剖分(区间更新,区间查询)较难

http://www.lydsy.com/JudgeOnline/problem.php?id=2243 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如"112221"由3段组成:"11"."222"和"1". 请你写一个程序依次完成这m个操作. Input 第一行包含2个整数

poj 3237 树链剖分(区间更新,区间查询)

http://poj.org/problem?id=3237 Description You are given a tree with N nodes. The tree's nodes are numbered 1 through N and its edges are numbered 1 through N ? 1. Each edge is associated with a weight. Then you are to execute a series of instruction

spoj375 树链剖分(单点更新,区间查询)

http://www.spoj.com/problems/QTREE/ QTREE - Query on a tree no tags You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANG

light oj 1348 树链剖分(单点更新区间求值)

http://lightoj.com/volume_showproblem.php?problem=1348 Finally the Great Magical Lamp was in Aladdin's hand. Now he wanted to return home. But he didn't want to take any help from the Genie because he thought that it might be another adventure for hi