zoukankan      html  css  js  c++  java
  • HDU 3966 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. 2.Huge input, be careful.
    #include <bits/stdc++.h>
    using namespace std;
    
    const int maxn=5e4+5;
    
    int n;
    int tot, head[maxn],a[maxn];
    struct Edge{
        int to, next;
    }edge[maxn*2];
    int son[maxn], num[maxn], deep[maxn], fa[maxn], top[maxn];
    int p[maxn], c[maxn], pos;
    
    void addedge(int u, int v){
        edge[++tot]={v, head[u]};
        head[u]=tot;
    }
    
    void init(){
        tot=0, pos=1;
        memset(head, -1, sizeof(head));
        memset(son, -1, sizeof(son));
    }
    
    void dfs(int u, int pre, int d)
    {
        deep[u]=d, fa[u]=pre, num[u]=1;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].to;
            if(v==pre) continue;
            dfs(v, u, d+1);
            num[u]+=num[v];
            if(son[u]==-1 || num[v]>num[son[u]])
                son[u]=v;
        }
    }
    
    void getpos(int u, int sp)
    {
        top[u]=sp, p[u]=pos++;
        if(son[u]==-1) return;
        getpos(son[u], sp);
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].to;
            if(v!=son[u] && v!=fa[u])
                getpos(v, v);
        }
    }
    
    inline int lowbit(int x){
        return x&(-x);
    }
    
    int query(int i)
    {
        int res=0;
        for( ; i>0; i-=lowbit(i))
            res+=c[i];
        return res;
    }
    
    void update(int i, int val)
    {
        for( ;i<=n; i+=lowbit(i))
                c[i]+=val;
    }
    
    void change(int u, int v, int val)
    {
        int fu=top[u], fv=top[v];
        while(fu!=fv)
        {
            if(deep[fu]<deep[fv]){
                swap(fu, fv); swap(u, v);
            }
            update(p[fu], val);
            update(p[u]+1, -val);
            u=fa[fu], fu=top[u];
        }
        if(deep[u]>deep[v]) swap(u, v);
        update(p[u], val);
        update(p[v]+1, -val);
    }
    
    
    int main()
    {
        int m, q;
        while(scanf("%d%d%d", &n, &m, &q)==3)
        {
            init();
            for(int i=1; i<=n; i++)
                scanf("%d", &a[i]);
            for(int u,v,i=1; i<=m; i++)
            {
                scanf("%d%d", &u, &v);
                addedge(u, v); addedge(v, u);
            }
    
            dfs(1, 0, 0);
            getpos(1, 1);
            memset(c, 0, sizeof(c));
            for(int i=1; i<=n; i++)
            {
                update(p[i], a[i]);
                update(p[i]+1, -a[i]);
            }
            char opt[10];
            int l, r, val;
            while(q--)
            {
               scanf("%s", opt);
                if(opt[0]=='Q'){
                    scanf("%d", &l);
                    printf("%d
    ", query(p[l]));
                }
                else{
                    scanf("%d%d%d", &l, &r, &val);
                    if(opt[0]=='I')
                        change(l, r, val);
                    else
                        change(l, r, -val);
                }
            }
        }
        return 0;
    }
    View Code
     
     
     
  • 相关阅读:
    切图
    2014年5月份
    notepad++下载Subversion插件,显示intalltion of subversion failed
    At-rule | CSS @ 规则
    我的第一个phonegap开发WebApp的demo 怎么搭建安卓开发环境以及安装phonegap
    移动端页面:viewport与分辨率的坑
    部署一个完整的vue webpack项目
    部署一个最简单的webpack项目
    Android ORM框架 greenDAO 记录
    ubuntu下使用nvm安装nodejs
  • 原文地址:https://www.cnblogs.com/Yokel062/p/13125342.html
Copyright © 2011-2022 走看看