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-制作选项卡

    最近在学习jquery,特地把今天写的一个选项卡源码贴出来.只是做只是梳理,大神们请不要吐槽,如果有更好的方法,欢迎指点.谢谢. css <style> #tab div{ width:2 ...

  2. android 使用 service 实现音乐

    今天的球员趁service.播放音乐service结束,进度条activity结束,因此,基础工作activity和service互动,本文将使用IBinder互动.主要activity能够调用ser ...

  3. ADFS 2.0 配置简介 PartⅡ – 配置 ADFS 信任关系

    ADFS 与应用程序间的各种验证是基于信任关系的,在 ADFS 服务器配置好要信赖的应用程序(以 URL 为标识)后,应用程序再通过指定认证服务器来将用户引导至 ADFS 登录页,登录完成后再将用户的 ...

  4. 增强型for语句与java,c#的不同之处,with语句的使用

    <script type="text/javascript" language="javascript"> //遍历数组 var arr = [&q ...

  5. 实现Client Credentials Grant

    [OAuth]基于DotNetOpenAuth实现Client Credentials Grant   Client Credentials Grant是指直接由Client向Authorizatio ...

  6. Orchard学习

      Data Source=.\SQLEXPRESS;Initial Catalog=Arale;Integrated Security=True;

  7. Bug Tracker

    Bug Tracker 使用笔记(有图有真相)   目的:管理Bug,完善业务流程. 前提条件:BugTracker是基于IIS和SQL Server和Asp.Net的.相当于一个Web端的管理系统. ...

  8. [转]Bypassing iOS security

    src: http://blog.thireus.com/tag/kernelcache Before going further it is important to enumerate some ...

  9. [转]ARM64 Function Calling Conventions

    from apple In general, iOS adheres to the generic ABI specified by ARM for the ARM64 architecture. H ...

  10. ASP.NET Web API 基础篇1

    ASP.NET Web API 直到我膝盖中了一箭[1]基础篇 无题 蓦然回首,那些年,我竟然一直很二. 小时候,读武侠小说的时候,看到那些猪脚,常常会产生一种代入感,幻想自己也会遭遇某种奇遇,遇到悬 ...