zoukankan      html  css  js  c++  java
  • hdu1116敌兵布阵

    详细讲解线段树:http://blog.csdn.net/metalseed/article/details/8039326

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166

    单点更新,区间求和。

     1 #include<cstdio>
     2 #include<cstring>
     3 const int m=50005;
     4 #define lson  l,m,rt<<1
     5 #define rson m+1,r,rt<<1|1
     6 
     7 int sum[m<<2];
     8 
     9 inline void pushplus(int rt)
    10 {
    11     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    12 }
    13 
    14 void build (int l,int r,int rt)
    15 {
    16     if(l==r)
    17     {
    18         scanf("%d",&sum[rt]);
    19         return;
    20     }
    21     int m=(l+r)>>1;
    22     build(lson);
    23     build(rson);
    24     pushplus(rt);
    25 }
    26 
    27 void update(int p,int add,int l,int r,int rt)
    28 {
    29     if(l==r) {
    30         sum[rt]+=add;
    31         return;
    32     }
    33     int m=(l+r)>>1;
    34     if(p<=m) update(p,add,lson);
    35     else update(p,add,rson);
    36     pushplus(rt);
    37 }
    38 
    39 int query(int L,int R,int l,int r,int rt)
    40 {
    41     if(L<=l&&r<=R)
    42     {
    43         return sum[rt];
    44     }
    45     int ans=0;
    46     int m=(l+r)>>1;
    47     if(L<=m) ans+=query(L,R,lson);
    48     if(R>m) ans+=query(L,R,rson);
    49    return ans;
    50 }
    51 
    52 int main()
    53 {
    54     int t,n,a,b;
    55     scanf("%d",&t);
    56     for(int i=1;i<=t;i++)
    57     {
    58         printf("Case %d:
    ",i);
    59         scanf("%d",&n);
    60         build(1,n,1);
    61         char s[8];
    62         while(scanf("%s",s)&&s[0]!='E')
    63         {
    64             scanf("%d%d",&a,&b);
    65             if(s[0]=='Q') printf("%d
    ",query(a,b,1,n,1));
    66             else if(s[0]=='S') update(a,-b,1,n,1);
    67             else update(a,b,1,n,1);
    68         }
    69     }
    70     return 0;
    71 }
  • 相关阅读:
    lamp
    ssh 交互脚本
    mysql 备份检测主从脚本
    RANDOM 猜数字脚本
    ansible+playbook 搭建lnmp环境
    集群搭建
    grafana
    nginx lnmp搭建
    shell 基础(1): 变量
    seq 增量输出命令
  • 原文地址:https://www.cnblogs.com/yijiull/p/6619079.html
Copyright © 2011-2022 走看看