zoukankan      html  css  js  c++  java
  • 线段树(单点更新) HDOJ 4288 Coder

    题目传送门

     1 #include <cstdio>
     2 #include <cstring>
     3 #define lson l, m, rt << 1
     4 #define rson m + 1, r, rt << 1 | 1
     5 
     6 const int MAX_N = 500000 + 10;
     7 int sum[MAX_N << 2];
     8 
     9 void pushup(int rt)
    10 {
    11     if (rt >> 1 % 5 == 3)
    12         sum[rt] += sum[rt >> 1];
    13     if (rt >>1 | 1 % 5 == 3)
    14         sum[rt] += sum[rt >> 1 | 1];
    15 }
    16 
    17 void build(int l, int r, int rt)
    18 {
    19     sum[rt] = r - l + 1;
    20     if (l == r)
    21     {
    22         return ;
    23     }
    24     int m = (l + r) >> 1;
    25     build (lson);
    26     build (rson);
    27     pushup (rt);
    28 }
    29 
    30 void update(int p, int l, int r, int rt)
    31 {
    32     if (l == r)
    33     {
    34         sum[rt] = p;
    35         return ;
    36     }
    37     int m = (l + r) >> 1;
    38     if (p <= m)    update (p, lson);
    39     else    update (p, rson);
    40 
    41     pushup (rt);
    42 }
    43 
    44 
    45 int main(void)        //HDOJ 4288 Coder
    46 {
    47     //freopen ("inG.txt", "r", stdin);
    48     int n;
    49     char op[10];
    50     int x;
    51 
    52     while (~scanf ("%d", &n))
    53     {
    54         build (1, n, 1);
    55         while (n--)
    56         {
    57             scanf ("%s", &op);
    58             if (strcmp (op, "add") == 0)
    59             {
    60                 scanf ("%d", &x);
    61                 update (x, 1, n, 1);
    62             }
    63             if (strcmp (op, "del") == 0)
    64             {
    65                 scanf ("%d", &x);
    66                 update (-x, 1, n, 1);
    67             }
    68             if (strcmp (op, "sum") == 0)
    69             {
    70                 printf ("%d
    ", sum[1]);
    71             }
    72         }
    73     }
    74 
    75     return 0;
    76 }
    编译人生,运行世界!
  • 相关阅读:
    climbing-stairs
    binary-tree-inorder-traversal
    search-insert-position
    balanced-binary-tree
    Java 接口工厂案例
    Java 接口案例
    Java 抽象类
    unique-paths
    maximum-subarray
    php修改文件夹下的所以图片png改为jpg,也可以作为修改为其他格式的方法
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4506580.html
Copyright © 2011-2022 走看看