bzoj3224 普通平衡树【splay版】

Description

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)

Input

第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)

Output

对于操作3,4,5,6每行输出一个数,表示对应答案




二叉平衡树模板题,splay版。

treap版见http://blog.csdn.net/sdfzyhx/article/details/51418974。

替罪羊树版见http://blog.csdn.Net/sdfzyhx/article/details/70212142。



#include<cstdio>
#include<cstring>
struct node
{
	int f,c[2],x,size,cnt;
}t[100010];
int root,size;
void up(int p)
{
	t[p].size=t[t[p].c[0]].size+t[t[p].c[1]].size+t[p].cnt;
}
void rot(int p,bool b)
{
	int x=t[p].f;
	int y=t[p].c[b];
	int z=t[y].c[!b];
	t[x].c[t[x].c[0]!=p]=y;
	if (y)
	  t[y].f=x;
	t[y].c[!b]=p;
	if (p)
	  t[p].f=y;
	t[p].c[b]=z;
	if (z)
	  t[z].f=p;
	up(p);
	up(y);
}
void splay(int p)
{
	int x=t[p].f;
	int y=t[x].f;
	if (!x) return;
	if (!y)
	{
		rot(x,t[x].c[0]!=p);
		return;
	}
	if ((t[x].c[0]==p)^(t[y].c[0]==x))
	  rot(x,t[x].c[0]!=p),rot(y,t[y].c[0]!=p),splay(p);
	else
	  rot(y,t[y].c[0]!=x),rot(x,t[x].c[0]!=p),splay(p);
}
void ins(int p,int x,int f,bool b)
{
	if (p==0)
	{
		p=++size;
		t[f].c[b]=p;
		t[p].f=f;
		t[p].x=x;
		t[p].size=t[p].cnt=1;
		splay(p);
		return;
	}
	t[p].size++;
	if (t[p].x==x)
	{
		t[p].cnt++;
		splay(p);
		return;
	}
	ins(t[p].c[x>t[p].x],x,p,x>t[p].x);
}
void del(int p,int x,int f,int b)
{
	if (t[p].x==x)
	{
		if (t[p].cnt>1)
		{
        	t[p].size--;
			t[p].cnt--;
			return;
		}
		if (t[p].c[0]==0&&t[p].c[1]==0)
		{
			t[f].c[b]=0;
			return;
		}
		if (t[p].c[0]*t[p].c[1]==0)
		{
			t[f].c[b]=t[p].c[0]+t[p].c[1];
			if (t[p].c[0])
			  t[t[p].c[0]].f=f;
			else
			  t[t[p].c[1]].f=f;
			return;
		}
		rot(p,t[t[p].c[1]].size<t[t[p].c[0]].size);
		t[t[p].f].size--;
		del(p,x,t[p].f,t[t[p].f].c[0]!=p);
		return;
	}
	t[p].size--;
	del(t[p].c[x>t[p].x],x,p,x>t[p].x);
}
int ran(int p,int x)
{
	if (p==0) return 0;
	if (x<t[p].x) return ran(t[p].c[0],x);
	if (x==t[p].x)
	{
		int ans=t[t[p].c[0]].size+1;
		splay(p);
		return ans;
	}
	return t[t[p].c[0]].size+t[p].cnt+ran(t[p].c[1],x);
}
int que(int p,int x)
{
	if (p==0) return 0;
	if (x<=t[t[p].c[0]].size) return que(t[p].c[0],x);
	if (x<=t[t[p].c[0]].size+t[p].cnt)
	{
		splay(p);
		return t[p].x;
	}
	return que(t[p].c[1],x-t[t[p].c[0]].size-t[p].cnt);
}
int pre(int p,int x)
{
	if (p==0) return -1;
	if (t[p].x<x)
	{
		int y=pre(t[p].c[1],x);
		if (y==-1)
		{
			splay(p);
			return t[p].x;
		}
		return y;
	}
	return pre(t[p].c[0],x);
}
int suc(int p,int x)
{
	if (p==0) return -1;
	if (t[p].x>x)
	{
		int y=suc(t[p].c[0],x);
		if (y==-1)
		{
			splay(p);
			return t[p].x;
		}
		return y;
	}
	return suc(t[p].c[1],x);
}
int main()
{
	int i,j,k,x,y,n;
	scanf("%d",&n);
	for (i=1;i<=n;i++)
	{
		scanf("%d%d",&x,&y);
		if (x==1) ins(t[0].c[0],y,0,0);
		if (x==2) del(t[0].c[0],y,0,0);
		if (x==3) printf("%d\n",ran(t[0].c[0],y));
		if (x==4) printf("%d\n",que(t[0].c[0],y));
		if (x==5) printf("%d\n",pre(t[0].c[0],y));
		if (x==6) printf("%d\n",suc(t[0].c[0],y));
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 有一个 $n$ 个点的棋盘,每个点上有一个数字 $a_i$,你需要从 $(1,1)$ 走到 $(n,n)$,每次只能往右或往下走,每个格子只能经过一次,路径上的数字和为 $S$。定义一个点 $(x,y)$ 的权值为 $a_x+a_y$,求所有满足条件的路径中,所有点的权值和的最小值。 输入格式 第一行一个整数 $n$。 接下来 $n$ 行,每行 $n$ 个整数,表示棋盘上每个点的数字。 输出格式 输出一个整数,表示所有满足条件的路径中,所有点的权值和的最小值。 数据范围 $1\leq n\leq 300$ 输入样例 3 1 2 3 4 5 6 7 8 9 输出样例 25 算法1 (形dp) $O(n^3)$ 我们可以先将所有点的权值求出来,然后将其看作是一个有权值的图,问题就转化为了在这个图中求从 $(1,1)$ 到 $(n,n)$ 的所有路径中,所有点的权值和的最小值。 我们可以使用形dp来解决这个问题,具体来说,我们可以将这个图看作是一棵,每个点的父节点是它的前驱或者后继,然后我们从根节点开始,依次向下遍历,对于每个节点,我们可以考虑它的两个儿子,如果它的两个儿子都被遍历过了,那么我们就可以计算出从它的左儿子到它的右儿子的路径中,所有点的权值和的最小值,然后再将这个值加上当前节点的权值,就可以得到从根节点到当前节点的路径中,所有点的权值和的最小值。 时间复杂度 形dp的时间复杂度是 $O(n^3)$。 C++ 代码 算法2 (动态规划) $O(n^3)$ 我们可以使用动态规划来解决这个问题,具体来说,我们可以定义 $f(i,j,s)$ 表示从 $(1,1)$ 到 $(i,j)$ 的所有路径中,所有点的权值和为 $s$ 的最小值,那么我们就可以得到如下的状态转移方程: $$ f(i,j,s)=\min\{f(i-1,j,s-a_{i,j}),f(i,j-1,s-a_{i,j})\} $$ 其中 $a_{i,j}$ 表示点 $(i,j)$ 的权值。 时间复杂度 动态规划的时间复杂度是 $O(n^3)$。 C++ 代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值