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): 2526    Accepted Submission(s): 709

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

Recommend

We have carefully selected several similar problems for you:  3964 3965 3962 3963 3967

题意:

给你一棵树。你可以对树进行三种操作。

1.给树上的路径u->v上所有点加上一个值。

2.。。。。。。。。。。。。减去一个值。

3.询问树上某个节点v的值。

思路:

裸的树链剖分。

详细见代码:

#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=50010;
typedef long long ll;
#define lson L,mid,ls
#define rson mid+1,R,rs
int sz[maxn],dep[maxn],fa[maxn],son[maxn],id[maxn],top[maxn];
int add[maxn<<2],val[maxn],arr[maxn],cnt,ptr;
struct node
{
    int v;
    node *next;
} ed[maxn<<1],*head[maxn];
void adde(int u,int v)
{
    ed[cnt].v=v;
    ed[cnt].next=head[u];
    head[u]=&ed[cnt++];
}
void build(int L,int R,int rt)
{
    add[rt]=0;
    if(L==R)
    {
        add[rt]=val[L];
        return;
    }
    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
    build(lson);
    build(rson);
}
void update(int L,int R,int rt,int l,int r,int d)
{
    if(l<=L&&R<=r)
    {
        add[rt]+=d;
        return;
    }
    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
    if(l<=mid)
        update(lson,l,r,d);
    if(r>mid)
        update(rson,l,r,d);
}
int qu(int L,int R,int rt,int p)
{
    if(L==R)
        return add[rt];
    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
    if(p<=mid)
        return qu(lson,p)+add[rt];
    else
        return qu(rson,p)+add[rt];
}
void dfs(int u)
{
    int v,ms=0;
    sz[u]=1,son[u]=-1;
    for(node *p=head[u];p!=NULL;p=p->next)
    {
        v=p->v;
        if(v==fa[u])
            continue;
        dep[v]=dep[u]+1;
        fa[v]=u;
        dfs(v);
        if(sz[v]>ms)
            ms=sz[v],son[u]=v;
        sz[u]+=sz[v];
    }
}
void getid(int u,int ft)
{
    id[u]=++ptr,top[u]=ft;
    val[ptr]=arr[u];
    if(son[u]!=-1)
        getid(son[u],top[u]);
    for(node *p=head[u];p!=NULL;p=p->next)
    {
        if(p->v==son[u]||p->v==fa[u])
            continue;
        getid(p->v,p->v);
    }
}
void init(int rt)
{
    fa[rt]=-1;
    dep[rt]=ptr=0;
    dfs(rt);
    getid(rt,rt);
}
void uppath(int u,int v,int d)
{
    int f1=top[u],f2=top[v];
    while(f1!=f2)
    {
        if(dep[f1]<dep[f2])
            swap(f1,f2),swap(u,v);
        update(1,ptr,1,id[f1],id[u],d);
        u=fa[f1],f1=top[u];
    }
    if(dep[u]>dep[v])
        swap(u,v);
    update(1,ptr,1,id[u],id[v],d);
}
int main()
{
    int n,m,p,i,u,v,w,rt;
    char cmd[100];

    while(~scanf("%d%d%d",&n,&m,&p))
    {
        for(i=1;i<=n;i++)
            scanf("%d",&arr[i]);
        cnt=0;
        memset(head,0,sizeof head);
        for(i=1;i<n;i++)
        {
            scanf("%d%d",&u,&v);
            adde(u,v);
            adde(v,u);
        }
        rt=(n+1)/2;
        init(rt);
        build(1,ptr,1);
        while(p--)
        {
            scanf("%s%d",cmd,&u);
            if(cmd[0]=='Q')
                printf("%d\n",qu(1,ptr,1,id[u]));
            else
            {
                scanf("%d%d",&v,&w);
                if(cmd[0]=='D')
                    w=-w;
                uppath(u,v,w);
            }
        }
    }
    return 0;
}

hdu 3966 Aragorn's Story(树链剖分对点编号)

时间: 2024-12-10 03:31:59

hdu 3966 Aragorn's Story(树链剖分对点编号)的相关文章

Hdu 3966 Aragorn&#39;s Story (树链剖分 + 线段树区间更新)

题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2:( D, i, j, x ) 从i到j的路径上经过的节点全部都减去x: 3:(Q, x) 查询节点x的权值为多少? 解题思路: 可以用树链剖分对节点进行hash,然后用线段树维护(修改,查询),数据范围比较大,要对线段树进行区间更新 1 #include <cstdio> 2 #include

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

Link: http://acm.hdu.edu.cn/showproblem.php?pid=3966 这题注意要手动扩栈. 这题我交g++无限RE,即使手动扩栈了,但交C++就过了. 1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include &

HDU 3966 Aragorn&#39;s Story 树链剖分+BIT区间修改/单点询问

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

hdu 3966 Aragorn&#39;s Story(树链剖分+树状数组)

题目链接:hdu 3966 Aragorn's Story 题目大意:给定一个棵树,然后三种操作 Q x:查询节点x的值 I x y w:节点x到y这条路径上所有节点的值增加w D x y w:节点x到y这条路径上所有节点的值减少w 解题思路:树链剖分,用树状数组维护每个节点的值. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cstring>

hdu 3966 Aragorn&#39;s Story 树链剖分 按点

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3494    Accepted Submission(s): 973 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lord

hdu 3966 Aragorn&#39;s Story : 树链剖分 O(nlogn)建树 O((logn)&#178;)修改与查询

1 /** 2 problem: http://acm.hdu.edu.cn/showproblem.php?pid=3966 3 裸板 4 **/ 5 #include<stdio.h> 6 #include<stdlib.h> 7 #include<string.h> 8 #include<vector> 9 using namespace std; 10 11 const int MAXN = 500005; 12 13 template <ty

HDU 3966 Aragorn&#39;s Story (树链点权剖分,成段修改单点查询)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 树链剖分的模版,成段更新单点查询.熟悉线段树的成段更新的话就小case啦. 1 //树链剖分 边权修改 单点查询 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 #include <cstdio> 6 using namespace std; 7 const int M

Hdu 3699 Aragorn&#39;s Story (树链剖分)

题目大意: 对一颗树上进行路径加减,然后询问单点的值. 思路分析: 简单的树链剖分模板题. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #pragma comment(linker,"/STACk:10240000,10240000") #define maxn 50005 #define lson num<<1,s

HDU 2966 Aragorn&#39;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 kingdom and M edges c