zoukankan      html  css  js  c++  java
  • HDOJ 3966 Aragorn's Story


    树链拆分+树阵

    (进入坑....)

    Aragorn's Story

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


    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.
     

    Source
     


    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn=100500;
    
    struct Edge
    {
        int to,next;
    }edge[maxn];
    
    int Adj[maxn],Size;
    
    void init_edge()
    {
        memset(Adj,-1,sizeof(Adj)); Size=0;
    }
    
    void add_edge(int u,int v)
    {
        edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++;
    }
    
    int fa[maxn],deep[maxn],num[maxn],son[maxn];
    int top[maxn],p[maxn],rp[maxn],pos;
    
    void init()
    {
        init_edge();
        memset(son,-1,sizeof(son)); pos=1;
    }
    
    void dfs1(int u,int pre,int d)
    {
        num[u]=1; fa[u]=pre; deep[u]=d;
        for(int i=Adj[u];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(v==pre) continue;
            dfs1(v,u,d+1);
            num[u]+=num[v];
            if(son[u]==-1||num[son[u]]<num[v])
            {
                son[u]=v;
            }
        }
    }
    
    void getPOS(int u,int to)
    {
        top[u]=to;
        p[u]=pos++;
        rp[p[u]]=u;
        if(son[u]!=-1) getPOS(son[u],to);
        for(int i=Adj[u];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(v!=fa[u]&&v!=son[u])
                getPOS(v,v);
        }
    }
    
    int tree[maxn];
    int n,m,q;
    
    inline int lowbit(int x)
    {
        return x&(-x);
    }
    
    void ADD(int p,int v)
    {
        for(int i=p;i<=n;i+=lowbit(i))
            tree[i]+=v;
    }
    
    int SUM(int p)
    {
        int ret=0;
        for(int i=p;i;i-=lowbit(i))
            ret+=tree[i];
        return ret;
    }
        
    
    void Change(int u,int v,int K)
    {
        int f1=top[u],f2=top[v];
        while(f1!=f2)
        {
            if(deep[f1]<deep[f2])
            {
                swap(f1,f2);
                swap(u,v);
            }
            ADD(p[f1],K);
            ADD(p[u]+1,-K);
            u=fa[f1];
            f1=top[u];
        }
        if(deep[u]<deep[v]) swap(u,v);
        ADD(p[v],K);
        ADD(p[u]+1,-K);
    }
    
    int a[maxn];
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(scanf("%d%d%d",&n,&m,&q)!=EOF)
        {
            init();
            for(int i=1;i<=n;i++)
                scanf("%d",a+i);
            while(m--)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                add_edge(a,b);
                add_edge(b,a);
            }
            dfs1(1,-1,0);
            getPOS(1,1);
            memset(tree,0,sizeof(tree));
            for(int i=1;i<=n;i++)
            {
                ADD(p[i],a[i]);
                ADD(p[i]+1,-a[i]);
            }
            char ord;
            int a,b,c;
            while(q--)
            {
                scanf("%*c %c",&ord);
                if(ord=='Q')
                {
                    scanf("%d",&a);
                    printf("%d
    ",SUM(p[a]));
                }
                else
                {
                    scanf("%d%d%d",&a,&b,&c);
                    if(ord=='D') c=-c;
                    Change(a,b,c);
                }
            }
        }
        return 0;
    }
    




    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    说说 C# 9 新特性的实际运用
    如何在Linux上使用VIM进行.Net Core开发
    写给程序员的机器学习入门 ---- 系列文章
    Java多线程Thread/Runnable/Callable之间的区别
    多种方式C#实现生成(条码/二维码)
    二维码(QR code)基本知识
    IntelliJ IDEA基本配置
    nps是一款轻量级、高性能、功能强大的内网穿透代理服务器。目前支持tcp、udp流量转发,可支持任何tcp、udp上层协议,还支持内网http代理、内网socks5代理、p2p等
    HTTP隧道ABPTTS——加密型的http隧道,todo自己搭建一个玩玩
    Neo-reGeorg ——目前比较好用的http隧道工具,支持本地建立 socks5 代理
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4687005.html
Copyright © 2011-2022 走看看