POJ 3580 SuperMemo (Splay tree)

转载请注明出处,谢谢 http://blog.csdn.net/ACM_cxlove?viewmode=contents           by---cxlove

也是一个很BT的数据结构题。

操作比较多。其中特有的一个操作是REVOLVE右移操作。将区间[a,b]右移c位

首先c可能比较多,可以先对区间长度取模。

在右移之后,可以发现[a,b]被分为两个区间[a,b-c]  [b-c+1,b],将后者插入到前者之前即可。

就可以化为INSERT 与DELETE 操作,内存吃紧,使用手动内存池,动态SPLAY可能时间上会多点,不太习惯写。

#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;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值