zoukankan      html  css  js  c++  java
  • 树状数组模板1——单点修改区间查询

    树状数组的模板,修改单点的值,查询某个区间

     

     1 #include<cstdio>
     2 #include<cctype> 
     3 using namespace std;
     4 
     5 int c[500010],n,m;
     6 
     7 int read()
     8 {
     9     int x=0,f=1;
    10     char c=getchar();
    11     while (!isdigit(c))
    12         f=c=='-'?-1:1,c=getchar();
    13     while (isdigit(c))
    14         x=(x<<1)+(x<<3)+(c^48),c=getchar();
    15     return x*f;
    16 }
    17 
    18 int lowbit(int x)
    19 {
    20     return x&-x;
    21 }
    22 
    23 int query(int x)
    24 {
    25     int ans=0;
    26     for (;x;x-=lowbit(x))
    27         ans+=c[x];
    28     return ans;
    29 }
    30 
    31 int update(int x,int val)
    32 {
    33     for (;x<=n;x+=lowbit(x))
    34         c[x]+=val;
    35 }
    36 
    37 int main()
    38 {
    39     int i,j,t;
    40     n=read(),m=read();
    41     for (i=1;i<=n;i++)
    42     {
    43         j=read();
    44         update(i,j);
    45     }
    46     while (m--)
    47     {
    48         t=read();
    49         i=read();
    50         j=read();
    51         if (t==1)
    52             update(i,j);
    53         else
    54             printf("%d
    ",query(j)-query(i-1));
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    Codeforces Round #609 (Div. 2)
    Educational Codeforces Round 78 (Rated for Div. 2)
    Codeforces
    crontab
    C6 C7的开机启动流程
    平均负载压力测试
    ps 和 top
    if判断
    使用3种协议搭建本地yum仓库
    linux rpm包
  • 原文地址:https://www.cnblogs.com/Ronald-MOK1426/p/8848825.html
Copyright © 2011-2022 走看看