Submission #942479

#TimeUsernameProblemLanguageResultExecution timeMemory
942479MilosMilutinovicSushi (JOI16_sushi)C++14
100 / 100
5335 ms105440 KiB
#include<bits/stdc++.h>
 
#define pb push_back
#define fi first
#define se second
#define mp make_pair
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
 
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
 
ll readint(){
	ll x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

const int blk=700;
int n,q;
int x[400005],idx[400005],fl[400005],fr[400005];
priority_queue<int> all[400005],vals[400005];

int ins(int i,int v){
	if(all[i].top()<=v) return v;
	int ret=all[i].top();
	vals[i].push(-v);
	all[i].pop();
	all[i].push(v);
	return ret;
}

void push(int i){
	for(int j=fl[i];j<=fr[i];j++){
		if(!vals[i].empty()&&x[j]>-vals[i].top()){
			vals[i].push(-x[j]);
			x[j]=-vals[i].top();
			vals[i].pop();
		}
	}
	while(!vals[i].empty()) vals[i].pop();
}

void build(int i){
	while(!all[i].empty()) all[i].pop();
	for(int j=fl[i];j<=fr[i];j++){
		all[i].push(x[j]);
	}
}

int update(int l,int r,int v){
	if(idx[l]==idx[r]){
		push(idx[l]);
		for(int i=l;i<=r;i++){
			if(v<x[i]) swap(v,x[i]);
		}
		build(idx[l]);
		return v;
	}
	push(idx[l]);
	for(int i=l;i<=fr[idx[l]];i++){
		if(v<x[i]) swap(v,x[i]);
	}
	build(idx[l]);
	for(int i=idx[l]+1;i<idx[r];i++){
		v=ins(i,v);
	}
	push(idx[r]);
	for(int i=fl[idx[r]];i<=r;i++){
		if(v<x[i]) swap(v,x[i]);
	}
	build(idx[r]);
	return v;
}

int main(){
	n=readint(); q=readint();
	for(int i=1;i<=n;i++) x[i]=readint();
	for(int i=0;i<=n;i++) fl[i]=1e9,fr[i]=-1;
	for(int i=1;i<=n;i++){
		idx[i]=i/blk;
		fl[idx[i]]=min(fl[idx[i]],i);
		fr[idx[i]]=max(fr[idx[i]],i);
		all[idx[i]].push(x[i]);
	}
	while(q--){
		int l=readint(),r=readint(),v=readint();
		if(l<=r){
			fflush(stdout);
			v=update(l,r,v);
		}else{
			v=update(l,n,v);
			v=update(1,r,v);
		}
		printf("%d\n",v);
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...