跳到正文

题解

题解:P10650 「ROI 2017 Day 1」排序幻觉

2147483647str约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月14日
篇幅
约 7 分钟
题目
P10650

版本

  1. v1当前

提供一种大常数 01trie 做法。

对于 01trie 的每个节点,我们暴力计算其最大值,最小值并用状态压缩维护答案每一位是否能取 0/10/1。

具体地,为了保证迅速找到一个节点的最大最小值,我们需要用 set 维护,但为了保证时间复杂度,我们只对叶子节点开 set ,其它的节点类似线段树 pushup 。

时间复杂度 O(nlog⁡n)O(n\log n)

代码如下:

C++
#include<bits/stdc++.h>
#define l(p) t[p].ch[0]
#define r(p) t[p].ch[1]
#define max(x,y) (x)>(y)?(x):(y)
#define min(x,y) (x)<(y)?(x):(y)
using namespace std;
const int N=1e6+5,inf=0x3f3f3f3f;
typedef unsigned long long ll;
struct Node{
	int maxn,minn,depth;
	int ch[2],id;
	ll ans;
}t[N*30];
set<int>st[N<<1];
int tot,root,cnt;
void pushup(int p){
	if(!r(p)){
		t[p].maxn=t[l(p)].maxn;
		t[p].minn=t[l(p)].minn;
		t[p].ans=t[l(p)].ans|(1ll<<(t[p].depth*2))|(1ll<<(t[p].depth*2+1));
		return;
	}
	if(!l(p)){
		t[p].maxn=t[r(p)].maxn;
		t[p].minn=t[r(p)].minn;
		t[p].ans=t[r(p)].ans|(1ll<<(t[p].depth*2))|(1ll<<(t[p].depth*2+1));
		return;
	}
	t[p].maxn=max(t[l(p)].maxn,t[r(p)].maxn);
	t[p].minn=min(t[l(p)].minn,t[r(p)].minn);
	t[p].ans=t[l(p)].ans&t[r(p)].ans;
	if(t[l(p)].maxn<t[r(p)].minn)t[p].ans|=1ll<<(t[p].depth*2);//0
	if(t[l(p)].minn>t[r(p)].maxn)t[p].ans|=1ll<<(t[p].depth*2+1);//1
}
void change(int&p,int d,int x,int v,bool f){
	if(!p){
		p=++tot,t[p].id=0;
		t[p].maxn=-inf;
		t[p].minn=inf;
	}
	t[p].depth=d;
	if(d==-1){
		if(!t[p].id)t[p].id=++cnt;
		if(f){//加入
			st[t[p].id].insert(v);
			t[p].depth=-1;t[p].ans=0;
			t[p].maxn=max(t[p].maxn,v);
			t[p].minn=min(t[p].minn,v);
		}else{//删除
			st[t[p].id].erase(v);
			t[p].depth=-1;t[p].ans=0;
			if(st[t[p].id].size()){
				if(t[p].maxn==v)t[p].maxn=*(st[t[p].id].rbegin());
				if(t[p].minn==v)t[p].minn=*(st[t[p].id].begin());
			}else{
				t[p].maxn=-inf;
				t[p].minn=inf;
			}
		}
		return;
	}change(t[p].ch[x>>d&1],d-1,x,v,f);
	pushup(p);
}
int query(){
	int ans=0;ll x=t[root].ans;
	for(int i=30;i>=0;i--){
		int op0=x>>(i*2)&1,op1=x>>(i*2+1)&1;
		if(op0)continue;
		if(op1)ans|=1<<i;
		else return -1;
	}return ans;
}
int n,a[N],m;
namespace Fread {
	const int SIZE=1<<21;char buf[SIZE],*S,*T;
	inline char getchar() {if(S==T){T=(S=buf)+fread(buf,1,SIZE,stdin);if(S==T)return '\n';}return *S++;}
}
namespace Fwrite {
	const int SIZE=1<<21;
	char buf[SIZE],*S=buf,*T=buf+SIZE;
	inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}
	inline void putchar(char c){*S++=c;if(S==T)flush();}
	struct POPOSSIBLE{~POPOSSIBLE(){flush();}}ztr;
}
#define getchar Fread :: getchar
#define putchar Fwrite :: putchar
namespace Fastio{
	struct Reader{
	    template<typename T>
    	Reader& operator >> (T& x) {
        	char c=getchar();T f=1;
        	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}x=0;
        	while(c>='0'&&c<='9'){x=x*10+(c-'0');c=getchar();}x*=f;
	        return *this;
    	}
	    Reader(){}
	}cin;
	struct Writer{
	    template<typename T>
	    Writer& operator << (T x) {
	        if(x==0){putchar('0');return *this;}
	        if(x<0){putchar('-');x=-x;}
	        static int sta[45];int top=0;
	        while(x){sta[++top]=x%10;x/=10;}
	        while(top){putchar(sta[top]+'0');--top;}
	        return *this;
    	}
    	Writer& operator << (char c) {putchar(c);return *this;}
    	Writer(){}
	}cout;
}
#define endl '\n'
#define cin Fastio :: cin
#define cout Fastio :: cout
int main(){
//	freopen("sort.in","r",stdin);
//	freopen("sort.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)change(root,30,a[i],i,1);
	cout<<query()<<endl;
	cin>>m;
	for(int i=1;i<=m;i++){
		int x,y;cin>>x>>y;
		change(root,30,a[x],x,0);
		a[x]=y;
		change(root,30,a[x],x,1);
		cout<<query()<<endl;
	}
	return 0;
}
原文