NOI2012-超级钢琴的升级版。
用线段树维护最小值及其出现位置,接下来就跟超级钢琴一个做法了。
#include<bits/stdc++.h>
#define N 500010
#define inf 1000000007
#define fi first
#define sc second
#define lson (o<<1)
#define rson (o<<1|1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pir;
inline int read(){
int f=,x=;char ch;
do{ch=getchar();if(ch=='-')f=-;}while(ch<''||ch>'');
do{x=x*+ch-'';ch=getchar();}while(ch>=''&&ch<='');
return f*x;
}
int n,a[N],l,r,k,v;
pir tmp;
struct Segment_Tree{
pir minv[N<<];int tagv[N<<];
inline void puttag(int o,int v){
tagv[o]=max(tagv[o],v);
minv[o].fi=max(minv[o].fi,v);
}
inline void pushdown(int o){
if(tagv[o]==)return;
puttag(lson,tagv[o]);puttag(rson,tagv[o]);
tagv[o]=;
}
inline void pushup(int o){minv[o]=min(minv[lson],minv[rson]);}
void change(int o,int l,int r,int ql,int qr,int v){
if(ql<=l&&r<=qr){puttag(o,v);return;}
pushdown(o);int mid=(l+r)>>;
if(ql<=mid)change(lson,l,mid,ql,qr,v);
if(qr>mid)change(rson,mid+,r,ql,qr,v);
pushup(o);
}
pir query(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr)return minv[o];
pushdown(o);int mid=(l+r)>>;
pir ans=make_pair(inf,);
if(ql<=mid)ans=min(ans,query(lson,l,mid,ql,qr));
if(qr>mid)ans=min(ans,query(rson,mid+,r,ql,qr));
return ans;
}
void build(int o,int l,int r){
if(l==r){minv[o]=make_pair(a[l],l);return;}
int mid=(l+r)>>;
build(lson,l,mid);build(rson,mid+,r);
pushup(o);
}
}T;
struct seg{int l,r;pir mv;};
bool operator <(seg a,seg b){return a.mv.fi>b.mv.fi;}
priority_queue<seg> Q;
int ans[N],top,q;
inline void push(int l,int r){
if(l>r)return;pir tmp=T.query(,,n,l,r);
if(tmp.fi>=k)return;
Q.push((seg){l,r,tmp});
}
int main(){
n=read();
for(int i=;i<=n;i++)a[i]=read();
T.build(,,n);
q=read();
while(q--){
int opt=read();
if(opt==){
int l=read(),r=read(),v=read();
T.change(,,n,l,r,v);
}
else{
top=;while(!Q.empty())Q.pop();
l=read(),r=read(),k=read(),v=read();
push(l,r);
while(!Q.empty()){
seg tmp=Q.top();Q.pop();
ans[++top]=tmp.mv.fi;
push(tmp.l,tmp.mv.sc-);push(tmp.mv.sc+,tmp.r);
if(--v==)break;
}
if(v==){
sort(ans+,ans+top+);
for(int i=;i<=top;i++)printf("%d ",ans[i]);puts("");
}
else puts("-1");
}
}
}