HDU3487 Play with Chain (Splay)

本文详细介绍了Splay树的数据结构及其在解决特定问题中的应用。通过实例解析了如何使用Splay树处理区间操作,包括区间切割和翻转等复杂操作,并提供了完整的代码实现。

题意:给两种操作,一种是切下[a,b]这一段加到c这个位置之后,还有一个翻转[a,b]这个位置,操作过后输出序列。

Splay。。。。虽然理论比较简单,但本渣还是写了一下午+调了3个小时。。。

对于两种操作,切下来的时候就是先把a-1旋转到根,然后把b+1旋转到a-1那个节点的右边,然后b+1这个节点的左子树就是[a,b]的序列了,插入把c旋转到根,c+1旋转到根的右边,然后直接插入到c+1的左子树就好了(这时一定是空的)。。翻转就好像线段树那样,弄个延迟标记就好了。

有很多细节要想清。。。首先,pushup和pushdown的位置要考虑清楚,然后就是注意边界的问题,比如1为左边界,那么我们就是查0这个点了,如果建树只建了1-n就会RE(我RE了几次),所以这里要想清,最后,我居然最后把dfs输出给写错了,导致样例都没过。。。还是太弱了。。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=300010;
vector<int> ans;
int num[MAXN];
struct Node
{
	Node *ch[2];
	Node *fa;
	int val,mark;
	int size;
}p[MAXN];
Node *null=&p[0];
int tot=0;
Node *root=null;
//debug部分copy from hh 
void Treaval(Node *x) {    
    if(x!=null) {    
        Treaval(x->ch[0]);    
        printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,key = %2d   mark=%d\n",x->val,x->ch[0]->val,x->ch[1]->val,x->fa->val,x->size,x->val,x->mark);    
        Treaval(x->ch[1]);    
    }    
}    
void debug() {printf("%d\n",root->val);Treaval(root);}    
//以上Debug  
void pushup(Node *rt)
{
	rt->size=1;
	if(rt->ch[0]!=null)
		rt->size+=rt->ch[0]->size;
	if(rt->ch[1]!=null)
		rt->size+=rt->ch[1]->size;
}
void pushdown(Node *rt)
{
	if(rt->mark)
	{
		rt->ch[0]->mark^=1;
		rt->ch[1]->mark^=1;
		rt->mark=0;
		swap(rt->ch[0],rt->ch[1]);
	}
}
void Rotate(Node *x,int c)
{
	Node *y=x->fa;
	pushdown(y);
	pushdown(x);
	y->ch[!c]=x->ch[c];
	if(x->ch[c]!=null)
		x->ch[c]->fa=y;
	x->fa=y->fa;
	if(y->fa!=null)
	{
		if(y->fa->ch[0]==y)
			y->fa->ch[0]=x;
		else
			y->fa->ch[1]=x;
	}
	x->ch[c]=y;
	y->fa=x;
	if(y==root)
		x=root;
	pushup(y);
}
void splay(Node *x,Node *f)
{
	if(x==null)
		return;
	while(x->fa!=f)
	{
		if(x->fa->fa==f)
		{
			if(x->fa->ch[0]==x)
				Rotate(x,1);
			else
				Rotate(x,0);
		}
		else
		{
			Node *y=x->fa,*z=y->fa;
			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 *new_Node(int val)
{
	Node *rt=&p[++tot];
	rt->ch[0]=rt->ch[1]=rt->fa=null;
	rt->size=1;
	rt->mark=0;
	rt->val=val;
	return rt;
}
Node *build(int l,int r)
{
	if(l>r)
		return null;
	int mid=(l+r)>>1;
	Node *rt=new_Node(num[mid]);
	rt->ch[0]=build(l,mid-1);
	rt->ch[1]=build(mid+1,r);
	rt->ch[0]->fa=rt->ch[1]->fa=rt;
	pushup(rt);
	return rt;
}
Node *kth(Node *rt,int k)
{
	pushdown(rt);
	if(rt->ch[0]->size+1==k)
		return rt;
	else if(rt->ch[0]->size>=k)
		return kth(rt->ch[0],k);
	else
		return kth(rt->ch[1],k-rt->ch[0]->size-1);
}
void solvecut(int a,int b,int c)
{
	Node *L,*R;
	L=kth(root,a);
	splay(L,null);
	R=kth(L,b+2);
	splay(R,L);
	Node *root1=R->ch[0];
	root1->ch[0]->fa=null;
	R->ch[0]->fa=null;
	R->ch[0]=null;
	Node *LL=kth(L,c+1);
	Node *RR=kth(L,c+2);
	splay(LL,null);
	splay(RR,LL);
	RR->ch[0]=root1;
	root1->fa=RR;
	splay(root1,null);
	splay(root,null);
}
void solveflip(int a,int b)
{
	Node *L,*R;
	L=kth(root,a);
	splay(L,null);
	root=L;
	R=kth(root,b+2);
	splay(R,L);
	R->ch[0]->mark^=1;
}
int flag;
void prin(Node *rt)
{
	if(rt==null)
		return;
	pushdown(rt);
	prin(rt->ch[0]);
	ans.push_back(rt->val);
	prin(rt->ch[1]);
	//printf("size=%d val=%d\n",rt->size,rt->val);
}
int main()
{
	int n,m,i,a,b,c;
	while(scanf("%d%d",&n,&m)==2)
	{
		if(n==-1&&m==-1)
			break;
		char op[10];
		if(n==1)
		{
			scanf("%s",op);
			if(op[0]=='C')
				scanf("%*d%*d%*d");
			else
				scanf("%*d%*d");
			printf("1\n");
		}
		//null->fa=null->ch[0]=null->ch[1]=null;
		ans.clear();
		tot=0;
		for(i=0;i<=n+2;i++)
			num[i]=i;
		num[n+1]=num[n+2]=0;
		root=build(0,n+2);
		//debug();
		while(m--)
		{
			scanf("%s",op);
			if(op[0]=='C')
			{
				scanf("%d%d%d",&a,&b,&c);
				solvecut(a,b,c);
				//debug();
			}
			else
			{
				scanf("%d%d",&a,&b);
				solveflip(a,b);
			}
		}
		flag=1;
		prin(root);
		//debug();
		for(i=0;i<ans.size();i++)
		{
			if(ans[i]==0)
				continue;
			if(flag)
				printf("%d",ans[i]);
			else
				printf(" %d",ans[i]);
			flag=0;
		}
		printf("\n");
	}
	return 0;
}


### HDU4453 Splay 问题的C++实现及其时间复杂度分析 HDU4453 是一个涉及Splay Tree(伸展树)的问题,要求对一系列操作进行高效处理。以下是对该问题的时间复杂度分析以及C++实现。 #### 1. Splay Tree 的基本操作时间复杂度 Splay Tree 是一种自调整二叉搜索树,其核心思想是通过旋转操作将访问过的节点移动到根节点位置,从而优化后续访问性能。Splay Tree 的基本操作包括插入、删除、查找和分裂等,这些操作的时间复杂度均为 \(O(\log n)\) 在均摊意义上[^1]。 - **插入操作**:在Splay Tree 中插入一个新节点时,首先需要找到插入位置,这一步的时间复杂度为 \(O(\log n)\)。插入后,树会通过一系列旋转操作将新节点调整到根节点位置。 - **删除操作**:删除操作需要先找到目标节点并将其旋转到根节点位置,然后调整树结构以移除该节点。这一过程的时间复杂度同样为 \(O(\log n)\)。 - **查找操作**:查找操作需要从根节点开始向下遍历,直到找到目标节点或确定目标节点不存在。查找完成后,目标节点会被旋转到根节点位置。查找操作的时间复杂度为 \(O(\log n)\)。 #### 2. HDU4453 的具体问题分析 HDU4453 要求对一个序列执行多种操作,包括插入、删除、查询排名、查询值等。以下是针对这些操作的时间复杂度分析: - **插入操作**:每次插入一个新元素时,Splay Tree 需要调整树结构以保持平衡。由于 Splay Tree 的插入操作时间复杂度为 \(O(\log n)\),因此对于 \(m\) 次插入操作,总时间复杂度为 \(O(m \log n)\)[^1]。 - **删除操作**:删除操作与插入操作类似,时间复杂度为 \(O(\log n)\)。对于 \(m\) 次删除操作,总时间复杂度为 \(O(m \log n)\)[^1]。 - **查询排名**:查询某个值的排名需要从根节点开始向下遍历,并根据左子树的大小计算排名。这一过程的时间复杂度为 \(O(\log n)\)[^1]。 - **查询值**:查询排名对应的值也需要从根节点开始向下遍历,时间复杂度同样为 \(O(\log n)\)。 #### 3. C++ 实现示例 以下是一个基于 Splay Tree 的 C++ 实现示例: ```cpp #include <bits/stdc++.h> using namespace std; struct Node { int val, size; Node *left, *right, *parent; Node(int v) : val(v), size(1), left(nullptr), right(nullptr), parent(nullptr) {} }; int getSize(Node* node) { return node ? node->size : 0; } void updateSize(Node* node) { if (node) node->size = 1 + getSize(node->left) + getSize(node->right); } void rotateRight(Node* &root, Node* y) { Node* x = y->left; y->left = x->right; if (x->right) x->right->parent = y; x->right = y; x->parent = y->parent; y->parent = x; if (x->parent) { if (x->parent->left == y) x->parent->left = x; else x->parent->right = x; } updateSize(y); updateSize(x); root = x; } void rotateLeft(Node* &root, Node* x) { Node* y = x->right; x->right = y->left; if (y->left) y->left->parent = x; y->left = x; y->parent = x->parent; x->parent = y; if (y->parent) { if (y->parent->left == x) y->parent->left = y; else y->parent->right = y; } updateSize(x); updateSize(y); root = y; } void splay(Node* &root, Node* x) { while (x->parent) { Node* p = x->parent; if (!p->parent) { if (p->left == x) rotateRight(root, p); else rotateLeft(root, p); } else { Node* pp = p->parent; if (pp->left == p) { if (p->left == x) { rotateRight(root, p); rotateRight(root, pp); } else { rotateLeft(root, x); rotateRight(root, pp); } } else { if (p->right == x) { rotateLeft(root, p); rotateLeft(root, pp); } else { rotateRight(root, x); rotateLeft(root, pp); } } } } root = x; } Node* findKth(Node* root, int k) { while (true) { int leftSize = getSize(root->left); if (k <= leftSize) { root = root->left; } else if (k == leftSize + 1) { return root; } else { k -= leftSize + 1; root = root->right; } } } Node* insert(Node* &root, int key) { if (!root) return root = new Node(key); Node* cur = root; while (true) { if (key < cur->val) { if (!cur->left) { cur->left = new Node(key); cur->left->parent = cur; break; } cur = cur->left; } else { if (!cur->right) { cur->right = new Node(key); cur->right->parent = cur; break; } cur = cur->right; } } updateSize(root); Node* newNode = key < cur->val ? cur->left : cur->right; splay(root, newNode); return root; } Node* remove(Node* &root, int key) { if (!root) return nullptr; Node* cur = root; while (cur) { if (key == cur->val) break; cur = key < cur->val ? cur->left : cur->right; } if (!cur) return root; splay(root, cur); if (!cur->left && !cur->right) { delete cur; root = nullptr; } else if (!cur->left) { root = cur->right; cur->right->parent = nullptr; delete cur; } else if (!cur->right) { root = cur->left; cur->left->parent = nullptr; delete cur; } else { Node* maxLeft = cur->left; while (maxLeft->right) maxLeft = maxLeft->right; splay(root, maxLeft); maxLeft->right = cur->right; cur->right->parent = maxLeft; delete cur; } updateSize(root); return root; } ``` #### 4. 总结 在 HDU4453 中,使用 Splay Tree 可以高效地完成插入、删除、查询排名和查询值等操作。所有这些操作的时间复杂度均为 \(O(\log n)\) 在均摊意义上[^1]。因此,对于 \(m\) 次操作,总时间复杂度为 \(O(m \log n)\)。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值