bzoj 4695 最假女选手
bzoj 4695 最假女选手
Segment Tree Beats!!!
[吉司机的ppt][https://files.cnblogs.com/files/wawawa8/Segment_tree_Beats!.pdf]
就是按照他讲课里面写的做
主要是为了贴代码在这里
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<iostream>
#include<queue>
#include<string>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<long long,long long> pll;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i,j,k) for(register int i=(int)(j);i<=(int)(k);i++)
#define rrep(i,j,k) for(register int i=(int)(j);i>=(int)(k);i--)
ll read(){
ll x=0,f=1;char c=getchar();
while(c<'0' || c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0' && c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
#define lc (i<<1)
#define rc (i<<1|1)
const int inf=(1<<30);
const int maxn=500500;
const int mxsz=2002000;
int n,q,a[maxn];
int mx[mxsz],mn[mxsz],add[mxsz],mxnum[mxsz],sx[mxsz],mnnum[mxsz],sn[mxsz];
ll sum[mxsz];
inline void update(int i){
sum[i]=sum[lc]+sum[rc];
if(mx[lc]>mx[rc]) mx[i]=mx[lc],mxnum[i]=mxnum[lc],sx[i]=max(sx[lc],mx[rc]);
else if(mx[rc]>mx[lc]) mx[i]=mx[rc],mxnum[i]=mxnum[rc],sx[i]=max(sx[rc],mx[lc]);
else mx[i]=mx[lc],mxnum[i]=mxnum[lc]+mxnum[rc],sx[i]=max(sx[lc],sx[rc]);
if(mn[lc]<mn[rc]) mn[i]=mn[lc],mnnum[i]=mnnum[lc],sn[i]=min(sn[lc],mn[rc]);
else if(mn[rc]<mn[lc]) mn[i]=mn[rc],mnnum[i]=mnnum[rc],sn[i]=min(sn[rc],mn[lc]);
else mn[i]=mn[lc],mnnum[i]=mnnum[lc]+mnnum[rc],sn[i]=min(sn[lc],sn[rc]);
}
inline void pushdown(int i,int l,int r){
if(add[i]){
int md=(l+r)>>1,num=add[i];
mx[lc]+=num;sx[lc]+=num;mx[rc]+=num;sx[rc]+=num;
mn[lc]+=num;sn[lc]+=num;mn[rc]+=num;sn[rc]+=num;
sum[lc]+=(md-l+1)*1ll*num;sum[rc]+=(r-md)*1ll*num;
add[lc]+=num;add[rc]+=num;add[i]=0;
}
if(mx[lc]>mx[i]){
if(mn[lc]==mx[lc]) mn[lc]=mx[i];
if(sn[lc]==mx[lc]) sn[lc]=mx[i];
sum[lc]+=1ll*(mx[i]-mx[lc])*mxnum[lc];
mx[lc]=mx[i];
}
if(mx[rc]>mx[i]){
if(mn[rc]==mx[rc]) mn[rc]=mx[i];
if(sn[rc]==mx[rc]) sn[rc]=mx[i];
sum[rc]+=1ll*(mx[i]-mx[rc])*mxnum[rc];
mx[rc]=mx[i];
}
if(mn[lc]<mn[i]){
if(mx[lc]==mn[lc]) mx[lc]=mn[i];
if(sx[lc]==mn[lc]) sx[lc]=mn[i];
sum[lc]+=1ll*(mn[i]-mn[lc])*mnnum[lc];
mn[lc]=mn[i];
}
if(mn[rc]<mn[i]){
if(mx[rc]==mn[rc]) mx[rc]=mn[i];
if(sx[rc]==mn[rc]) sx[rc]=mn[i];
sum[rc]+=1ll*(mn[i]-mn[rc])*mnnum[rc];
mn[rc]=mn[i];
}
}
inline void build(int i,int l,int r){
if(l==r){
sum[i]=mx[i]=mn[i]=a[l];
mxnum[i]=mnnum[i]=1;
sx[i]=-inf;sn[i]=inf;
return;
}
int md=(l+r)>>1;
build(lc,l,md);build(rc,md+1,r);
update(i);
}
inline void ADD(int i,int l,int r,int L,int R,int val){
if(l>=L && r<=R){
mx[i]+=val;sx[i]+=val;mn[i]+=val;sn[i]+=val;
sum[i]+=1ll*(r-l+1)*val;add[i]+=val;
return;
}
pushdown(i,l,r);
int md=(l+r)>>1;
if(L<=md) ADD(lc,l,md,L,R,val);
if(R>md) ADD(rc,md+1,r,L,R,val);
update(i);
}
inline void MAX(int i,int l,int r,int L,int R,int val){
if(mn[i]>=val) return;
if(l>=L && r<=R && sn[i]>val){
if(mx[i]==mn[i]) mx[i]=val;
if(sx[i]==mn[i]) sx[i]=val;
sum[i]+=1ll*(val-mn[i])*mnnum[i];
mn[i]=val;
return;
}
pushdown(i,l,r);
int md=(l+r)>>1;
if(L<=md) MAX(lc,l,md,L,R,val);
if(R>md) MAX(rc,md+1,r,L,R,val);
update(i);
}
inline void MIN(int i,int l,int r,int L,int R,int val){
if(mx[i]<=val) return;
if(l>=L && r<=R && sx[i]<val){
if(mn[i]==mx[i]) mn[i]=val;
if(sn[i]==mx[i]) sn[i]=val;
sum[i]+=1ll*(val-mx[i])*mxnum[i];
mx[i]=val;
return;
}
pushdown(i,l,r);
int md=(l+r)>>1;
if(L<=md) MIN(lc,l,md,L,R,val);
if(R>md) MIN(rc,md+1,r,L,R,val);
update(i);
}
inline ll qsum(int i,int l,int r,int L,int R){
if(l>=L && r<=R) return sum[i];
pushdown(i,l,r);
int md=(l+r)>>1;
ll ret=0;
if(L<=md) ret+=qsum(lc,l,md,L,R);
if(R>md) ret+=qsum(rc,md+1,r,L,R);
return ret;
}
inline int qmax(int i,int l,int r,int L,int R){
if(l>=L && r<=R) return mx[i];
pushdown(i,l,r);
int md=(l+r)>>1,ret=-inf;
if(L<=md) ret=max(ret,qmax(lc,l,md,L,R));
if(R>md) ret=max(ret,qmax(rc,md+1,r,L,R));
return ret;
}
inline int qmin(int i,int l,int r,int L,int R){
if(l>=L && r<=R) return mn[i];
pushdown(i,l,r);
int md=(l+r)>>1,ret=inf;
if(L<=md) ret=min(ret,qmin(lc,l,md,L,R));
if(R>md) ret=min(ret,qmin(rc,md+1,r,L,R));
return ret;
}
int main(){
//freopen("in","r",stdin);
//freopen("out","w",stdout);
n=read();
rep(i,1,n) a[i]=read();
build(1,1,n);
q=read();
while(q--){
int op=read(),l=read(),r=read();
if(op==1){int val=read();ADD(1,1,n,l,r,val);}
else if(op==2){int val=read();MAX(1,1,n,l,r,val);}
else if(op==3){int val=read();MIN(1,1,n,l,r,val);}
else if(op==4) printf("%lld\n",qsum(1,1,n,l,r));
else if(op==5) printf("%d\n",qmax(1,1,n,l,r));
else printf("%d\n",qmin(1,1,n,l,r));
}
return 0;
}