Samkpoe

segtree permanent tag

Feb 26th, 2021
638
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<algorithm>
  3. #define int long long
  4. using namespace std;
  5. const int maxn=1e6+5;
  6. int a[maxn],seg[4*maxn],tag[4*maxn];
  7. void mod(int l,int r,int p,int ql,int qr,int v){
  8.     if(r<ql||qr<l)return;
  9.     if(ql<=l&&r<=qr){
  10.         seg[p]+=(r-l+1)*v;
  11.         tag[p]+=v;
  12.         return;
  13.     }
  14.     int mid=l+r>>1;
  15.     mod(l,mid,p<<1,ql,qr,v);
  16.     mod(mid+1,r,p<<1|1,ql,qr,v);
  17.     seg[p]=seg[p<<1]+seg[p<<1|1];
  18. }
  19. int query(int l,int r,int p,int ql,int qr,int tags){
  20.     if(r<ql||qr<l)return 0;
  21.     if(ql<=l&&r<=qr){
  22.         return tags*(r-l+1)+seg[p];
  23.     }
  24.     int mid=l+r>>1;
  25.     return query(l,mid,p<<1,ql,qr,tags+tag[p])+query(mid+1,r,p<<1|1,ql,qr,tags+tag[p]);
  26. }
  27. signed main(){
  28.     int n,q,l,r,k;
  29.     char cmd;
  30.     cin>>n>>q;
  31.     for(int i=1;i<=n;i++)cin>>a[i],mod(1,n,1,i,i,a[i]);
  32.     for(int i=0;i<q;i++){
  33.         cin>>cmd>>l>>r;
  34.         if(cmd=='q')cout<<query(1,n,1,l,r,0)<<"\n";
  35.         else{
  36.             cin>>k;
  37.             mod(1,n,1,l,r,k);
  38.         }
  39.     }
  40. }
  41.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×