HDU 4286 Data Handler(splay)

保存两个光标的位置。插入,删除,翻转,splay基本操作。这题用G++交mle,用C++交就可以

这里

//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

#pragma comment(linxer, "/STACK:102400000,102400000")
#define LL long long 
#define pii pair<int, int>
#define MP maxe_pair
#define lx i << 1
#define rx lx | 1
#define md (ll + rr >> 1)
#define lson ll, md, lx
#define rson md + 1, rr, rx
#define mod 1000000007
#define inf 25000
#define N 1000020
#define M 500010

int ch[N][2], pre[N], sz[N];
int n, tot, root;
short int a[N], val[N];
bool down[N], ok;

int creat(int v, int fa){
	int x = ++tot;
	pre[x] = fa;
	ch[x][0] = ch[x][1] = 0;
	val[x] = v;
	sz[x] = 1;
	down[x] = 0;
	return x;
}
void push_up(int x){
	sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;
}
int build(int ll, int rr, int fa){
	if(ll > rr) return 0;
	int x = creat(a[md], fa);
	ch[x][0] = build(ll, md - 1, x);
	ch[x][1] = build(md + 1, rr, x);
	push_up(x);
	return x;
}
void push_down(int x){
	if(down[x]){
		swap(ch[x][0], ch[x][1]);
		if(ch[x][0]) down[ch[x][0]] ^= 1;
		if(ch[x][1]) down[ch[x][1]] ^= 1;
		down[x] = 0;
	}
}
void rotate(int x){
	int y = pre[x], d = ch[y][1] == x;
	ch[y][d] = ch[x][!d];
	if(ch[x][!d]) pre[ch[x][!d]] = y;
	ch[x][!d] = y;
	pre[x] = pre[y];
	pre[y] = x;
	if(pre[x]) ch[pre[x]][ch[pre[x]][1] == y] = x;
	push_up(y);
}
void P(int x){
	if(pre[x])
		P(pre[x]);
	push_down(x);
}
void splay(int x, int goal){
	P(x);
	while(pre[x] != goal){
		int f = pre[x], ff = pre[f];
		if(ff == goal)
			rotate(x);
		else if((ch[ff][1] == f) == (ch[f][1] == x))
			rotate(f), rotate(x);
		else 
			rotate(x), rotate(x);
	}
	push_up(x);
	if(goal == 0) root = x;
}
int Kth(int k){
	int x = root;
	while(x){
		push_down(x);
		if(sz[ch[x][0]] >= k) x = ch[x][0];
		else{
			k -= sz[ch[x][0]] + 1;
			if(k == 0) return x;
			x = ch[x][1];
		}
	}
	return x;
}

void output(int x){
	if(!x) return ;
	push_down(x);
	output(ch[x][0]);
	if(val[x] < inf){
		if(!ok) ok = 1;
		else printf(" ");
		printf("%d", val[x]);
	}
	output(ch[x][1]);
}
void Insert(int k, int v){
	if(k == 0){
		splay(Kth(1), 0);
		ch[root][0] = creat(v, root);
		push_up(root);
		return ;
	}
	splay(Kth(k), 0);
	splay(Kth(k+1), root);
	int f = ch[root][1];
	ch[f][0] = creat(v, f);
	push_up(f);
	push_up(root);
}
void Delete(int k){
	if(k == 0){
		splay(Kth(1), 0);
		ch[root][0] = 0;
		push_up(root);
		return ;
	}
	splay(Kth(k), 0);
	splay(Kth(k+2), root);
	int f = ch[root][1];
	ch[f][0] = 0;
	push_up(f);
	push_up(root);
}
int main(){
	int cas;
	scanf("%d", &cas);
	while(cas--){
		scanf("%d", &n);
		for(int i = 1; i <= n; ++i){
			int v;
			scanf("%d", &v);
			a[i] = v;
		}
		tot = 0;
		a[0] = inf, a[n+1] = inf;
		root = build(0, n + 1, 0);
		int L, R, m;
		scanf("%d%d%d", &L, &R, &m);
		char s[20];
		while(m--){
			scanf("%s", s);
			if(s[0] == 'M' && s[4] == 'L'){
				scanf("%s", s);
				if(s[0] == 'L')
					L--;
				else R--;
			}
			else if(s[0] == 'M' && s[4] == 'R'){
				scanf("%s", s);
				if(s[0] == 'L')
					L++;
				else R++;
			}
			else if(s[0] == 'I'){
				int x;
				scanf("%s%d", s, &x);
				if(s[0] == 'L')
					Insert(L, x), R++;
				else 
					Insert(R + 1, x), R++;
				//output(root), puts("");	
				//printf("%d %d\n", L, R);
			}
			else if(s[0] == 'D'){
				scanf("%s", s);
				if(s[0] == 'L')
					Delete(L), R--;
				else 
					Delete(R), R--;
				//output(root), puts("");	
				//printf("%d %d\n", L, R);
			}
			else{
				splay(Kth(L), 0);
				splay(Kth(R+2), root);
				int x = ch[ch[root][1]][0];
				down[x] ^= 1;
			}
		}
		ok = 0;
		output(root);
		puts("");
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值