HDU 3966 Aragorn's Story 树链剖分

来源:互联网 发布:安卓6.0 java模拟器 编辑:程序博客网 时间:2024/06/11 03:57

Aragorn's Story

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


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 51 2 32 12 3I 1 3 5Q 2D 1 2 2Q 1 Q 3
 

Sample Output
748
Hint
1.The number of enemies may be negative.2.Huge input, be careful.
 

Source
题目大意:

给出 n 个节点,每个节点有一个值,然后给出 m 条边,最后给出 q 个操作,  I   a  b c    a 到  b的所有节点加上  c  。  D  为 减去  c   Q 为查询某个节点。

思路:

树链剖分之后,用线段树进行维护,每次更新,查询。这样。

AC代码:

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#include <iostream>using namespace std;#define Del(a,b) memset(a,b,sizeof(a))const int N = 50010;int n,m,q;int dep[N],siz[N],fa[N],id[N],son[N],val[N],top[N]; //top 最近的重链父节点int rankk[N];int num;int coss[N];vector<int> v[N];struct tree{    int x,y,val;    void read()    {        scanf("%d%d",&x,&y);    }};tree e[N];void dfs1(int u, int f, int d)///当前节点  father  dep   一开始  u = root{    dep[u] = d;    siz[u] = 1;    son[u] = 0;    fa[u] = f;    for (int i = 0; i < v[u].size(); i++)    {        int ff = v[u][i];        if (ff == f)            continue;        dfs1(ff, u, d + 1);        siz[u] += siz[ff];        if (siz[son[u]] < siz[ff])            son[u] = ff;    }}void dfs2(int u, int tp){    top[u] = tp;    id[u] = ++num;    //cout<<"u "<<u<<" "<<id[u]<<endl;    rankk[id[u]]=u;    if (son[u])        dfs2(son[u], tp);    for (int i = 0; i < v[u].size(); i++)    {        int ff = v[u][i];        if (ff == fa[u] || ff == son[u])            continue;        dfs2(ff, ff);    }}///线段树部分!#define lson l , m , rt << 1#define rson m + 1 , r , rt << 1 | 1struct Tree{    int l,r,sum,add;};Tree tree[4*N];void PushUp(int rt){tree[rt].sum =tree[rt<<1].sum + tree[rt<<1|1].sum;}void PushDown(int rt,int m){if (tree[rt].add)    {tree[rt<<1].add += tree[rt].add;tree[rt<<1|1].add += tree[rt].add;tree[rt<<1].sum += tree[rt].add * (m - (m >> 1));tree[rt<<1|1].sum += tree[rt].add * (m >> 1);tree[rt].add = 0;}}void build(int l,int r,int rt){tree[rt].add = 0;tree[rt].l=l;    tree[rt].r=r;if (l == r)    {        tree[rt].sum = coss[rankk[l]];        cout<<l<<" --  "<<tree[rt].sum<<endl;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){if (L <= l && r <= R)    {tree[rt].add += c;tree[rt].sum += 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);}void change(int u,int v,int vvv){    int tp1 = top[u], tp2 = top[v];    while (tp1 != tp2)    {        if (dep[tp1] < dep[tp2])        {            swap(tp1, tp2);            swap(u, v);        }        update(id[tp1], id[u],vvv,1,n,1);        u = fa[tp1];        tp1 = top[u];    }    if (dep[u] > dep[v])        swap(u, v);    update(id[u], id[v],vvv,1,n,1);}int query(int L,int R,int l,int r,int rt){if (L <= l && r <= R)    {return tree[rt].sum;}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 Clear(int n){    for(int i=1; i<=n; i++)        v[i].clear();}int main(){    while(~scanf("%d%d%d",&n,&m,&q))    {        for(int i=1;i<=n;i++)        {            scanf("%d",&coss[i]);        }        for(int i=1; i<=m; i++)        {            e[i].read();            v[e[i].x].push_back(e[i].y);            v[e[i].y].push_back(e[i].x);        }        num=0;        dfs1(1,0,1);        dfs2(1,1);        build(1,n,1);        char s[200];        while(q--)        {            scanf("%s",&s);            int x,y,t;            if(s[0]=='Q')            {                scanf("%d",&x);                printf("%d\n",query(id[x],id[x],1,n,1));            }            else if(s[0]=='I')            {                scanf("%d%d%d",&x,&y,&t);                change(x,y,t);            }            else            {                scanf("%d%d%d",&x,&y,&t);                change(x,y,-t);            }        }        Clear(n);    }    return 0;}


0 0
原创粉丝点击