HDU 3966 Aragorn's Story 树链剖分+线段树(区间更新 单点查值)

Aragorn's Story


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



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.

树链剖分,新学,参考了不少博客,顺便把以前不太会的线段树区间更新也给补了补。
树链剖分原理请参考ACdreamers的博客: 树链剖分原理
本题也是在大佬的博客上找的,但是大佬贴的代码有点问题,我就用以前的线段树的模板把大佬的代码给改成了自己的风格,也顺便写了这篇博客。
我觉得树链剖分方面大佬讲的已经很详细了,我就重点说一下如何将树链剖分处理后的树在线段树上进行维护。
线段树可以维护一个连续区间,树链剖分中对树的每一个节点都按链添了一个新ID,所以我们按照这个ID将树的节点值放在线段树的最低层数组中,当我们想更新树上某两个节点路径之间的所有节点时,并不能直接使用线段树的update函数,而是需要根据节点 所在链确定我们需要更新哪一条链的哪一部分,因为同一条链的节点ID都是紧挨的,所有这个时候就可以使用update函数了。
当然,会线段树区间更新的都应该明白,区间更新时使用的是延迟更新标记法,从而使线段树的区间更新效率比较高。
当然对于本题来说,查询只是单点查询,所以PushUp函数并没有什么用处,但是代码里也给出了,只是为了更广泛的情况而已。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define maxn 50010
#define ll long long
using namespace std;
int n, m, q;
int num[maxn];
//邻接表
struct edge
{
        int v;
        int next;
} e[maxn * 2];
int cnt, tim;
int pre[maxn];
void add_edge(int u, int v)
{
        e[cnt].v = v;
        e[cnt].next = pre[u];
        pre[u] = cnt++;
        e[cnt].v = u;
        e[cnt].next = pre[v];
        pre[v] = cnt++;
}
//树链剖分
int dep[maxn], fa[maxn], siz[maxn], son[maxn];
// 深度数组   父节点  子节点数目  重儿子
int tid[maxn], ran[maxn], top[maxn];
// 节点的编号 编号的节点 所在链顶端节点
void dfs1(int u, int father, int d)
{
        dep[u] = d;
        fa[u] = father;
        siz[u] = 1;
        for (int i = pre[u]; ~i; i = e[i].next)
        {
                int v = e[i].v;
                if (v != father)
                {
                        dfs1(v, u, d + 1);
                        siz[u] += siz[v];
                        if (son[u] == -1 || siz[v] > siz[son[u]])
                        {
                                son[u] = v;
                        }
                }
        }
}
void dfs2(int u, int tp)
{
        top[u] = tp;
        tid[u] = ++tim;
        ran[tid[u]] = u;
        if (son[u] == -1)
        {
                return;
        }
        dfs2(son[u], tp);
        for (int i = pre[u]; ~i; i = e[i].next)
        {
                int v = e[i].v;
                if (v != son[u] && v != fa[u])
                {
                        dfs2(v, v);
                }
        }
}
//线段树
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int NV = 50010;
int lazy[NV << 2], sum[NV << 2];
void PushUp(int rt) //向上更新
{
        sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void PushDown(int rt, int m) // 向下更新  m:区间长度
{
        if (lazy[rt])
        {
                lazy[rt << 1] += lazy[rt];
                lazy[rt << 1 | 1] += lazy[rt];
                sum[rt << 1] += lazy[rt] * (m - (m >> 1));
                sum[rt << 1 | 1] += lazy[rt] * (m >> 1);
                lazy[rt] = 0;
        }
}
void build(int l, int r, int rt = 1) //建树
{
        lazy[rt] = 0;
        if (l == r)
        {
                sum[rt] = num[ran[l]]; //初始化
                return ;
        }
        int m = (l + r) >> 1;
        build(lson);
        build(rson);
        PushUp(rt);
}
void update(int L, int R, int c, int l, int r, int rt = 1) //区间更新
{
        if (L <= l && r <= R)
        {
                lazy[rt] += c;
                sum[rt] += c * (r - l + 1);
                return ;
        }
        PushDown(rt , r - l + 1);
        int m = (l + r) >> 1;
        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 = 1) //区间查询
{
        if (L <= l && r <= R)
        {
                return sum[rt];
        }
        PushDown(rt , r - l + 1);
        int m = (l + r) >> 1;
        int ret = 0;
        if (L <= m)
        {
                ret += query(L , R , lson);
        }
        if (m < R)
        {
                ret += query(L , R , rson);
        }
        return ret;
}
void change(int x, int y, int val) //精髓
{
        while (top[x] != top[y])
        {
                if (dep[top[x]] < dep[top[y]])
                {
                        swap(x, y);//x所在链在y链下方
                }
                update(tid[top[x]], tid[x], val, 1, n, 1); //更新x链
                x = fa[top[x]]; //一直更新到y所在链
        }
        if (dep[x] > dep[y])
        {
                swap(x, y);//x、y在同一条链且x在y上方
        }
        update(tid[x], tid[y], val, 1, n, 1); //更新y链
}
void init()
{
        cnt = 0;
        tim = 0;
        memset(pre, -1, sizeof(pre));
        memset(son, -1, sizeof(son));
}
int main()
{

        while (scanf("%d %d %d", &n, &m, &q) != EOF)
        {
                init();
                for (int i = 1; i <= n; i++)
                {
                        scanf("%d", &num[i]);
                }
                for (int i = 0; i < m; i++)
                {
                        int a, b;
                        scanf("%d %d", &a, &b);
                        add_edge(a, b);
                }
                dfs1(1, 0, 1);
                dfs2(1, 1);
                build(1, n, 1);
                while (q--)
                {
                        char a;
                        int b, c, d;
                        scanf(" %c", &a);
                        if (a == 'Q')
                        {
                                scanf(" %d", &b);
                                printf("%d\n", query(tid[b], tid[b], 1, n, 1));
                        }
                        else
                        {
                                scanf(" %d %d %d", &b, &c, &d);
                                if (a == 'D')
                                {
                                        d = -d;
                                }
                                change(b, c, d);
                        }
                }
        }
        return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值