Kattis - Fenwick Tree(树状数组区间更新单点求值)
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 |
23 |
Sample Input 2 | Sample Output 2 |
---|---|
5 4 |
0 |
题意
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(树状数组区间更新单点求值)的更多相关文章
- HDU - 3584 Cube (三维树状数组 + 区间改动 + 单点求值)
HDU - 3584 Cube Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Subm ...
- 【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)
Problem 1050: Just Go Time Limits: 3000 MS Memory Limits: 65536 KB 64-bit interger IO format: % ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Sub ...
- hdu1556 树状数组区间更新单点查询板子
就是裸的区间更新: 相对于直观的线段树的区间更新,树状数组的区间更新原理不太相同:由于数组中的一个结点控制的是一块区间,当遇到更新[l,r]时,先将所有能控制到 l 的结点给更新了,这样一来就是一下子 ...
- hdu3966 树链剖分点权模板+线段树区间更新/树状数组区间更新单点查询
点权树的模板题,另外发现树状数组也是可以区间更新的.. 注意在对链进行操作时方向不要搞错 线段树版本 #include<bits/stdc++.h> using namespace std ...
- HDU 1556 Color the ball (树状数组 区间更新+单点查询)
题目链接 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽&quo ...
- 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?
开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...
随机推荐
- [置顶] Android开发之Thread类分析
在我们Linux系统中创建线程函数为:pthread_create(),在Android中我们为线程封装了一个类Thread,实际调用的还是pthread_create() 当我们想创建线程的时候,只 ...
- serialVersionUID行动
ORIGINAL:未知 Java断类的serialVersionUID来验证版本号一致性的.在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地对应实体(类)的seria ...
- android判断网络的类型
转自:http://blog.csdn.net/xxxsz/article/details/8199031 判断网络类型是wifi,还是3G,还是2G网络 对不同的网络进行不同的处理,现将判断方法整理 ...
- [置顶] Ants(Northeastern Europe 2007)
Ants Time Limit: 5 ...
- ScriptCase升级到7.01
今天打开ScriptCase的开发环境,发现有新的升级,联网自动升级后,发现已经升级到7.01版本. 7.01版本对界面进行了优化,菜单和图标均以立体的形式进行展现. 不过粗粗看了一下,翻译还是有很多 ...
- 【IOS开发】创建XML文件
- (void)viewDidLoad { [super viewDidLoad]; NSString *path = [[NSBundle mainBundle] pathForResource:@ ...
- 10-18 noip提高组模拟赛(codecomb)T1倍增[未填]
T1只想到了找环,> <倍增的思想没有学过,所以看题解看得雨里雾里的(最近真的打算学一下! 题目出的挺好的,觉得noip极有可能出现T1T2T3,所以在此mark 刚开始T1以为是模拟,还 ...
- 模仿c的字符转整数函数 atoi
#include<stdio.h> , KInvalid}; int g_nStatus = KValid; long StrToIntCore(char *str,bool minus) ...
- 用py2exe打包pyqt4出现的问题(转)
使用pyqt完成窗体界面很方便,但是打包成exe之后会有问题,在网上找到解决办法如下: Another Solution to the same problem: from distutils.cor ...
- WinDBG调试.NET程序示例
WinDBG调试.NET程序示例 好不容易把环境打好了,一定要试试牛刀.我创建了一个极其简单的程序(如下).让我们期待会有好的结果吧,阿门! using System; using System.Co ...