Advertisement
NgJaBach

Untitled

Jun 15th, 2025
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 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];
  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(int x,int L,int R,int pos,int val){
  27.     if(L==R){
  28.         seg[x]=val;
  29.         return;
  30.     }
  31.     int mid=(L+R)/2;
  32.     if(pos>mid) Update(2*x+1,mid+1,R,pos,val);
  33.     else Update(2*x,L,mid,pos,val);
  34.     seg[x]=seg[2*x]+seg[2*x+1];
  35.     return;
  36. }
  37.  
  38. ll Query(int x,int L,int R,int lo,int hi){
  39.     if(L>hi or R<lo) return 0;
  40.     else if(L>=lo and R<=hi) return seg[x];
  41.     int mid=(L+R)/2;
  42.     return Query(2*x,L,mid,lo,hi)+Query(2*x+1,mid+1,R,lo,hi);
  43. }
  44.  
  45. int main(){
  46.     ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr);
  47.     int n,q;
  48.     cin>>n>>q;
  49.     for(int i=1;i<=n;++i) cin>>a[i];
  50.     Build(1,1,n);
  51.     while(q--){
  52.         int t,x,y;
  53.         cin>>t>>x>>y;
  54.         if(t==1) Update(1,1,n,x,y);
  55.         else cout<<Query(1,1,n,x,y)<<endl;
  56.     }
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement