Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- #define endl "\n"
- const int N=200005,M=1e9+7;
- const ll INF=1e18+7;
- ll seg[4*N];
- int a[N];
- void Build(int x,int L,int R){
- if(L==R){
- seg[x]=a[L];
- return;
- }
- int mid=(L+R)/2;
- Build(2*x,L,mid);
- Build(2*x+1,mid+1,R);
- seg[x]=seg[2*x]+seg[2*x+1];
- return;
- }
- void Update(int x,int L,int R,int pos,int val){
- if(L==R){
- seg[x]=val;
- return;
- }
- int mid=(L+R)/2;
- if(pos>mid) Update(2*x+1,mid+1,R,pos,val);
- else Update(2*x,L,mid,pos,val);
- seg[x]=seg[2*x]+seg[2*x+1];
- return;
- }
- ll Query(int x,int L,int R,int lo,int hi){
- if(L>hi or R<lo) return 0;
- else if(L>=lo and R<=hi) return seg[x];
- int mid=(L+R)/2;
- return Query(2*x,L,mid,lo,hi)+Query(2*x+1,mid+1,R,lo,hi);
- }
- int main(){
- ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr);
- int n,q;
- cin>>n>>q;
- for(int i=1;i<=n;++i) cin>>a[i];
- Build(1,1,n);
- while(q--){
- int t,x,y;
- cin>>t>>x>>y;
- if(t==1) Update(1,1,n,x,y);
- else cout<<Query(1,1,n,x,y)<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement