Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdio>
- using namespace std;
- #define MAX 100005
- int 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 || l>tr)
- 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;
- 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];
- }
- int sum(int node,int tl,int tr,int l,int r){
- if(tl>tr || tl>r || l>tr)
- 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