[BZOJ1012]JSOI2008 最大数|线段树|平衡树

水题一道,显然线段树水过,不过我为了练练spaly就写了个splay作死,巨慢。。

#include<iostream>
#include<cstdio>
using namespace std;
const int inf=99999999,maxn=200005;
int m,d,i,nnode=0,root=0,last,l,get,c[maxn][2],size[maxn],lmax[maxn],num[maxn],pre[maxn];
char cc;
void update(int x){size[x]=size[c[x][1]]+size[c[x][0]]+1;lmax[x]=max(max(lmax[c[x][0]],lmax[c[x][1]]),num[x]);}
void newnode(int &x,int fa,int k)
{
	x=++nnode;
	pre[x]=fa;num[x]=k;size[x]=1;lmax[x]=k;
	c[x][0]=c[x][1]=0;
}
void rot(int x,int kind)
{
	int y=pre[x];int z=pre[y];
	c[y][!kind]=c[x][kind];pre[c[x][kind]]=y;
	c[x][kind]=y;pre[y]=x;
	pre[x]=z;
	if (z) c[z][c[z][1]==y]=x;
	update(y);update(x);if (z) update(z);
}
void splay(int x,int goal)
{
	int y,z,kind;
	while (pre[x]!=goal)
	{
		y=pre[x];
		if (pre[y]==goal) rot(x,c[y][0]==x);
		else
		{
			z=pre[y],kind=c[z][0]==y;
			if (c[y][kind]!=x) rot(y,kind); else rot(x,!kind);
			rot(x,kind);
		} 
	}
	if (goal==0) root=x;
}
int findkth(int x,int k)
{
	if (k<=size[c[x][0]]) return findkth(c[x][0],k);
	if (k==size[c[x][0]]+1) return x;
	if (k>size[c[x][0]]+1) return findkth(c[x][1],k-1-size[c[x][0]]);
}
void insert(int k)
{
	get=root;
	while (c[get][1]) get=c[get][1];
	newnode(c[get][0],get,k);
	splay(c[get][0],0);
}
void solve(int l)
{
	get=findkth(root,nnode-l-1);splay(get,0);
	last=lmax[c[root][1]];
	printf("%d\n",last);
}
void print(int x)
{
	if (c[x][0]) print(c[x][0]);
	printf("x->num:%d,size:%d,pre:%d,lmax:%d\n",num[x],size[x],pre[x],lmax[x]);
	if (c[x][1]) print(c[x][1]);
}
int main()
{
	scanf("%d%d",&m,&d);
	c[0][1]=c[0][0]=size[0]=pre[0]=0;num[0]=lmax[0]=-inf;last=0;
	newnode(root,0,-inf);newnode(c[root][1],root,-inf);update(root);
	for (i=1;i<=m;i++)
	{
		scanf("%c",&cc);
		scanf("%c%d",&cc,&l);
		//print(root);
		if (cc=='A') insert((l+last)%d);
		else solve(l);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值