首页 技术 正文
技术 2022年11月8日
0 收藏 902 点赞 1,886 浏览 1108 个字

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,024
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,514
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,362
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,776
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,854