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

扫描二维码关注公众号,回复: 2411194 查看本文章

2.Huge input, be careful.

思路

n个点,m条边,p个询问。
3个操作:

  • I x y k给x到y之间增加k个
  • D x y k给x到y之间减少k个
  • Q x查询x点的值

直接树剖维护即可

代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 2e5 + 10;
const int inf = 0x3f3f3f3f;
int first[N], tot, n, m, p;
int sum[N << 2], lazy[N << 2];
int w[N], wt[N], cnt, siz[N], dep[N], fa[N], son[N], top[N], id[N];
struct edge
{
    int v, next;
} e[N];
void add_edge(int u, int v)
{
    e[tot].v = v;
    e[tot].next = first[u];
    first[u] = tot++;
}
void init()
{
    mem(first, -1);
    tot = 0;
    cnt = 0;
}
void pushup(int rt)
{
    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void pushdown(int rt, int 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)
{
    lazy[rt] = 0;
    if (l == r)
    {
        sum[rt] = wt[l];
        return;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int L, int R, int k, int l, int r, int rt)
{
    if (L <= l && r <= R)
    {
        lazy[rt] += k;
        sum[rt] += k * (r - l + 1);
        return;
    }
    pushdown(rt, r - l + 1);
    int m = (l + r) >> 1;
    if (L <= m)
        update(L, R, k, lson);
    if (R > m)
        update(L, R, k, rson);
    pushup(rt);
}
int query(int p, int l, int r, int rt)
{
    if (l == r)
        return sum[rt];
    int m = (l + r) >> 1;
    pushdown(rt, r - l + 1);
    int ans = 0;
    if (p <= m)
        ans += query(p, lson);
    else
        ans += query(p, rson);
    pushup(rt);
    return ans;
}
void dfs1(int u, int f, int deep)
{
    siz[u] = 1;
    dep[u] = deep;
    fa[u] = f;
    son[u] = 0;
    int maxson = -1;
    for (int i = first[u]; ~i; i = e[i].next)
    {
        int v = e[i].v;
        if (v == f)
            continue;
        dfs1(v, u, deep + 1);
        siz[u] += siz[v];
        if (siz[v] > maxson)
        {
            son[u] = v;
            maxson = siz[v];
        }
    }
}
void dfs2(int u, int topf)
{
    top[u] = topf;
    id[u] = ++cnt;
    wt[cnt] = w[u];
    if (!son[u])
        return;
    dfs2(son[u], topf);
    for (int i = first[u]; ~i; i = e[i].next)
    {
        int v = e[i].v;
        if (v == fa[u] || v == son[u])
            continue;
        dfs2(v, v);
    }
}
void change(int x, int y, int k)
{
    while (top[x] != top[y])
    {
        if (dep[top[x]] < dep[top[y]])
            swap(x, y);
        update(id[top[x]], id[x], k, 1, n, 1);
        x = fa[top[x]];
    }
    if (dep[x] > dep[y])
        swap(x, y);
    update(id[x], id[y], k, 1, n, 1);
}
int main()
{
    //freopen("in.txt", "r", stdin);
    int u, v;
    while (~scanf("%d%d%d", &n, &m, &p))
    {
        init();
        for (int i = 1; i <= n; i++)
            scanf("%d", &w[i]);
        for (int i = 1; i <= m; i++)
        {
            scanf("%d%d", &u, &v);
            add_edge(u, v);
            add_edge(v, u);
        }
        dfs1(1, 0, 1);
        dfs2(1, 1);
        build(1, n, 1);
        char s[5];
        int x, y, k;
        while (p--)
        {
            scanf("%s", s);
            if (s[0] == 'I')
            {
                scanf("%d%d%d", &x, &y, &k);
                change(x, y, k);
            }
            else if (s[0] == 'D')
            {
                scanf("%d%d%d", &x, &y, &k);
                change(x, y, -k);
            }
            else if (s[0] == 'Q')
            {
                scanf("%d", &x);
                printf("%d\n", query(id[x], 1, n, 1));
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/riba2534/article/details/81075201