调了一上午,样例都没过,求路过的神犇帮忙看看
查看原帖
调了一上午,样例都没过,求路过的神犇帮忙看看
262620
hez_EX楼主2022/7/22 13:12
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct node
{
	int sz,idl,idr,hp,ls,rs;
}tr[300005][10]; //tr[0]是末列平衡树 
int n,m,q,u,v,root[300005];
short tot[300005];
int makenode(int t,int l,int r)
{
	tr[t][++tot[t]]=(node){r-l+1,l,r,rand(),0,0};
	return tot[t];
}
void update(int t,int now)
{
	tr[t][now].sz=tr[t][tr[t][now].ls].sz+tr[t][tr[t][now].rs].sz+tr[t][now].idr-tr[t][now].idl+1;
}
int merge(int t,int x,int y)
{
	if(!x||!y) return x|y;
	if(tr[t][x].hp<tr[t][y].hp) return tr[t][x].rs=merge(t,tr[t][x].rs,y),update(t,x),x;
	else return tr[t][y].ls=merge(t,x,tr[t][y].ls),update(t,y),y;
}
void splitnode(int t,int now,int k,int &l,int &r)
{
	l=now;
	r=makenode(t,tr[t][now].idl+k,tr[t][now].idr);
	tr[t][l].idr=k-1;
}
void rank_split(int t,int now,int k,int &x,int &y)
{
	if(!now) return (void)(x=y=0);
	if(1<k-tr[t][tr[t][now].ls].sz&&k-tr[t][tr[t][now].ls].sz<=tr[t][now].idr-tr[t][now].idl+1)
	{
		int midl=0,midr=0;
		splitnode(t,now,k-tr[t][tr[t][now].ls].sz,midl,midr);
		tr[t][midl].rs=merge(t,tr[t][midl].rs,midr);
	}
	if(k<=tr[t][tr[t][now].ls].sz)
	{
		y=now;
		rank_split(t,tr[t][now].ls,k,x,tr[t][y].ls);
		update(t,y);
	}
	else if(tr[t][tr[t][now].ls].sz+tr[t][now].idr-tr[t][now].idr+1<=k)
	{
		x=now;
		rank_split(t,tr[t][now].rs,k-tr[t][tr[t][now].ls].sz-tr[t][now].idr+tr[t][now].idr-1,tr[t][x].rs,y);
		update(t,x);
	}
}
void inorder(int t,int now)
{
	if(!now) return;
	inorder(t,tr[t][now].ls);
	cout<<'['<<tr[t][now].idl<<','<<tr[t][now].idr<<"] ";
	inorder(t,tr[t][now].rs);
}
int change(int x,int y)
{
	int ret;
	if(y==m)
	{
		int ct=0,cmid=0,cb=0;
		rank_split(0,root[0],x-1,ct,cmid);
		rank_split(0,cmid,1,cmid,cb);
		cout<<"t: ";inorder(0,ct);puts("");
		cout<<"mid: ";inorder(0,cmid);puts("");
		cout<<"b: ";inorder(0,cb);puts("");
		ret=tr[0][cmid].idl;
		int ctmp=merge(0,ct,cb);
		root[0]=merge(0,ctmp,cmid);
	}
	else
	{
		int ll=0,lmid=0,lr=0,ct=0,cmid=0,cb=0;
		rank_split(x,root[x],y-1,ll,lmid);
		rank_split(x,lmid,1,lmid,lr);
		ret=tr[x][lmid].idl;
		rank_split(0,root[0],x-1,ct,cmid);
		rank_split(0,cmid,1,cmid,cb);
		int ltmp=merge(x,ll,lr),ctmp=merge(0,ct,cb);
		root[x]=merge(x,ltmp,cmid);
		root[0]=merge(0,ctmp,lmid);
		cout<<"l: ";inorder(x,root[x]);puts("");
		cout<<"c: ";inorder(0,root[0]);puts("");
	}
	return ret;
}
int main()
{
	srand((long long)new char);
	cin>>n>>m>>q;
	root[0]=makenode(0,1,n);
	for(int i=1;i<=n;i++) root[i]=makenode(i,1,m-1);
	while(q--)
	{
		cin>>u>>v;
		cout<<change(u,v)<<endl;
	}
	return 0;
}
2022/7/22 13:12
加载中...