HDU1754 I Hate It (Splay)

本文介绍使用Splay树进行单点更新及区间查询的方法。通过详细解释算法流程,包括节点结构定义、树的构建、旋转操作、Splay操作及查询实现等,帮助读者理解如何利用Splay树高效地完成此类任务。

题意:中文题。。。

以前用线段树写过。。现在学Splay,也来写一下。。

用伸展树来进行单点更新和区间查询,单点更新就直接查选第k个直接查选到这个点然后再维护上去就可以了,,区间查选,因为这里是闭区间,所以要先把区间扩大一下(自己用手写下就知道),然后第k个找到左边界的点,旋转到根,找到右边界的点旋转到根的右子树,然后右边界的点的左子树就是要找的答案了。。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=222222;
struct Node
{
	Node *ch[2];
	Node *pre;
	int size,val,mx;
}p[MAXN];
int num[MAXN];
int tot;
Node *null=&p[0];
Node *root;
Node *new_Node(int val)
{
	Node *rt=&p[++tot];
	rt->val=val;
	rt->size=1;
	rt->ch[0]=null;
	rt->ch[1]=null;
	rt->pre=null;
	return rt;
}
void pushup(Node *rt)
{
	rt->mx=max(rt->val,max(rt->ch[0]->mx,rt->ch[1]->mx));
	rt->size=rt->ch[0]->size+rt->ch[1]->size+1;
}
Node *build(int l,int r)
{
	if(l>r)
		return null;
	Node *rt;
	int mid=(l+r)>>1;
	rt=new_Node(num[mid-1]);
	rt->ch[0]=build(l,mid-1);
	rt->ch[0]->pre=rt;
	rt->ch[1]=build(mid+1,r);
	rt->ch[1]->pre=rt;
	pushup(rt);
	return rt;
}
void Rotate(Node *x,int c)
{
	Node *y=x->pre;
	y->ch[!c]=x->ch[c];
	if(x->ch[c]!=null)
	{
		x->ch[c]->pre=y;
	}
	x->pre=y->pre;
	if(y->pre!=null)
	{
		if(y->pre->ch[0]==y)
			y->pre->ch[0]=x;
		else
			y->pre->ch[1]=x;
	}
	x->ch[c]=y;
	y->pre=x;
	if(y==root)
		x=root;
	pushup(y);
	pushup(x);
}
void splay(Node *x,Node *f)
{
	while(x->pre!=f)
	{
		if(x->pre->pre==f)
		{
			if(x->pre->ch[0]==x)
			{
				Rotate(x,1);
			}
			else
			{
				Rotate(x,0);
			}
		}
		else
		{
			Node *y=x->pre,*z=y->pre;
			if(z->ch[0]==y)
			{
				if(y->ch[0]==x)
				{
					Rotate(y,1);
					Rotate(x,1);
				}
				else
				{
					Rotate(x,0);
					Rotate(x,1);
				}
			}
			else
			{
				if(y->ch[1]==x)
				{
					Rotate(y,0);
					Rotate(x,0);
				}
				else
				{
					Rotate(x,1);
					Rotate(x,0);
				}
			}
		}
	}
	pushup(x);
}
Node *kth(Node *rt,int k)
{
	if(rt->ch[0]->size+1==k)
		return rt;
	if(k<=rt->ch[0]->size)
		return kth(rt->ch[0],k);
	else
		return kth(rt->ch[1],k-(rt->ch[0]->size)-1);
}
int main()
{
	int n,m,i;
	Node *L,*R;
	while(scanf("%d%d",&n,&m)==2)
	{
		for(i=1;i<=n;i++)
			scanf("%d",&num[i]);
		tot=0;
		num[0]=0;
		num[n+1]=0;
		root=build(1,n+2);
		char op[2];
		int x,y;
		while(m--)
		{
			scanf("%s%d%d",op,&x,&y);
			if(op[0]=='Q')
			{
				y+=2;
				L=kth(root,x);
				R=kth(root,y);
				splay(L,null);
				splay(R,L);
				printf("%d\n",R->ch[0]->mx);
				root=L;
			}
			else
			{
				x++;
				L=kth(root,x);
				splay(L,null);
				root=L;
				root->val=y;
				pushup(root);
			}
		}
	}
	return 0;
}


下载方式:https://renmaiwang.cn/s/t0445 在时序发生器设计实验中,如何达成T4至T1的生成? 时序发生器的构建可以通过运用一个4位循环移位寄存器来达成T4至T1的输出。 具体而言:- **CLR(清除)**: 作为全局清零信号,当CLR呈现低电平状态时,所有输出(涵盖T1至T4)皆会被清除。 - **STOP**: 在T4脉冲的下降沿时刻,若STOP信号处于低电平状态,则T1至T4会被重置。 - **启动流程**: 当启动信号START处于高电平,并且STOP为高电平时,移位寄存器将在每个时钟的上升沿向左移动一位。 移位寄存器的输出端对应了T4、T3、T2、T1。 #### 2. 时序发生器如何调控T1至T4的波形形态? 时序发生器通过以下几个信号调控T1至T4的波形形态:- **CLR**: 当CLR处于低电平状态时,所有输出均会被清零。 - **STOP**: 若STOP信号为低电平,且在T4脉冲的下降沿时刻,所有输出同样会被清零。 - **START**: 在START信号有效(通常为高电平),并且STOP为高电平时,移位寄存器启动,从而产生环形脉冲输出。 ### 微程序控制器实验#### 3. 微程序控制器实验中的四条机器指令及其对应的微程序段指定的机器指令及其关联的微程序段如下:- **NOP**: 00- **R0->B**: 04- **A+B->R0**: 05- **P<1>**: 30- **IN->R0**: 32- **R0->OUT**: 33- **HLT**: 35#### 4. 微程序段中的微操作/微命令序列针对每条微指令,其对应的微操作或微命令序列如下:- **IN->R0**: 输入(IN)单元的数据被...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值