[UOJ228 underlying data structures exercises]
Before opening the root == see that the first reaction zone square root of each number will only open up to 5 times
But this problem have an addition operation if the next time they finish the cycle of added back exploded
If the same value to reduce the number of some sections of the prescribing we can be transformed into such a complex range of subtraction can guarantee the ==
For example 8 9
the evolution is 2 3
to reduce the values are 6
So special sentenced tree line still have to maintain the minimum and maximum range of each prescription to reduce the values are equal
Gangster summary yyb
The purpose of such practice focuses on these special operations
At this point the main direction of thinking is not segment tree has been how to use the
But to clear the current operation has special properties
Accordingly, the segment again everything we need to maintain the trees
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define Max(x,y) ((x)>(y)?(x):(y))
#define Min(x,y) ((x)>(y)?(y):(x))
#define ls (o<<1)
#define rs (o<<1|1)
const int N=1e5+5,M=32000+5,inf=0x3f3f3f3f;
int n,m,a[N],b[N];
template <class t>void rd(t &x){
x=0;int w=0;char ch=0;
while(!isdigit(ch)) w|=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
x=w?-x:x;
}
struct node{ll mx,mn,add,sum;}t[N<<2];
void pup(int o){
t[o].sum=t[ls].sum+t[rs].sum;
t[o].mx=Max(t[ls].mx,t[rs].mx);
t[o].mn=Min(t[ls].mn,t[rs].mn);
}
void updnode(int o,int l,int r,ll k){
t[o].sum+=(ll)(r-l+1)*k;
t[o].add+=k,t[o].mn+=k,t[o].mx+=k;
}
void pudw(int o,int l,int r){
int mid=l+r>>1;
if(t[o].add!=0) updnode(ls,l,mid,t[o].add),updnode(rs,mid+1,r,t[o].add),t[o].add=0;
}
void upd1(int o,int l,int r,int x,int y,ll k){
if(l>y||r<x) return;
if(x<=l&&r<=y){updnode(o,l,r,k);return;}
pudw(o,l,r);
int mid=l+r>>1;
upd1(ls,l,mid,x,y,k),upd1(rs,mid+1,r,x,y,k);
pup(o);
}
void upd2(int o,int l,int r,int x,int y){
if(l>y||r<x) return;
if(x<=l&&r<=y){
ll A=t[o].mn-(ll)sqrt(t[o].mn),B=t[o].mx-(ll)sqrt(t[o].mx);
if(A==B){updnode(o,l,r,-A);return;}
}
pudw(o,l,r);
int mid=l+r>>1;
upd2(ls,l,mid,x,y),upd2(rs,mid+1,r,x,y);
pup(o);
}
ll query(int o,int l,int r,int x,int y){
if(l>y||r<x) return 0;
if(x<=l&&r<=y) return t[o].sum;
pudw(o,l,r);
int mid=l+r>>1;ll ans=0ll;
ans+=query(ls,l,mid,x,y)+query(rs,mid+1,r,x,y);
pup(o);
return ans;
}
void build(int o,int l,int r){
t[o].add=0;
if(l==r){t[o].mn=t[o].mx=t[o].sum=a[l];return;}
int mid=l+r>>1;
build(ls,l,mid),build(rs,mid+1,r);
pup(o);
}
int main(){
freopen("in.txt","r",stdin);
// freopen("numbers.out","w",stdout);
rd(n),rd(m);
for(int i=1;i<=n;++i) rd(a[i]);
build(1,1,n);
for(int i=1,opt,x,y,k;i<=m;++i){
rd(opt),rd(x),rd(y);
if(opt==1)
rd(k),upd1(1,1,n,x,y,k);
else if(opt==2) upd2(1,1,n,x,y);
else printf("%lld\n",query(1,1,n,x,y));
}
return 0;
}