鸭蛋求调(关),yl输入都有问题
查看原帖
鸭蛋求调(关),yl输入都有问题
1074889
zg_ji楼主2024/7/13 08:44
#include<bits/stdc++.h>
const int N=3e6+100;
const int mod=1000000;
const int inf = 1<<31-1;
using namespace std;

int n;
int sz,root;
int ch[N][2],size[N],f[N],cnt[N],key[N];

inline int read()
{
	int f=1,x=0;
	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 f*x;
}

inline void clear(int x)
{
	ch[x][0]=ch[x][1]=f[x]=size[x]=cnt[x]=key[x]=0;
}

inline bool get(int x)
{
	return ch[f[x]][1]==x;
}

inline void update(int x)
{
	if (x)
	{
		size[x]=cnt[x];
		if (ch[x][0]) size[x]+=size[ch[x][0]];
		if (ch[x][1]) size[x]+=size[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; size[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;
			size[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]?size[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<=size[ch[now][0]])
		  now=ch[now][0];
		else{
			int temp=(ch[now][0]?size[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); 
}

int main()
{
	cin>>n;
	insert(-inf),insert(inf);
	int x,y;
	int ans=0,tot=0,now=0;
	while(n--)
	{
		cin>>x>>y;
		insert(y);
		if(!tot) now=x;
		if(now==x^1&&tot) tot--;
		else
		{
			tot++;
			continue;
		}
		int u=pre(),v=next();
		del(root);
		if(u==1&&v==2) continue;
		if((v==2||abs(key[u]-y))<=abs(key[v]-y))
			ans=(ans+abs(u)-y)%mod,del(u);
		else
			ans=(ans+abs(key[v]-y))%mod;
			del(v);
	}
	cout<<ans<<'\n';
	
	return 0;
	
}
2024/7/13 08:44
加载中...