POJ3580

#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<algorithm>
#define N 200005
#define inf 1<<29
#define MOD 100000007
#define LL long long
#define Key_value ch[ch[root][1]][0]
#define _match(a,b) ((a)==(b))
using namespace std;
int n,q,a[N];
int size[N],pre[N],rev[N],val[N],same[N],add[N],m[N];
int ch[N][2],tot1,root,s[N],tot2;
//debug部分copy from hh  
void Treaval(int x) {  
    if(x) {  
        Treaval(ch[x][0]);  
        printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,val = %2d  min =%2d \n",x,ch[x][0],ch[x][1],pre[x],size[x],val[x],m[x]);  
        Treaval(ch[x][1]);  
    }  
}  
void debug() {printf("%d\n",root);Treaval(root);}  
//以上Debug  
void Update_Rev(int r){
	if(!r) return;
	swap(ch[r][0],ch[r][1]);
	rev[r]^=1;
}
void Update_Add(int r,int ADD){
	if(!r) return;
	m[r]+=ADD;
	val[r]+=ADD;
	add[r]+=ADD;
}
void NewNode(int &r,int k,int father){
    if(tot2)
        r=s[tot2--];
    else
        r=++tot1;
    ch[r][0]=ch[r][1]=0;
    pre[r]=father;
	val[r]=m[r]=k;
	size[r]=1;
	add[r]=0;
}
void Push_Up(int x){
    int l=ch[x][0],r=ch[x][1];
    size[x]=size[l]+size[r]+1;
    m[x]=min(min(m[l],m[r]),val[x]);
}
void Push_Down(int x){
	int l=ch[x][0],r=ch[x][1];
    if(add[x]){
       Update_Add(l,add[x]);
	   Update_Add(r,add[x]);
	   add[x]=0;
    }
	if(rev[x]){
		Update_Rev(l);
		Update_Rev(r);
		rev[x]=0;
	}
}
void Bulid(int &r,int L,int R,int father){
    if(L>R)
        return ;
    int mid=(L+R)/2;
    NewNode(r,a[mid],father);
    Bulid(ch[r][0],L,mid-1,r);
    Bulid(ch[r][1],mid+1,R,r);
    Push_Up(r);
}
void Init(){
    tot1=tot2=root=0;
    ch[root][0]=ch[root][1]=pre[root]=rev[root]=size[root]=add[root]=0;
	m[root]=inf;
    NewNode(root,inf,0);
    NewNode(ch[root][1],inf,root);
    Push_Up(root);
    Bulid(Key_value,1,n,ch[root][1]);
    Push_Up(ch[root][1]);
    Push_Up(root);
}
void Rotate(int x,int kind){  
    int y=pre[x];    
    Push_Down(y);
    Push_Down(x);
    ch[y][!kind]=ch[x][kind];   
    pre[ch[x][kind]]=y;  
    if(pre[y])  
        ch[pre[y]][ch[pre[y]][1]==y]=x;  
    pre[x]=pre[y];  
    ch[x][kind]=y;  
    pre[y]=x;  
    Push_Up(y);  
}   
void Splay(int r,int goal){  
    Push_Down(r);
    while(pre[r]!=goal){  
        if(pre[pre[r]]==goal)  
            Rotate(r,ch[pre[r]][0]==r);  
        else{  
            int y=pre[r];  
            int kind=(ch[pre[y]][0]==y);  
            if(ch[y][kind]==r){  
                Rotate(r,!kind);  
                Rotate(r,kind);  
            }  
            else{  
                Rotate(y,kind);  
                Rotate(r,kind);  
            }  
        }  
    }  
    Push_Up(r);  
    if(goal==0) root=r;  
} 
void RotateTo(int k,int goal) {  
    int r=root;  
    Push_Down(r);  
    while(size[ch[r][0]]!=k){  
        if(k<size[ch[r][0]]){  
            r=ch[r][0];  
        } else {  
            k-=(size[ch[r][0]]+1);  
            r=ch[r][1];  
        }  
        Push_Down(r);  
    }  
    Splay(r,goal);  
}  
int Get_Kth(int r,int k){
    Push_Down(r);
    int t=size[ch[r][0]]+1;
    if(t==k)
        return r;
    if(t>k)
        return Get_Kth(ch[r][0],k);
    else
        return Get_Kth(ch[r][1],k-t);
}
int Get_Min(int r){
    Push_Down(r);
    while(ch[r][0]){
        r=ch[r][0];
        Push_Down(r);
    }
    return r;
}
int Get_Max(int r){
    Push_Down(r);
    while(ch[r][1]){
        r=ch[r][1];
        Push_Down(r);
    }
    return r;
}
void Reversal(int l,int r){
    int x=Get_Kth(root,l);
	Splay(x,0);
	int y=Get_Kth(root,r+2);
	Splay(y,root);
    Update_Rev(Key_value);
}
void Cut(int a,int b,int c){
    int x=Get_Kth(root,a);
    int y=Get_Kth(root,b+2);    
    Splay(x,0);
    Splay(y,root);
    int tmp=Key_value;
    Key_value=0;
    Push_Up(ch[root][1]);
    Push_Up(root);
    int z=Get_Kth(root,c+1);
    Splay(z,0);
    int m=Get_Min(ch[root][1]);
    Splay(m,root);
    Key_value=tmp;
    pre[Key_value]=ch[root][1];
    Push_Up(ch[root][1]);
    Push_Up(root);
}
int cnt;
void InOrder(int r){
    if(r==0)
        return;
    Push_Down(r);
    InOrder(ch[r][0]);
  if(cnt>=1)
      printf("%d  ",val[r]);
    cnt++;
    InOrder(ch[r][1]);
}
void Insert(int pos,int k){
    int x=Get_Kth(root,pos);
    Splay(x,0);
    int y=Get_Min(ch[root][1]);
    Splay(y,root);
	NewNode(Key_value,k,ch[root][1]);
    Push_Up(ch[root][1]);
    Push_Up(root);
}
void erase(int r){
    if(!r) return;
    s[++tot2]=r;
    erase(ch[r][0]);
    erase(ch[r][1]);
}
void Delete(int l){
    int x=Get_Kth(root,l);
    Splay(x,0); 
    int y=Get_Kth(root,l+2);
    Splay(y,root);
    erase(Key_value);
    pre[Key_value]=0;
    Key_value=0;
    Push_Up(ch[root][1]);
    Push_Up(root);
}
void ADD(int l,int r,int k){
	int x=Get_Kth(root,l);
	Splay(x,0);
	int y=Get_Kth(root,r+2);
	Splay(y,root);
	Update_Add(Key_value,k);
	Push_Up(ch[root][1]);
	Push_Up(root);
}
int Get_Answer(int l,int r){
	int x=Get_Kth(root,l);
	Splay(x,0);
	int y=Get_Kth(root,r+2);
	Splay(y,root);
	return m[Key_value];
}
void Revolve(int a,int b,int t){
	if(!t)
		return;
	int c=b-t;
	int x=Get_Kth(root,a);
	Splay(x,0);
	x=Get_Kth(root,c+2);
	Splay(x,root);
       //将后者取出
	int tmp=Key_value;
	Key_value=0;
	Push_Up(ch[root][1]);
	Push_Up(root);
	Splay(Get_Kth(root,b-c+a),0);
	Splay(Get_Kth(root,b-c+a+1),root);
	//旋转到相应位置
       Key_value=tmp;
	pre[Key_value]=ch[root][1];
       //插入
	Push_Up(ch[root][1]);
	Push_Up(root);
}
int main(){
    while(scanf("%d",&n)!=EOF){
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		scanf("%d",&q);
		Init();
		char str[10];
		int l,r,k;
		while(q--){
			scanf("%s",str);
			if(!strcmp(str,"ADD")){
				scanf("%d%d%d",&l,&r,&k);
				ADD(l,r,k);
			}
			else if(!strcmp(str,"REVERSE")){
				scanf("%d%d",&l,&r);
				Reversal(l,r);
			}
			else if(!strcmp(str,"REVOLVE")){
				scanf("%d%d%d",&l,&r,&k);
				Revolve(l,r,(k%(r-l+1)+(r-l+1))%(r-l+1));
			}
			else if(!strcmp(str,"INSERT")){
				scanf("%d%d",&l,&k);
				Insert(l+1,k);
			}
			else if(!strcmp(str,"DELETE")){
				scanf("%d",&l);
				Delete(l);
			}
			else{
				scanf("%d%d",&l,&r);
				printf("%d\n",Get_Answer(l,r));
			}
		}
	}
	return 0;
}



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 200010
#define inf 0x3f3f3f3f
#define LL long long int
#define Key_value ch[ch[root][1]][0]
int n,q,a[maxn];

inline int min(int a,int b)
{
	return a>b?b:a;
}

struct SplayTree
{
	int pre[maxn],key[maxn],ch[maxn][2],m[maxn],s[maxn],size[maxn],add[maxn],flip[maxn],root,cnt1,cnt2;

	void init()
	{
		cnt1 = cnt2 = root = 0;
		ch[0][0] = ch[0][1] = pre[0] = flip[0] = size[0] = add[0] = 0;
		m[0] = inf;
		NewNode(root,inf,0);
		NewNode(ch[root][1],inf,root);
		PushUp(root);
		BuildTree(Key_value,1,n,ch[root][1]);
		PushUp(ch[root][1]);
		PushUp(root);
	}

	void NewNode(int & r,int k,int father)
	{
		if(cnt2)
			r = s[cnt2--];
		else r = ++cnt1;
		ch[r][0] = ch[r][1] = 0;
		pre[r] = father;
		key[r] = m[r] = k;
		size[r] = 1;
		add[r] = 0;
	}

	void Update_Rev(int r)
	{
		if(!r)	return;
		swap(ch[r][0],ch[r][1]);
		flip[r] ^= 1;
	}

	void Update_Add(int r,int Add)
	{
		if(!r)	return;
		m[r] += Add;
		key[r] += Add;
		add[r] += Add;
	}

	void PushUp(int x)
	{
		int l = ch[x][0],r = ch[x][1];
		size[x] = size[l] + size[r] + 1;
		m[x] = min(min(m[l],m[r]),key[x]);
	}

	void PushDown(int x)
	{
		int l = ch[x][0],r = ch[x][1];
		if(add[x])
		{
			Update_Add(l,add[x]);
			Update_Add(r,add[x]);
			add[x] = 0;
		}
		if(flip[x])
		{
			Update_Rev(l);
			Update_Rev(r);
			flip[x] = 0;
		}
	}

	void BuildTree(int & r,int L,int R,int father)
	{
		if(L > R)	return;
		int mid = (L + R) >> 1;
		NewNode(r,a[mid],father);
		BuildTree(ch[r][0],L,mid-1,r);
		BuildTree(ch[r][1],mid+1,R,r);
		PushUp(r);
	}

	void Rotate(int x,int kind)
	{
		int y = pre[x];
		PushDown(y);
		PushDown(x);
		ch[y][!kind] = ch[x][kind];
		pre[ch[x][kind]] = y;
		if(pre[y])
			ch[pre[y]][ch[pre[y]][1]==y] = x;
		pre[x] = pre[y];
		ch[x][kind] = y;
		pre[y] = x;
		PushUp(y);
	}

	void Splay(int r,int goal)
	{
		PushDown(r);
		while(pre[r] != goal)
		{
			if(pre[pre[r]] == goal)
				Rotate(r,ch[pre[r]][0]==r);
			else 
			{
				int y = pre[r];
				int kind = (ch[pre[y]][0] == y);
				if(ch[y][kind] == r)
				{
					Rotate(r,!kind);
					Rotate(r,kind);
				}
				else 
				{
					Rotate(y,kind);
					Rotate(r,kind);
				}
			}
		}
		PushUp(r);
		if(goal == 0)	root = r;
	}

	int Get_Kth(int r,int k)
	{
		PushDown(r);
		int t = size[ch[r][0]] + 1;
		if(t == k)
			return r;
		if(t > k)
			return Get_Kth(ch[r][0],k);
		else return Get_Kth(ch[r][1],k-t);
	}

	int Get_Min(int r)
	{
		PushDown(r);
		while(ch[r][0])
		{
			r = ch[r][0];
			PushDown(r);
		}
		return r;
	}
	
	int Get_Max(int r)
	{
		PushDown(r);
		while(ch[r][1])
		{
			r = ch[r][1];
			PushDown(r);
		}
		return r;
	}
	
	void Reversal(int l,int r)
	{
		int x = Get_Kth(root,l);
		Splay(x,0);
		int y = Get_Kth(root,r+2);
		Splay(y,root);
		Update_Rev(Key_value);
	}

	void Cut(int a,int b,int c)
	{
		int x = Get_Kth(root,a);
		int y = Get_Kth(root,b+2);
		Splay(x,0);
		Splay(y,root);
		int tmp = Key_value;
		Key_value = 0;
		PushUp(ch[root][1]);
		PushUp(root);
		int z = Get_Kth(root,c+1);
		Splay(z,0);
		int m = Get_Min(ch[root][1]);
		Splay(m,root);
		Key_value = tmp;
		pre[Key_value] = ch[root][1];
		PushUp(ch[root][1]);
		PushUp(root);
	}

	void Insert(int pos,int k)
	{
		int x = Get_Kth(root,pos);
		Splay(x,0);
		int y = Get_Min(ch[root][1]);
		Splay(y,root);
		NewNode(Key_value,k,ch[root][1]);
		PushUp(ch[root][1]);
		PushUp(root);
	}

	void erase(int r)
	{
		if(!r)	return;
		s[++cnt2] = r;
		erase(ch[r][0]);
		erase(ch[r][1]);
	}

	void Delete(int l)
	{
		int x = Get_Kth(root,l);
		Splay(x,0);
		int y = Get_Kth(root,l+2);
		Splay(y,root);
		erase(Key_value);
		pre[Key_value] = 0;
		Key_value = 0;
		PushUp(ch[root][1]);
		PushUp(root);
	}

	void Add(int l,int r,int k)
	{
		int x = Get_Kth(root,l);
		Splay(x,0);
		int y = Get_Kth(root,r+2);
		Splay(y,root);
		Update_Add(Key_value,k);
		PushUp(ch[root][1]);
		PushUp(root);
	}

	int Get_Answer(int l,int r)
	{
		int x = Get_Kth(root,l);
		Splay(x,0);
		int y = Get_Kth(root,r+2);
		Splay(y,root);
		return m[Key_value];
	}

	void Revolve(int a,int b,int t)
	{
		if(!t)	return;
		int c = b - t;
		int x = Get_Kth(root,a);
		Splay(x,0);
		x = Get_Kth(root,c+2);
		Splay(x,root);
		//将后者取出
		int tmp = Key_value;
		Key_value = 0;
		PushUp(ch[root][1]);
		PushUp(root);
		Splay(Get_Kth(root,b-c+a),0);
		Splay(Get_Kth(root,b-c+a+1),root);
		//旋转到相应位置
		Key_value = tmp;
		pre[Key_value] = ch[root][1];
		//插入
		PushUp(ch[root][1]);
		PushUp(root);
	}
	
	void solve()
	{
		for(int i = 1;i <= n;i++)
			scanf("%d",&a[i]);
		scanf("%d",&q);
		init();
		char str[10];
		int l,r,k;
		while(q--)
		{
			scanf("%s",str);
			if(!strcmp(str,"ADD"))
			{
				scanf("%d%d%d",&l,&r,&k);
				Add(l,r,k);
			}
			else if(!strcmp(str,"REVERSE"))
			{
				scanf("%d%d",&l,&r);
				Reversal(l,r);
			}
			else if(!strcmp(str,"REVOLVE"))
			{
				scanf("%d%d%d",&l,&r,&k);
				Revolve(l,r,(k%(r-l+1)+(r-l+1))%(r-l+1));
			}
			else if(!strcmp(str,"INSERT"))
			{
				scanf("%d%d",&l,&k);
				Insert(l+1,k);
			}
			else if(!strcmp(str,"DELETE"))
			{
				scanf("%d",&l);
				Delete(l);
			}
			else 
			{
				scanf("%d%d",&l,&r);
				printf("%d\n",Get_Answer(l,r));
			}
		}
	}
}spt;

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		spt.solve();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值