码迷,mamicode.com
首页 > 编程语言 > 详细

树状数组模板

时间:2019-09-05 18:55:32      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:space   for   font   lowbit   lse   bit   main   ons   nbsp   

树状数组模板

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=5e5+5;
 5 
 6 int n,m;
 7 int a[maxn];
 8 ll tree[maxn];
 9 
10 int lowbit(int x){
11     return x&(-x);
12 }
13 
14 void add(int idx,int v){
15     for(int i=idx;i<=n;i+=lowbit(i))
16         tree[i]+=v;
17 } 
18 
19 ll sum(int x){
20     ll ans=0;
21     while(x!=0){
22         ans+=tree[x];
23         x-=lowbit(x);
24     }
25     return ans;
26     
27 }
28 int main(){
29     cin>>n>>m;
30     for(int i=1;i<=n;i++) cin>>a[i],add(i,a[i]);
31     for(int i=1;i<=m;i++){
32         int op,x,y;cin>>op>>x>>y;
33         if(op==1) add(x,y);
34             else cout <<sum(y)-sum(x-1)<<"\n";
35     }
36     
37     return 0;
38 } 

 

树状数组模板

标签:space   for   font   lowbit   lse   bit   main   ons   nbsp   

原文地址:https://www.cnblogs.com/Msmw/p/11468599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!