Advertisement
NgJaBach

Untitled

Jun 15th, 2025
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. #define endl "\n"
  7.  
  8. const int N=200005,M=1e9+7;
  9. const ll INF=1e18+7;
  10.  
  11. ll seg[4*N],lazy[4*N];
  12. int a[N];
  13.  
  14. void Build(int x,int L,int R){
  15.     if(L==R){
  16.         seg[x]=a[L];
  17.         return;
  18.     }
  19.     int mid=(L+R)/2;
  20.     Build(2*x,L,mid);
  21.     Build(2*x+1,mid+1,R);
  22.     seg[x]=seg[2*x]+seg[2*x+1];
  23.     return;
  24. }
  25.  
  26. void Update_Lazy(int x,int L,int R){
  27.     if(lazy[x]==0) return;
  28.     seg[x]+=lazy[x]*(R-L+1);
  29.     if(L!=R){
  30.         lazy[2*x]+=lazy[x];
  31.         lazy[2*x+1]+=lazy[x];
  32.     }
  33.     lazy[x]=0;
  34.     return;
  35. }
  36.  
  37. void Update(int x,int L,int R,int lo,int hi,int val){
  38.     Update_Lazy(x,L,R);
  39.     if(L>hi or R<lo) return;
  40.     else if(L>=lo and R<=hi){
  41.         lazy[x]=val;
  42.         Update_Lazy(x,L,R);
  43.         return;
  44.     }
  45.     int mid=(L+R)/2;
  46.     Update(2*x,L,mid,lo,hi,val);
  47.     Update(2*x+1,mid+1,R,lo,hi,val);
  48.     seg[x]=seg[2*x]+seg[2*x+1];
  49.     return;
  50. }
  51.  
  52. ll Query(int x,int L,int R,int lo,int hi){
  53.     Update_Lazy(x,L,R);
  54.     if(L>hi or R<lo) return 0;
  55.     else if(L>=lo and R<=hi) return seg[x];
  56.     int mid=(L+R)/2;
  57.     ll sum=Query(2*x,L,mid,lo,hi)+Query(2*x+1,mid+1,R,lo,hi);
  58.     seg[x]=seg[2*x]+seg[2*x+1];
  59.     return sum;
  60. }
  61.  
  62. int main(){
  63.     ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr);
  64.     int n,q;
  65.     cin>>n>>q;
  66.     for(int i=1;i<=n;++i) cin>>a[i];
  67.     Build(1,1,n);
  68.     while(q--){
  69.         int t,x,y,z;
  70.         cin>>t>>x>>y;
  71.         if(t==1){
  72.             cin>>z;
  73.             Update(1,1,n,x,y,z);
  74.         }
  75.         else cout<<Query(1,1,n,x,y)<<endl;
  76.     }
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement