树状数组变形:带区间修改的树状数组

原理很简单,利用差分知识做的,只能单点查询,在性能上优于线段树,但没有区间查询功能。

 1 #include<bits/stdc++.h>
 2 #define f(i,a,b) for(int i=a;i<=b;i++)
 3 using namespace std;
 4 
 5 const int N=5e5+5;
 6 int n,q,opt,le,ri,pos,val;
 7 int a[N];
 8 int tree[N];
 9 #define lowbit(x) (x&(-x))
10 void update(int pos,int val){
11     if(pos<=0) return;
12     while(pos<=n){
13         tree[pos]+=val;
14         pos+=lowbit(pos);
15     }
16 }
17 int sum_1_to_pos(int pos){
18     int ans=0;
19     while(pos>0){
20         ans+=tree[pos];
21         pos-=lowbit(pos);
22     }
23     return ans;
24 }
25 #define query(x,y) (sum_1_to_pos(y)-sum_1_to_pos(x-1))
26 
27 int main(){
28     scanf("%d%d",&n,&q);
29     scanf("%d",&a[1]);
30     f(i,2,n) scanf("%d",&a[i]),update(i-1,a[i]-a[i-1]);
31     f(i,1,q){
32         scanf("%d",&opt);
33         if(opt==1){
34             scanf("%d%d%d",&le,&ri,&val);
35             update(le-1,val);
36             update(ri,-val);
37             if(le==1) a[1]+=val;
38             //cout<<"array is:\n";
39             //f(i,1,n) cout<<i<<" : "<<a[1]+query(1,i-1)<<endl;
40         }
41         else{
42             scanf("%d",&pos);
43             printf("%d\n",a[1]+query(1,pos-1));
44         }
45     }
46 } 

 

原文链接: https://www.cnblogs.com/St-Lovaer/p/12263997.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    树状数组变形:带区间修改的树状数组

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/327804

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午4:25
下一篇 2023年3月1日 下午4:26

相关推荐