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

    http://acm.hdu.edu.cn/showproblem.php?pid=1166

    线段树,单点更新

     1 #include <stdio.h>
     2 
     3 #define lson l, m, root<<1
     4 #define rson m+1, r, root<<1|1
     5 
     6 const int maxn = 55555;
     7 
     8 int sum[maxn<<2];
     9 
    10 void push_up(int root)
    11 {
    12     sum[root] = sum[root<<1] + sum[root<<1|1];
    13 }
    14 
    15 void build(int l, int r, int root)
    16 {
    17     int m = (l + r) >> 1;
    18     if(l == r)
    19     {
    20         scanf("%d", sum+root);
    21         return;
    22     }
    23     build(lson);
    24     build(rson);
    25     push_up(root);
    26 }
    27 
    28 void update(int x, int add, int l, int r, int root)
    29 {
    30     int m = (l + r) >> 1;
    31     if(l == r)
    32     {
    33         sum[root] += add;
    34         return;
    35     }
    36     if(x <= m)
    37     {
    38         update(x, add, lson);
    39     }
    40     if(m+1 <= x)
    41     {
    42         update(x, add, rson);
    43     }
    44     push_up(root);
    45 }
    46 
    47 int query(int L, int R, int l, int r, int root)
    48 {
    49     int m = (l + r) >> 1, result = 0;
    50     if(L <= l && r <= R)
    51     {
    52         return sum[root];
    53     }
    54     if(L <= m)
    55     {
    56         result += query(L, R, lson);
    57     }
    58     if(m+1 <= R)
    59     {
    60         result += query(L, R, rson);
    61     }
    62     return result;
    63 }
    64 
    65 int main()
    66 {
    67     int t, cases, n, a, b;
    68     char s[8];
    69     scanf("%d", &t);
    70     for(cases=1; cases<=t; cases++)
    71     {
    72         printf("Case %d:\n", cases);
    73         scanf("%d", &n);
    74         build(1, n, 1);
    75         while(scanf("%s", s), s[0]-'E')
    76         {
    77             scanf("%d%d", &a, &b);
    78             if(s[0] == 'Q')
    79             {
    80                 printf("%d\n", query(a, b, 1, n, 1));
    81                 continue;
    82             }
    83             if(s[0] == 'A')
    84             {
    85                 update(a, b, 1, n, 1);
    86             }
    87             else
    88             {
    89                 update(a, -b, 1, n, 1);
    90             }
    91         }
    92     }
    93     return 0;
    94 }
  • 相关阅读:
    day15内置函数
    day14生成器进阶
    day13迭代器、生成器
    day12装饰器进阶
    day11装饰器
    day10函数进阶
    day9函数
    day8文件操作
    正则表达式
    初识递归
  • 原文地址:https://www.cnblogs.com/yuan1991/p/hdu1166.html
Copyright © 2011-2022 走看看