struct seqTree{
#define lc (o<<1)
#define rc ((o<<1)|1)
#define MID ((L+R)>>1)
#define ll long long
int n;
vector addv;
vector sumv;
vector minv;
vector maxv;
vector A;
void treeClear(){
addv.clear();
sumv.clear();
minv.clear();
maxv.clear();
A.clear();
}
void treeResize(int n){
addv.resize(n<<2);
sumv.resize(n<<2);
minv.resize(n<<2);
maxv.resize(n<<2);
}
seqTree():n(0){
treeClear();
}
seqTree(int n):n(n),A(n+2),addv(n<<2),sumv(n<<2),minv(n<<2),maxv(n<<2){}
void maintain(int o,int L,int R)
{
if(L==R){
sumv[o]=minv[o]=maxv[o]=A[L];
}
if(R>L){
sumv[o]=sumv[lc]+sumv[rc];
minv[o]=min(minv[lc],minv[rc]);
maxv[o]=max(maxv[lc],maxv[rc]);
}
}
void dfsBuild(int o,int L,int R)
{
if(L==R){
sumv[o]=A[L];
minv[o]=A[L];
maxv[o]=A[L];
}
if(Lif(MID>=L) dfsBuild(lc,L,MID);
if(MID1,R);
maintain(o,L,R);
}
}
void build(vector X,unsigned int L,unsigned int R)
{
if(X.empty()) return;
R=min(R,(unsigned int)X.size());
L=max(L,(unsigned int)0);
treeClear();
A.push_back(0);
for(unsigned int i=L;i1;
treeResize(n);
dfsBuild(1,1,A.size()-1);
}
seqTree(vector A)
{
build(A,0,A.size());
}
void updata(int o,int L,int R,int y1,int y2,ll v){
if(y1<=L && y2>=R){
addv[o]+=v;
minv[o]+=v;
maxv[o]+=v;
sumv[o]+=v*(R-L+1);
}
else{
pushDown(o,L,R);
if(y1<=MID) updata(lc,L,MID,y1,y2,v);
if(y2>MID) updata(rc,MID+1,R,y1,y2,v);
maintain(o,L,R);
}
}
void pushDown(int o,int L,int R)
{
if(addv[o]!=0){
addv[lc]+=addv[o];
addv[rc]+=addv[o];
sumv[lc]+=addv[o]*(MID-L+1);
sumv[rc]+=addv[o]*(R-MID);
minv[lc]+=addv[o];
minv[rc]+=addv[o];
maxv[lc]+=addv[o];
maxv[rc]+=addv[o];
addv[o]=0;
}
}
ll getSum(int o,int L,int R,int y1,int y2)
{
if(y1<=L && y2>=R){
return sumv[o];
}
pushDown(o,L,R);
ll ret=0;
if(y1<=MID) ret+=getSum(lc,L,MID,y1,y2);
if(y2> MID) ret+=getSum(rc,MID+1,R,y1,y2);
return ret;
}
ll getMin(int o,int L,int R,int y1,int y2)
{
if(y1<=L && y2>=R){
return minv[o];
}
pushDown(o,L,R);
ll ret=2e9;
if(y1<=MID) ret=min(ret,getMin(lc,L,MID,y1,y2));
if(y2> MID) ret=min(ret,getMin(rc,MID+1,R,y1,y2));
return ret;
}
ll getMax(int o,int L,int R,int y1,int y2)
{
if(y1<=L && y2>=R){
return maxv[o];
}
pushDown(o,L,R);
ll ret=-2e9;
if(y1<=MID) ret=max(ret,getMax(lc,L,MID,y1,y2));
if(y2> MID) ret=max(ret,getMax(rc,MID+1,R,y1,y2));
return ret;
}
};