待填坑
代码:
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #define ll long long using namespace std; int T,n,m; ll a[1000001]; class Segtree { public: ll sum[1000001*5],mx[1000001*5],smx[1000001*5],cnt[1000001*5],fl[1000001*5]; void pushup(int pos) { sum[pos]=sum[pos<<1]+sum[pos<<1|1]; if(mx[pos<<1]>mx[pos<<1|1]) { mx[pos]=mx[pos<<1]; cnt[pos]=cnt[pos<<1]; if(mx[pos<<1|1]>smx[pos<<1])smx[pos]=mx[pos<<1|1]; else smx[pos]=smx[pos<<1]; } if(mx[pos<<1]<mx[pos<<1|1]) { mx[pos]=mx[pos<<1|1]; cnt[pos]=cnt[pos<<1|1]; if(mx[pos<<1]>smx[pos<<1|1])smx[pos]=mx[pos<<1]; else smx[pos]=smx[pos<<1|1]; } if(mx[pos<<1]==mx[pos<<1|1]) { mx[pos]=mx[pos<<1]; cnt[pos]=cnt[pos<<1]+cnt[pos<<1|1]; if(smx[pos<<1]>smx[pos<<1|1])smx[pos]=smx[pos<<1]; else smx[pos]=smx[pos<<1|1]; } } void pushdown(int pos) { if(fl[pos]>=0) { if(mx[pos<<1]>fl[pos]) { sum[pos<<1]-=(mx[pos<<1]-fl[pos])*cnt[pos<<1]; fl[pos<<1]=mx[pos<<1]=fl[pos]; } if(mx[pos<<1|1]>fl[pos]) { sum[pos<<1|1]-=(mx[pos<<1|1]-fl[pos])*cnt[pos<<1|1]; fl[pos<<1|1]=mx[pos<<1|1]=fl[pos]; } fl[pos]=-1; } } void build(int l,int r,int pos) { if(l==r) { sum[pos]=mx[pos]=a[l]; cnt[pos]=1; fl[pos]=smx[pos]=-1; return; } int mid=l+r>>1; build(l,mid,pos<<1); build(mid+1,r,pos<<1|1); pushup(pos); fl[pos]=-1; } void change(int l,int r,int al,int ar,ll v,int pos) { int mid=l+r>>1; if(l==al && r==ar) { if(v>=mx[pos])return; if(smx[pos]<v && v<mx[pos]) { sum[pos]-=(mx[pos]-v)*cnt[pos]; fl[pos]=mx[pos]=v; return; } change(l,mid,al,mid,v,pos<<1); change(mid+1,r,mid+1,ar,v,pos<<1|1); pushup(pos); return; } pushdown(pos); if(ar<=mid)change(l,mid,al,ar,v,pos<<1); if(al>mid)change(mid+1,r,al,ar,v,pos<<1|1); if(al<=mid && ar>mid){change(l,mid,al,mid,v,pos<<1);change(mid+1,r,mid+1,ar,v,pos<<1|1);} pushup(pos); } ll askmax(int l,int r,int al,int ar,int pos) { if(l==al && r==ar)return mx[pos]; int mid=l+r>>1; pushdown(pos); if(ar<=mid)return askmax(l,mid,al,ar,pos<<1); if(al>mid)return askmax(mid+1,r,al,ar,pos<<1|1); if(al<=mid && ar>mid) { ll t1=askmax(l,mid,al,mid,pos<<1),t2=askmax(mid+1,r,mid+1,ar,pos<<1|1); return t1>t2?t1:t2; } } ll asksum(int l,int r,int al,int ar,int pos) { if(l==al && r==ar)return sum[pos]; int mid=l+r>>1; pushdown(pos); if(ar<=mid)return asksum(l,mid,al,ar,pos<<1); if(al>mid)return asksum(mid+1,r,al,ar,pos<<1|1); if(al<=mid && ar>mid)return asksum(l,mid,al,mid,pos<<1)+asksum(mid+1,r,mid+1,ar,pos<<1|1); } }segtree; int main() { scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%lld",&a[i]); segtree.build(1,n,1); int mod,x,y;ll t; while(m--) { scanf("%d",&mod); if(mod==0) { scanf("%d%d%lld",&x,&y,&t); segtree.change(1,n,x,y,t,1); } if(mod==1) { scanf("%d%d",&x,&y); printf("%lld\n",segtree.askmax(1,n,x,y,1)); } if(mod==2) { scanf("%d%d",&x,&y); printf("%lld\n",segtree.asksum(1,n,x,y,1)); } } } return 0; }
原文地址:https://www.cnblogs.com/worcher/p/11561073.html
时间: 2024-11-17 03:44:07