AC日记——Aragorn's Story HDU 3966

Aragorn‘s Story

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

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

思路:

  裸树剖;

来,上代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

#define maxn 50001

using namespace std;

struct TreeNodeType {
    int l,r,dis,mid,flag;

    void clear()
    {
        l=0,r=0,dis=0,mid=0,flag=0;
    }
};
struct TreeNodeType tree[maxn<<2];

struct EdgeType {
    int to,next;
};
struct EdgeType edge[maxn<<1];

int if_z,n,m,q,cnt,tot,Enum,deep[maxn],size[maxn],belong[maxn];
int flag[maxn],head[maxn],dis[maxn],dis_[maxn],f[maxn];

char Cget;

inline void read_int(int &now)
{
    now=0,if_z=1,Cget=getchar();
    while(Cget<‘0‘||Cget>‘9‘)
    {
        if(Cget==‘-‘) if_z=-1;
        Cget=getchar();
    }
    while(Cget>=‘0‘&&Cget<=‘9‘)
    {
        now=now*10+Cget-‘0‘;
        Cget=getchar();
    }
    now*=if_z;
}

inline void edge_add(int from,int to)
{
    edge[++Enum].to=from,edge[Enum].next=head[to],head[to]=Enum;
    edge[++Enum].to=to,edge[Enum].next=head[from],head[from]=Enum;
}

void search(int now,int fa)
{
    int pos=tot++;
    deep[now]=deep[fa]+1,f[now]=fa;
    for(int i=head[now];i;i=edge[i].next)
    {
        if(edge[i].to==fa) continue;
        search(edge[i].to,now);
    }
    size[now]=tot-pos;
}

void search_(int now,int chain)
{
    int pos=0;
    flag[now]=++tot,dis_[flag[now]]=dis[now];
    belong[now]=chain;
    for(int i=head[now];i;i=edge[i].next)
    {
        if(flag[edge[i].to]) continue;
        if(size[edge[i].to]>size[pos]) pos=edge[i].to;
    }
    if(pos!=0) search_(pos,chain);
    else return ;
    for(int i=head[now];i;i=edge[i].next)
    {
        if(flag[edge[i].to]) continue;
        search_(edge[i].to,edge[i].to);
    }
}

inline void tree_up(int now)
{
    tree[now].dis=tree[now<<1].dis+tree[now<<1|1].dis;
}

inline void tree_down(int now)
{
    if(tree[now].l==tree[now].r) return ;
    tree[now<<1].dis+=(tree[now<<1].r-tree[now<<1].l+1)*tree[now].flag;
    tree[now<<1].flag+=tree[now].flag;
    tree[now<<1|1].dis+=(tree[now<<1|1].r-tree[now<<1|1].l+1)*tree[now].flag;
    tree[now<<1|1].flag+=tree[now].flag;
    tree[now].flag=0;
}

void tree_build(int now,int l,int r)
{
    tree[now].clear();
    tree[now].l=l,tree[now].r=r;
    if(l==r)
    {
        tree[now].dis=dis_[++tot];
        return ;
    }
    tree[now].mid=(l+r)>>1;
    tree_build(now<<1,l,tree[now].mid);
    tree_build(now<<1|1,tree[now].mid+1,r);
    tree_up(now);
}

void tree_change(int now,int l,int r,int x)
{
    if(tree[now].l==l&&tree[now].r==r)
    {
        tree[now].dis+=(r-l+1)*x;
        tree[now].flag+=x;
        return ;
    }
    if(tree[now].flag) tree_down(now);
    if(l>tree[now].mid) tree_change(now<<1|1,l,r,x);
    else if(r<=tree[now].mid) tree_change(now<<1,l,r,x);
    else
    {
        tree_change(now<<1,l,tree[now].mid,x);
        tree_change(now<<1|1,tree[now].mid+1,r,x);
    }
    tree_up(now);
}

int tree_query(int now,int to)
{
    if(tree[now].l==tree[now].r&&tree[now].l==to)
    {
        return tree[now].dis;
    }
    if(tree[now].flag) tree_down(now);
    tree_up(now);
    if(to>tree[now].mid) return tree_query(now<<1|1,to);
    else return tree_query(now<<1,to);
}

inline void solve_change(int x,int y,int z)
{
    while(belong[x]!=belong[y])
    {
        if(deep[belong[x]]<deep[belong[y]]) swap(x,y);
        tree_change(1,flag[belong[x]],flag[x],z);
        x=f[belong[x]];
    }
    if(deep[x]<deep[y]) swap(x,y);
    tree_change(1,flag[y],flag[x],z);
}

int main()
{
    while(scanf("%d%d%d",&n,&m,&q)==3)
    {
        memset(head,0,sizeof(head));
        memset(size,0,sizeof(size));
        memset(flag,0,sizeof(flag));
        tot=0,cnt=0,Enum=0;
        for(int i=1;i<=n;i++) read_int(dis[i]);
        int from,to,cur;
        for(int i=1;i<=m;i++)
        {
            read_int(from),read_int(to);
            edge_add(from,to);
        }
        search(1,0),tot=0,search_(1,1);
        tot=0,tree_build(1,1,n);
        char type;
        for(int i=1;i<=q;i++)
        {
            cin>>type;
            if(type==‘I‘)
            {
                read_int(from),read_int(to),read_int(cur);
                solve_change(from,to,cur);
            }
            if(type==‘D‘)
            {
                read_int(from),read_int(to),read_int(cur);
                solve_change(from,to,-cur);
            }
            if(type==‘Q‘)
            {
                read_int(from);
                printf("%d\n",tree_query(1,flag[from]));
            }
        }
    }
    return 0;
}

AC日记——Aragorn's Story HDU 3966

时间: 2024-09-30 19:05:20

AC日记——Aragorn's Story HDU 3966的相关文章

AC日记——Dylans loves tree hdu 5274

Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1611    Accepted Submission(s): 388 Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i] .Node

HDU 3966 Aragorn&#39;s Story

题目:Aragorn's Story 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给一棵树,每个结点都有初始的权值,有m个操作,分两种:一是从x 结点到y 结点路上所有的结点权值+z或-z,二是问x结点的权值. 思路: 树链剖分. 这是我学树剖的第一题,建议还没接触过的伙伴,第一次学习的时候不要一直纠结理论,直接找一道模板题,然后找一篇AC代码,直接理解,做完一题后,你就会发现理论其实也挺好理解的,树剖也挺好学的. 树剖中的新概念:重

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 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(树链剖分 模板题)

题目链接: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, th

树链剖分 [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): 3544    Accepted Submission(s): 995 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lord

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

HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 50005; inline int lowbit(int x) {return x&(-x);} int dep

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