zoukankan      html  css  js  c++  java
  • HDU-3966 Aragorn's Story(树链剖分+线段树)

    Aragorn's Story

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


    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
     
    Recommend
    We have carefully selected several similar problems for you:  3964 3965 3962 3963 3967 
    HDU是真鸡巴垃圾的一个OJ  老子的程序明明没有什么问题  结果判我RE
    今天学了树链剖分  简单来说就是把树上的节点标个号  然后拆成一条条链来操作,这一条条链的一个重要的性质是他们的编号都是连在一起的  这些操作比如区间修改查询等需要依托线段树,splay等等数据结构来实现
    好吧这是目前为止写的最长的程序。。。。。
      1 #pragma comment(linker, "/STACK:1024000000,1024000000")  //据说这玩意可以手动扩栈
      2 #include "bits/stdc++.h"
      3 #define mem(a,b) memset(a,b,sizeof(a))
      4 using namespace std;
      5 typedef long long LL;
      6 const int MAX=50005;
      7 int n,m,T;
      8 int tot,ttt;
      9 int head[MAX],adj[MAX<<2],nex[MAX<<2];
     10 int a[MAX],deep[MAX],fa[MAX];
     11 int siz[MAX],son[MAX],top[MAX],tid[MAX],rak[MAX];
     12 void addedge(int u,int v){
     13     tot++;
     14     adj[tot]=v;
     15     nex[tot]=head[u];
     16     head[u]=tot;
     17 }
     18 void init(){
     19     int i,j;
     20     mem(head,0),mem(son,0);
     21     tot=ttt=0;
     22 }
     23 void dfs1(int x,int ff,int de){ //标父亲,后代个数(包括自己),深度,重儿子 
     24     int i,j;
     25     deep[x]=de;
     26     siz[x]=1;fa[x]=ff;
     27     for (i=head[x];i;i=nex[i]){
     28         int v=adj[i];
     29         if (v==ff) continue;
     30         dfs1(v,x,de+1);
     31         siz[x]+=siz[v];
     32         if (!son[x] || siz[v]>siz[son[x]]){
     33             son[x]=v;
     34         }
     35     }
     36 }
     37 void dfs2(int x,int tp){//编号,拆树 
     38     int i,j;
     39     top[x]=tp;
     40     tid[x]=++ttt;
     41     rak[tid[ttt]]=x;
     42     if (!son[x]) return;
     43     dfs2(son[x],tp);
     44     for (i=head[x];i;i=nex[i]){
     45         if (adj[i]!=son[x] && adj[i]!=fa[x]){
     46             dfs2(adj[i],adj[i]);
     47         }
     48     }
     49 }
     50 //线段树 
     51 #define lson rt<<1,l,m
     52 #define rson rt<<1|1,m+1,r
     53 int sum[MAX<<2],la[MAX<<2];
     54 
     55 void PushUp(int rt){
     56     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
     57 }
     58 void PushDown(int rt,int l,int r){
     59     if (la[rt]){
     60         int m=(l+r)>>1;
     61         la[rt<<1]+=la[rt];la[rt<<1|1]+=la[rt];
     62         sum[rt<<1]+=(m-l+1)*la[rt];
     63         sum[rt<<1|1]+=(r-m)*la[rt];
     64         la[rt]=0;
     65     }
     66 }
     67 void build(int rt,int l,int r){
     68     la[rt]=0;
     69     if(l==r){
     70         sum[rt]=a[rak[l]];
     71         return;
     72     }
     73     int m=(l+r)>>1;
     74     build(lson);
     75     build(rson);
     76     PushUp(rt);
     77 }
     78 void update(int rt,int l,int r,int x,int y,int z){
     79     if (x<=l && r<=y){
     80         sum[rt]+=z*(r-l+1);
     81         la[rt]+=z;
     82         return;
     83     }
     84     int m=(l+r)>>1;
     85     PushDown(rt,l,r);
     86     if (x<=m) update(lson,x,y,z);
     87     if (y>m) update(rson,x,y,z);
     88     PushUp(rt);
     89 }
     90 int search(int rt,int l,int r,int x){
     91     if (l==r){
     92         return sum[rt];
     93     }
     94     int res=0;
     95     int m=(l+r)>>1;
     96     PushDown(rt,l,r);
     97     if (x<=m) res+=search(lson,x);
     98     if (x>m) res+=search(rson,x);
     99     PushUp(rt);
    100     return res;
    101 }
    102 void calc(int x,int y,int z){
    103     int i,j;
    104     while (top[x]!=top[y]){
    105         if (deep[top[x]]<deep[top[y]]) swap(x,y);
    106         update(1,1,n,tid[top[x]],tid[x],z);
    107         x=fa[top[x]];
    108     }
    109     if (deep[x]>deep[y]) swap(x,y);
    110     update(1,1,n,tid[x],tid[y],z);
    111 }
    112 int main(){
    113     freopen ("story.in","r",stdin);
    114     freopen ("story.out","w",stdout);
    115     int i,j;
    116     int u,v,x,y,z;
    117     char c;
    118     while (~scanf("%d%d%d",&n,&m,&T)){
    119         init();
    120         for (i=1;i<=n;i++){
    121             scanf("%d",a+i);
    122         }
    123         for (i=1;i<=m;i++){
    124             scanf("%d%d
    ",&u,&v);
    125             addedge(u,v);
    126             addedge(v,u);
    127         }
    128         dfs1(1,0,1);
    129         dfs2(1,1);
    130         build(1,1,n);
    131         while (T--){
    132             c=getchar();//cout<<c<<endl;
    133             if (c=='Q'){
    134                 scanf("%d
    ",&x);
    135                 printf("%d
    ",search(1,1,n,tid[x]));
    136             }
    137             else{
    138                 scanf("%d%d%d
    ",&x,&y,&z);
    139                 if (c=='D') z=-z;
    140                 calc(x,y,z);
    141             }
    142         }
    143     }
    144     return 0;
    145 }
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    修改微信电脑版的字体
    今天把自己的ocr镜像开源了
    写点恐怖小说为自己打call
    分享一波目前写的最强的autohotkey 插件
    分享一个我改进过的ocr算法
    从git源码安装zabbix-agent
    windows bat更改系统时间 & 同步internet时间
    Jmeter执行多条Mysql语句报错
    性能测试图片总结
    Jmeter beanshell 生成手机号加密签名
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/7349906.html
Copyright © 2011-2022 走看看