20分蒟蒻求助,为什么全超时了
查看原帖
20分蒟蒻求助,为什么全超时了
378996
UncleSam_Died楼主2023/7/2 21:15

蒟蒻太弱了,只能想到这个很弱时间复杂度又高的算法,希望有大佬可以指出如何优化

#include<bits/stdc++.h> 
using namespace std;
#define MAXN 1000000
int ch[MAXN][2],f[MAXN],sizes[MAXN],cnt[MAXN],key[MAXN];
int sz,root;
inline void clear(int x){
	ch[x][0]=ch[x][1]=f[x]=sizes[x]=cnt[x]=key[x]=0;
}
inline bool get(int x){
	return ch[f[x]][1]==x;
}
inline void update(int x){
	if (x){
		sizes[x]=cnt[x];
		if (ch[x][0]) sizes[x]+=sizes[ch[x][0]];
		if (ch[x][1]) sizes[x]+=sizes[ch[x][1]];
	}
}
inline void rotate(int x){
	int old=f[x],oldf=f[old],whichx=get(x);
	ch[old][whichx]=ch[x][whichx^1]; f[ch[old][whichx]]=old;
	ch[x][whichx^1]=old; f[old]=x;
	f[x]=oldf;
	if (oldf)
		ch[oldf][ch[oldf][1]==old]=x;
	update(old); update(x);
}
inline void splay(int x){
	for (int fa;fa=f[x];rotate(x))
	  if (f[fa])
	    rotate((get(x)==get(fa))?fa:x);
	root=x;
}
inline void insert(int x){
	if (root==0){sz++; ch[sz][0]=ch[sz][1]=f[sz]=0; root=sz; sizes[sz]=cnt[sz]=1; key[sz]=x; return;}
	int now=root,fa=0;
	while(1){
		if (x==key[now]){
			cnt[now]++; update(now); update(fa); splay(now); break;
		}
		fa=now;
		now=ch[now][key[now]<x];
		if (now==0){
			sz++;
			ch[sz][0]=ch[sz][1]=0;
			f[sz]=fa;
			sizes[sz]=cnt[sz]=1;
			ch[fa][key[fa]<x]=sz;
			key[sz]=x;
			update(fa);
			splay(sz);
			break;
		}
	}
}
inline int find(int x){
	int now=root,ans=0;
	while(1){
		if (x<key[now])
		  now=ch[now][0];
		else{
			ans+=(ch[now][0]?sizes[ch[now][0]]:0);
			if (x==key[now]){
				splay(now); return ans+1;
			}
			ans+=cnt[now];
			now=ch[now][1];
		}
	}
}
inline int findx(int x){
	int now=root;
	while(1){
		if (ch[now][0]&&x<=sizes[ch[now][0]])
		  now=ch[now][0];
		else{
			int temp=(ch[now][0]?sizes[ch[now][0]]:0)+cnt[now];
			if (x<=temp) return key[now];
			x-=temp; now=ch[now][1];
		}
	}
}
inline int pre(){
	int now=ch[root][0];
	while (ch[now][1]) now=ch[now][1];
	return now;
}
inline int next(){
	int now=ch[root][1];
	while (ch[now][0]) now=ch[now][0];
	return now;
}
inline void del(int x){
	int whatever=find(x);
	if (cnt[root]>1){cnt[root]--; update(root); return;}
	if (!ch[root][0]&&!ch[root][1]) {clear(root); root=0; return;}
	if (!ch[root][0]){
		int oldroot=root; root=ch[root][1]; f[root]=0; clear(oldroot); return;
	}
	else if (!ch[root][1]){
		int oldroot=root; root=ch[root][0]; f[root]=0; clear(oldroot); return;
	}
	int leftbig=pre(),oldroot=root;
	splay(leftbig);
	ch[root][1]=ch[oldroot][1];
	f[ch[oldroot][1]]=root;
	clear(oldroot);
	update(root); 
}
struct Num{
	int w,id;
};
Num num[2000005];
inline bool cmp(Num c,Num d){
	return c.w<d.w;
}
int s[2000005];
int main(){
	int n,opt,x;
	scanf("%d%d",&n,&opt);
	for(int i=1;i<=n;i++){
		cin>>num[i].w;
		num[i].id=i;
		insert(num[i].w);
	}
	sort(1+num,num+n+1,cmp);
	for(int i=1;i<=n;i++){
		s[num[i].id]=i;	
	}
//	for(int i=1;i<=n;i++){
//		cout<<s[num[i].id]<<" "<<num[i].id<<" "<<num[i].w<<" "<<i<<endl;
//	}
	for(int i=1;i<=opt;i++){
		cin>>x;
		cout<<findx(s[x])<<endl;
	}
}
2023/7/2 21:15
加载中...