Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- using namespace std;
- #define si(n) scanf("%d",&n)
- #define MAX 100005
- long long arr[MAX],tree[4*MAX],lazy[4*MAX];
- void update(int node,int tl,int tr,int l,int r,int val){
- //
- if(lazy[node]){
- tree[node]+=(tr-tl+1)*lazy[node];
- if (tl != tr){
- lazy[node*2]+=lazy[node];
- lazy[node*2+1]+=lazy[node];
- }
- lazy[node]=0;
- }
- if(tl>tr || tl>r || tr<l){
- //cout<<"here"<<endl;
- return ;
- }
- if(tl>=l && tr<=r){
- tree[node]+=(tr-tl+1)*val;
- if(tl != tr){
- lazy[node*2]+=val;
- lazy[node*2+1]+=val;
- }
- return ;
- }
- int mid=(tl+tr)/2;
- //cout<<mid<<endl;
- update(node*2,tl,mid,l,r,val);
- update(node*2+1,mid+1,tr,l,r,val);
- tree[node]=tree[node*2]+tree[node*2+1];
- }
- long long sum(int node,int tl,int tr,int l,int r){
- if(tl>tr || tl>r || tr<l)
- return 0;
- if(lazy[node]!=0){
- tree[node]+=(tr-tl+1)*lazy[node];
- if(tl != tr){
- lazy[node*2]+=lazy[node];
- lazy[node*2+1]+=lazy[node];
- }
- lazy[node]=0;
- }
- if(tl>=l && tr<=r)
- return tree[node];
- int mid=(tl+tr)/2;
- return sum(node*2,tl,mid,l,r)+
- sum(node*2+1,mid+1,tr,l,r);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement