zoukankan      html  css  js  c++  java
  • 敌兵布阵(树状数组)

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

    树状数组模板题

     1 #include <stdio.h>
     2 #include <iostream>
     3 #include <string>
     4 #include <string.h>
     5 using namespace std;
     6 const int N=50005;
     7 int a[N],c[N];
     8 int n;
     9 int lowbit(int x)
    10 {
    11     return x&(-x);
    12 }
    13 int sum(int x)
    14 {
    15     int ans = 0;
    16     while(x > 0)
    17     {
    18         ans+=c[x];
    19         x-=lowbit(x);
    20     }
    21     return ans;
    22 }
    23 void add(int x,int d)
    24 {
    25     while(x <= n)
    26     {
    27         c[x]+=d;
    28         x+=lowbit(x);
    29     }
    30 }
    31 int main()
    32 {
    33     int t,L,R;
    34     int x,d,cnt = 0;
    35     char str[20];
    36     scanf("%d",&t);
    37     while(t--)
    38     {
    39         cnt++;
    40         scanf("%d",&n);
    41         memset(a,0,sizeof(a));
    42         memset(c,0,sizeof(c));
    43         for (int i = 1; i <= n; i++)
    44         {
    45             scanf("%d",&a[i]);
    46             add(i,a[i]);
    47         }
    48         printf("Case %d:
    ",cnt);
    49         while(scanf("%s",str))
    50         {
    51             if(str[0]=='E')
    52                 break;
    53             else if(str[0]=='Q')
    54             {
    55                 scanf("%d %d",&L,&R);
    56                 printf("%d
    ",sum(R)-sum(L-1));
    57             }
    58             else if(str[0]=='A')
    59             {
    60                 scanf("%d %d",&x,&d);
    61                 add(x,d);
    62             }
    63             else
    64             {
    65                 scanf("%d %d",&x,&d);
    66                 add(x,-d);
    67             }
    68         }
    69     }
    70     return 0;
    71 }
    View Code
  • 相关阅读:
    js 词法作用域揭秘
    Python 操作 Redis 发布订阅
    Logistic Regression
    快速逆平方根
    牛顿法
    Ubuntu安装BCC
    树莓派4b安装Ubuntu20.04
    丢弃法
    SpringBoot整合Redis
    Linear Regression
  • 原文地址:https://www.cnblogs.com/lahblogs/p/3523550.html
Copyright © 2011-2022 走看看