「JOISC 2016 Day 3」回转寿司

https://loj.ac/problem/2736

题解

挺有意思的题。

考虑这种操作不好直接维护,还有时限比较长,所以考虑分块。

考虑一个操作对整个块的影响,无非就是可能把最大的拿走,再把新的元素插进去。

对于散块我们可以重构,那么对于整块我们也可以快速查询。

现在的问题就是对于一个整块,我们打上了一堆标记,现在如何快速下放标记。

考虑到我们只需要知道最终的结果是什么,我们可以开一个小根堆存所有的标记,然后从左到右模拟就行了。

代码

#include<bits/stdc++.h>
#define N 400005
#define M 1002
using namespace std;
typedef long long ll;
int n,q,n1,a[N],be[N];
priority_queue<int>blo[M];
priority_queue<int,vector<int>,greater<int> >c[M];
inline ll rd(){
	ll x=0;char c=getchar();bool f=0;
	while(!isdigit(c)){if(c=='-')f=1;c=getchar();}
	while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
	return f?-x:x;
}
inline void rebuild(int x){
	if(c[x].empty())return;
	int l=(x-1)*n1+1,r=min(n,x*n1);
	for(int i=l;i<=r;++i){
		if(a[i]>c[x].top()){
			int xx=a[i];
			a[i]=c[x].top();c[x].pop();
			c[x].push(xx);
		}
	}
	while(!c[x].empty())c[x].pop();
}
inline int upd(int x,int l,int r,int val){
	rebuild(x);
	for(int i=l;i<=r;++i)if(a[i]>val)swap(a[i],val);
	while(!blo[x].empty())blo[x].pop();
	for(int i=(x-1)*n1+1;i<=min(n,x*n1);++i)blo[x].push(a[i]);
	return val;
}
inline int solve(int l,int r,int val){
	int be1=be[l],be2=be[r];
	if(be1==be2)return upd(be1,l,r,val);
	else{
		val=upd(be1,l,be1*n1,val);
		for(int i=be1+1;i<be2;++i){
			if(blo[i].top()>val){
				int xx=blo[i].top();
		        blo[i].pop();
		        blo[i].push(val);
		        c[i].push(val);
		        val=xx;
			}
		}
		return upd(be2,(be2-1)*n1+1,r,val);
	}
}
int main(){
	n=rd();q=rd();
	for(int i=1;i<=n;++i)a[i]=rd();
	n1=sqrt(n);
	for(int i=1;i<=n;++i){
		be[i]=(i-1)/n1+1;
		blo[be[i]].push(a[i]);
	}
	int l,r,x;
	while(q--){
		l=rd();r=rd();x=rd();
		if(l<=r)printf("%d\n",solve(l,r,x));
		else printf("%d\n",solve(1,r,solve(l,n,x)));
	}
	return 0;
}
posted @ 2019-06-26 21:21  comld  阅读(357)  评论(0编辑  收藏  举报