Fenwick Tree

Input

The first line of input contains two integers NN, QQ, where 1≤N≤50000001≤N≤5000000 is the length of the array and 0≤Q≤50000000≤Q≤5000000 is the number of operations. Then follow QQ lines giving the operations. There are two types of operations:

  • + ii δδ” indicates that a[i]a[i] is incremented by δδ, where 0≤i<N0≤i<N and −109≤δ≤109−109≤δ≤109 (both are integers)

  • ? ii” is a query for the value of a[0]+a[1]+…+a[i−1]a[0]+a[1]+…+a[i−1], where 0≤i≤N0≤i≤N (for i=0i=0 this is interpreted as an empty sum)

Output

For each query in the input, output one line giving the answer to that query.

Sample Input 1 Sample Output 1
10 4
+ 7 23
? 8
+ 3 17
? 8
23
40
Sample Input 2 Sample Output 2
5 4
+ 0 -43
+ 4 1
? 0
? 5
0
-42

题意

N个数,Q个询问,+i表示a[i] +一个数,?i表示询问a[0] ~ a[i-1]的和

思路

放上树状数组模板

代码

#include<bits/stdc++.h>
using namespace std;
const int MAXN = ;
int N,Tree[MAXN];
#define LL long long
LL a[MAXN];
LL lowbit(LL p) { return (p&-p); }
LL sum(LL p) {
LL ret = ;
while (p> ) ret+=a[p], p-=lowbit(p);
return ret;
}
void add(LL p, LL v) { // 若要减去,则v传入一个负数
while (p <= N) a[p]+=v, p+=lowbit(p);
}
int main(){
while(cin>>N){
int t;
cin>>t;
memset(Tree,,sizeof(Tree));
for(int i=;i<=t;i++){
char ch;
int a,b;
cin>>ch;
if(ch=='+'){
cin>>a>>b;
add(a+,b);
}
else{
cin>>a;
cout<<sum(a)<<endl;
}
}
}
return ;
}

Kattis - Fenwick Tree(树状数组区间更新单点求值)的更多相关文章

  1. HDU - 3584 Cube (三维树状数组 + 区间改动 + 单点求值)

    HDU - 3584 Cube Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Subm ...

  2. 【poj2155】Matrix(二维树状数组区间更新+单点查询)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  3. NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)

    Problem 1050: Just Go Time Limits:  3000 MS   Memory Limits:  65536 KB 64-bit interger IO format:  % ...

  4. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  5. HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Sub ...

  6. hdu1556 树状数组区间更新单点查询板子

    就是裸的区间更新: 相对于直观的线段树的区间更新,树状数组的区间更新原理不太相同:由于数组中的一个结点控制的是一块区间,当遇到更新[l,r]时,先将所有能控制到 l 的结点给更新了,这样一来就是一下子 ...

  7. hdu3966 树链剖分点权模板+线段树区间更新/树状数组区间更新单点查询

    点权树的模板题,另外发现树状数组也是可以区间更新的.. 注意在对链进行操作时方向不要搞错 线段树版本 #include<bits/stdc++.h> using namespace std ...

  8. HDU 1556 Color the ball (树状数组 区间更新+单点查询)

    题目链接 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽&quo ...

  9. 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?

    开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...

随机推荐

  1. JQUERY 插件开发——MENU(导航菜单)

    JQUERY 插件开发——MENU(导航菜单) 故事背景:由于最近太忙了,已经很久没有写jquery插件开发系列了.但是凭着自己对这方面的爱好,我还是抽了一些时间来过一下插件瘾的.今天的主题是导航菜单 ...

  2. Android 5.0之应用中实现材料设计—Material Design

    上午的时候在刷Google+,看到了Abraham Williams转发了一篇强文,是Android Developers网站新发的一篇博客—Implementing Material Design ...

  3. 编写高质量equals方法

    什么是equals方法 指示其他某个对象是否与此对象相等,equals方法存在Object类中,我们编写的类继承Object,可以覆盖Object的equals方法来实现我们的逻辑,去判断两个对象是否 ...

  4. malloc内存分配与free内存释放的原理

    malloc内存分配与free内存释放的原理 前段时间一直想看malloc的原理,在搜了好几篇malloc源码后遂放弃,晦涩难懂. 后来室友买了本深入理解计算机系统的书,原来上面有讲malloc的原理 ...

  5. nginx 安装启动

    [root@localhost ~]# wget http://nginx.org/download/nginx-0.7.67.tar.gz --2010-09-24 14:48:12-- http: ...

  6. 用django搭建一个简易blog系统(翻译)(三)

    06. Connecting the Django admin to the blog app Django 本身就带有一个应用叫作Admin,而且它是一个很好的工具 在这一部分,我们将要激活admi ...

  7. JS的基本概念

    JS的基本概念 任何语言的核心都必然会描述这门语言最基本的工作原理.而描述的内容通常都要涉及这门语言的语法,操作符,数据类型,内置功能等用于构建复杂解决方案的概念.Ecma-262通过叫做EcmaSc ...

  8. NCache:最新发布的.NET平台分布式缓存系统

    NCache:最新发布的.NET平台分布式缓存系统在等待Microsoft完成Velocity这个.NET平台下的分布式内存缓存系统的过程中,现在让我们将目光暂时投向其他已经有所建树的软件开发商.Al ...

  9. 对用户控件(ascx)属性(property)赋值

    对用户控件(ascx)属性(property)赋值 Insus.NET写此博文,是对用户控件(ASCX)的属性赋值经验与技巧分享.是这样子的,在做新闻站点时,一般都会有分很多类别. 在站点首页会显示最 ...

  10. [置顶] github 出现 Permission denied (publickey)的解决

    今天写了一篇博客,想push到github上的时候出现了以下错误 Permission denied (publickey). fatal: The remote end hung up unexpe ...