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,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...
随机推荐
- js 设为首页、加入收藏
// 加入收藏 <a onclick="AddFavorite(window.location,document.title)">加入收藏</a>func ...
- WisDom .net开发框架设计 2
随笔- 10 文章- 0 评论- 57 WisDom .net开发框架设计 (二) WisDom .net 权限设计 1.前言 几乎在所有的管理的系统,都离不开用户,角色,权 ...
- cefsharp实现javascript回调C#方法
在构建完WebView webView = new WebView(url)后,即可调用RegisterJsObject方法来注册一个js对象,从而前端的javascript就可以访问这个对象,调用定 ...
- 设计模式之观察者模式(Observable与Observer)
设计模式之观察者模式(Observable与Observer) 好久没有写博客啦,之前看完了<设计模式之禅>也没有总结一下,现在回忆一下设计模式之观察者模式. 1.什么是观察者模式 简单情 ...
- 用Python实现gmail邮箱服务,实现两个邮箱之间的绑定(中)
这篇博客,主要讲解用Python实现邮箱服务的几个需要学习的模块:E-mail Compotion and Decoding(邮件生成和解析).SMTP.POP.IMAP 如上篇博客所讲,我学习过程参 ...
- ASP.NET所谓前台调用后台、后台调用前台想到HTTP——实践篇
由ASP.NET所谓前台调用后台.后台调用前台想到HTTP——实践篇 在由ASP.NET所谓前台调用后台.后台调用前台想到HTTP——理论篇中描述了一下ASP.NET新手的三个问题及相关的HTTP协议 ...
- URL 调度器(URL dispatcher)
URL 调度器(URL dispatcher) 在刚开始接触 django 的时候, 我们尝试着从各种入门文档中创建一个自己的 django 项目, 需要在 mysite.urls.py 中配置 UR ...
- IOS学习之路七(通过xib自定义UITableViewCell)
一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是product name是:UITableViewCe ...
- Mobile页面项目总结
项目过去个把月了,一直没有写些东西总结下,这次借着年后的空隙,将当时记录下来的几个点回顾一下. 今明的布局:position技巧 每当看到类似横向并排布局的时候,总是想起定宽浮动,和下面讲到的列表并排 ...
- Android过滤Logcat输出
logcat和grep配合使用 1.打印特定tag的log,如打印Tag为Adm的Log adb logcat | grep Adm adb logcat | grep - ...