HDU 3966 Aragorn'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, 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.

题意:

给出每个点的初始值,和给出一些边,连成一颗树;

接着三种操作:

1、Q X:询问X这个点的值;

2、I  X Y Z:在点X到点Y的路径上所有的点,都增加Z;

3、D  X Y Z:在点X到点Y的路径上所有的点,都减少Z;

代码如下:

#include <cstdio>
#include <cstring>
#define MAXN 50017
#define maxn 100017
int N, M, Q;
int siz[MAXN];//siz[v]表示以v为根的子树的节点数
int dep[MAXN];//dep[v]表示v的深度(根深度为1)
int top[MAXN];//top[v]表示v所在的重链的顶端节点
int fa[MAXN];//fa[v]表示v的父亲
int son[MAXN];//son[v]表示与v在同一重链上的v的儿子节点
int w[MAXN];//w[v]表示v与其父亲节点的连边(姑且称为v的父边)在线段树中的位置
int cont;
int first[maxn], e, next[maxn], v[maxn];
int a[MAXN], add[MAXN*4], q[MAXN];
void swap_(int &x, int &y)
{
    int t;
    t = x, x = y, y = t;
}

void Add(int x, int y)
{
    v[e] = y;
    next[e] = first[x], first[x] = e++;
}
void pushdown(int cur)
{
    if(add[cur])
    {
        add[cur << 1] += add[cur];
        add[cur << 1 | 1] += add[cur];
        add[cur] = 0;
    }
}

void prepare()//剖分部分
{
    int x, rear = 0;
    q[rear++] = 1;
    fa[1] = 0;
    dep[1] = 1;//根深度为1
    memset(top,0,sizeof(top));
    for(int i = 0; i < rear; i++)
    {
        x = q[i];
        for(int j = first[x]; j != -1; j = next[j])
        {
            if(v[j] != fa[x])
            {
                fa[v[j]] = x, dep[v[j]] = dep[x] + 1;
                q[rear++] = v[j];
            }
        }
    }
    siz[0] = 0;
    for(int i = rear-1; i >= 0; i--)
    {
        x = q[i];
        siz[x] = 1, son[x] = 0;
        for(int j = first[x]; j != -1; j = next[j])
        {
            if(v[j] != fa[x])
            {
                siz[x] += siz[v[j]];
                if(siz[v[j]] > siz[son[x]])
                    son[x] = v[j];
            }
        }
    }
    cont = 0;
    for(int i = 0; i < rear; i++)
    {
        x = q[i];
        if(top[x] == 0)
        {
            for(int j = x; j != 0; j = son[j])
            {
                top[j] = x, w[j] = ++cont;
            }
        }
    }
}

void  Update(int cur, int x, int y, int s, int t, int v)//线段树部分
{
    int mid = (x+y) >> 1;
    int ls = cur << 1;
    int rs = cur << 1 | 1;
    if(x >= s && y <= t)
    {
        add[cur] += v;
        return ;
    }
    pushdown(cur);
    if(mid >= s)
        Update(ls, x, mid, s, t, v);
    if(mid < t)
        Update(rs, mid+1, y, s, t, v);
}

int query(int cur, int x, int y, int k)
{
    int mid = (x+y) >> 1;
    int ls = cur << 1;
    int rs = cur << 1 | 1;
    if(x == y)
        return add[cur];
    pushdown(cur);
    if( k <= mid)
        return query(ls, x, mid, k);
    else
        return query(rs, mid+1, y, k);
}

void Deal(int x, int y, int z)
{
    int fx = top[x], fy = top[y];
    while(fx != fy)
    {
        if(dep[fx] > dep[fy])
        {
            swap_(fx, fy);
            swap_(x, y);
        }
        Update(1, 1, N, w[fy], w[y], z);
        y = fa[fy], fy = top[y];
    }
    if(dep[x] > dep[y])
    {
        swap_(x, y);
    }
    Update(1, 1, N, w[x], w[y], z);
}

int main()
{
    int x, y, z;
    while(scanf("%d%d%d",&N,&M,&Q) != EOF)
    {
        for(int i = 1; i <= N; i++)
        {
            scanf("%d",&a[i]);
        }
        e = 0;
        memset(first,-1,sizeof(first));
        for(int i = 0; i < M; i++)
        {
            scanf("%d%d",&x,&y);
            Add(x, y);
            Add(y, x);
        }
        prepare();

        memset(add,0,sizeof(add));
        for(int i = 1; i <= N; i++)
        {
            Update(1, 1, N, w[i], w[i], a[i]);
        }
        char op[7];
        for(int i = 1; i <= Q; i++)
        {
            scanf("%s",op);
            if(op[0] == 'Q')
            {
                scanf("%d",&x);
                printf("%d\n",query(1, 1, N, w[x]));
            }
            else
            {
                scanf("%d%d%d",&x,&y, &z);
                if(op[0] == 'I')
                    Deal(x, y, z);
                else
                    Deal(x, y, -z);
            }
        }
    }
    return 0;
}

HDU 3966 Aragorn's Story(树链剖分 模板题)

时间: 2024-10-23 05:28:25

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(树链剖分对点编号)

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

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 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

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