poj3580 splay

#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <string>
#include <set>
#include <stack>

using namespace std;
#define ll long long 
#define eps 1e-10
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sqr(x) ((x)*(x))
#define lson (u<<1)
#define rson (u<<1|1)
#define maxn 300100
#define keyTree ch[ch[root][1]][0]


struct SplayTree{

	int ch[maxn][2],pre[maxn];
	int val[maxn],mi[maxn],sz[maxn];
	int lz[maxn],num[maxn];
	bool re[maxn];
	int root,top;

	void swap(int &a,int &b){int t = a;a = b;b = t;}
	void add(int x,int v){
		val[x] += v;
		lz[x] += v;
		mi[x] += v;
	}
	void push_up(int x){
		mi[x] = min(val[x],min(mi[ch[x][0]],mi[ch[x][1]]));
		sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;

	}
	void push_down(int x){
		if(lz[x]){
			if(ch[x][0]) add(ch[x][0],lz[x]);
			if(ch[x][1]) add(ch[x][1],lz[x]);
			lz[x] = 0;
		}
		if(re[x]){
			re[ch[x][0]] = !re[ch[x][0]];
			re[ch[x][1]] = !re[ch[x][1]];
			swap(ch[x][0],ch[x][1]);
			re[x] = false;
		}
	}

	void rotate(int x,int f){
		int y = pre[x];
		push_down(y); push_down(x);
		ch[y][!f] = ch[x][f];
		pre[ ch[x][f] ] = y;
		pre[x] = pre[y];
		if(pre[x]) ch[ pre[y] ][ ch[pre[y]][1]==y ] = x;
		ch[x][f] = y;
		pre[y] = x;
		push_up(y);
	}



	void splay(int x,int goal){
		push_down(x);
		while(pre[x]!=goal){
			if(pre[ pre[x] ] == goal)
				rotate(x,ch[pre[x]][0]==x);
			else{
				int y = pre[x], z = pre[y];
				int f = (ch[z][0]==y);
				if(ch[y][f]==x) 
					rotate(x,!f),rotate(x,f);
				else
					rotate(y,f),rotate(x,f);
			}
		}
		push_up(x);
		if(goal==0) root = x;
	}

	void rotateTo(int k,int goal)
	{
		int x = root;
		push_down(x);
		while(sz[ ch[x][0] ] != k)
		{
			if(k < sz[ ch[x][0] ])x = ch[x][0];
			else
			{
				k -= (sz[ ch[x][0] ] + 1);
				x = ch[x][1];
			}
			push_down(x);
		}
		splay(x,goal);
	}
	void new_node(int &x,int key)
	{
		x = ++top;
		ch[x][0] = ch[x][1] = pre[x] = 0;
		sz[x] = 1;
		lz[x] = 0;
		re[x] = false;
		val[x] = mi[x] = key;
	}
	void makeTree(int &x,int l,int r,int f,int *p)
	{
		if(l > r)return;
		int m = (l + r) >> 1;
		new_node(x,p[m]);
		makeTree(ch[x][0],l,m - 1,x,p);
		makeTree(ch[x][1],m + 1,r,x,p);
		pre[x] = f;
		push_up(x);
	}
	void init(int n){
		ch[0][0] = ch[0][1] = pre[0] = sz[0] = 0;
		root = top = 0;
		re[0] = val[0] = lz[0] = 0; mi[0] = inf;
		new_node(root,inf);
		new_node(ch[root][1],inf);
		pre[ch[root][1]] = root;
		sz[root] = 2;
		for(int i=1;i<=n;i++) scanf("%d",num+i);
		makeTree(keyTree,1,n,ch[root][1],num);
		push_up(ch[root][1]);
		push_up(root);
	}

	void insert(int x,int pos){
		rotateTo(pos,0);rotateTo(pos+1,root);
		new_node(keyTree,x);
		pre[keyTree] = ch[root][1];
		push_up(ch[root][1]); push_up(root);
	}
	void update(int l,int r,int c){
		rotateTo(l-1,0); 
		rotateTo(r+1,root);
		add(keyTree,c);
		push_up(ch[root][1]); push_up(root);
	}
	void rev(int l,int r){
		rotateTo(l-1,0); 
		rotateTo(r+1,root);
		re[keyTree] = !re[keyTree];
	}
	void work(int l,int r,int num){
		num = (num%(r-l+1)+(r-l+1))%(r-l+1);
		if(!num) return;
		rotateTo(r-num,0); rotateTo(r+1,root);
		int tmp = keyTree;
		keyTree = 0;
		push_up(ch[root][1]); push_up(root);
		rotateTo(l-1,0); rotateTo(l,root);
		keyTree = tmp;
		pre[keyTree] = ch[root][1];
		push_up(ch[root][1]); push_up(root);

	}
	int query(int l,int r){
		rotateTo(l-1,0); rotateTo(r+1,root);
		return mi[keyTree];
	}
	void erace(int pos){
		rotateTo(pos-1,0); rotateTo(pos+1,root);
		keyTree = 0;
		push_up(ch[root][1]); push_up(root);
	}

}sp;
int n,m;

int main()
{

	int a,b,c;
	char op[10];
	scanf("%d",&n);
	sp.init(n);
	scanf("%d",&m);
	while(m--){
		scanf("%s",op);
		if(!strcmp(op,"ADD")){
			scanf("%d%d%d",&a,&b,&c);

			sp.update(a,b,c);
		}
		if(!strcmp(op,"REVERSE")){
			scanf("%d%d",&a,&b);
			sp.rev(a,b);
		}
		else if(!strcmp(op,"REVOLVE")){
			scanf("%d%d%d",&a,&b,&c);
			sp.work(a,b,c);
		}
		else if(!strcmp(op,"INSERT")){
			scanf("%d%d",&a,&b);
			sp.insert(b,a);
		}
		else if(!strcmp(op,"MIN")){
			scanf("%d%d",&a,&b);
			printf("%d\n",sp.query(a,b));
		}
		else if(!strcmp(op,"DELETE")){
			scanf("%d",&a);
			sp.erace(a);

		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值