AHOI 2006 文本编辑器

一道splay的模板题,收录

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits> 
#include <set>
#include <string> 
#include <sstream>
#include <utility>
#include <ctime>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::getline;
using std::make_pair;
using std::greater;

const int MAXN(2100000);

struct SPLAY_TREE
{
	struct NODE 
	{
		char data;
		int reverse;
		int size;
		NODE *fa;
		NODE *ch[2];
	};
	
	NODE pool[MAXN];
	NODE *root, *NIL, *rear;
	NODE *ll, *rl;

	void push_up(NODE *sour)
	{
		sour->size = sour->ch[0]->size+sour->ch[1]->size+1;
	}

	void push_down(NODE *sour)
	{
		if(sour->reverse)
		{
			sour->ch[0]->reverse ^= 1;
			sour->ch[1]->reverse ^= 1;
			swap(sour->ch[0], sour->ch[1]);
			sour->reverse = 0;
		}
	}

	void init()
	{
		NIL = pool;
		rear = pool+1;
		NIL->data = ' ';
		NIL->size = 0;
		NIL->reverse = 0;
		NIL->fa = NIL->ch[0] = NIL->ch[1] = NIL;
		newnode(root, NIL, ' ');
		newnode(root->ch[1], root, ' ');
		ll = root;
		rl = root->ch[1];
		push_up(rl);
		push_up(ll);
	}

	void newnode(NODE *&sour, NODE *f, char td)
	{
		rear->data = td;
		rear->size = 1;
		rear->fa = f;
		rear->reverse = 0;
		rear->ch[0] = rear->ch[1] = NIL;
		sour = rear++;
	}

	void rotate(NODE *sour, int flag)
	{
		NODE *f = sour->fa;
		push_down(f);
		push_down(sour);
		f->ch[!flag] = sour->ch[flag];
		sour->ch[flag]->fa = f;
		sour->fa = f->fa;
		if(f->fa != NIL)
			f->fa->ch[f->fa->ch[1] == f] = sour;
		sour->ch[flag] = f;
		f->fa = sour;
		push_up(f);
	}

	void splay(NODE *sour, NODE *goal)
	{
		push_down(sour);
		while(sour->fa != goal)
		{
			if(sour->fa->fa == goal)
				rotate(sour, sour->fa->ch[0] == sour);
			else
			{
				NODE *f = sour->fa;
				int flag = (f->fa->ch[0] == f);
				if(f->ch[flag] == sour)
					rotate(sour, !flag);
				else
					rotate(f, flag);
				rotate(sour, flag);
			}
		}
		push_up(sour);
		if(goal == NIL)
			root = sour;
	}

	NODE *select(NODE *sour, int k)
	{
		push_down(sour);
		while(sour != NIL && k != sour->ch[0]->size+1)
		{
			if(k <= sour->ch[0]->size)
				sour = sour->ch[0];
			else
			{
				k -= sour->ch[0]->size+1;
				sour = sour->ch[1];
			}
			push_down(sour);
		}
		return sour;
	}

	void move(int k)
	{
		NODE *temp = select(root, k+1);
		splay(temp, NIL);
	}

	void insert(int n)
	{
		char td;
		NODE *temp = select(root->ch[1], 1);
		splay(temp, root);
		td = getchar();
		newnode(temp->ch[0], temp, td);
		temp = temp->ch[0];
		--n;
		while(n)
		{
			td = getchar();
			newnode(temp->ch[1], temp, td);
			temp = temp->ch[1];
			--n;
		}
		splay(temp, root);
		push_up(root);
	}

	void Delete(int n)
	{
		NODE *temp = select(root->ch[1], n+1);
		splay(temp, root);
		temp->ch[0] = NIL;
		push_up(temp);
		push_up(root);
	}

	void Rotate(int n)
	{
		NODE *temp = select(root->ch[1], n+1);
		splay(temp, root);
		temp->ch[0]->reverse ^= 1;
	}
	
	void get()
	{
		NODE *temp = select(root->ch[1], 1);
		splay(temp, root);
		printf("%c\n", temp->data);
	}

	void prev()
	{
		NODE *temp = select(root->ch[0], root->ch[0]->size);
		splay(temp, NIL);
	}

	void next()
	{
		NODE *temp = select(root->ch[1], 1);
		splay(temp, NIL);
	}

	void debug()
	{
		debug_(root);
		printf("\n");
	}

	void debug_(NODE *sour)
	{
		if(sour == NIL)
			return;
		debug_(sour->ch[0]);
		putchar(sour->data);
		debug_(sour->ch[1]);
	}
};

SPLAY_TREE spt;

int main()
{
	int n;
	char str[10];
	int op;
	while(~scanf("%d", &n))
	{
		spt.init();
		for(int i = 0; i < n; ++i)
		{
			scanf("%s", str);
			switch(str[0])
			{
			case 'M':	scanf("%d", &op);
						spt.move(op);
						break;
			case 'I':	scanf("%d", &op);
						while(getchar() != '\n');
						spt.insert(op);
						break;
			case 'D':	scanf("%d", &op);
						spt.Delete(op);
						break;
			case 'R':	scanf("%d", &op);
						spt.Rotate(op);
						break;
			case 'G':	spt.get();
						break;
			case 'P':	spt.prev();
						break;
			case 'N':	spt.next();
						break;
			}
		//	printf("%c\n", spt.root->data);
		//	spt.debug();
		}
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值