HDU 4286 Data Handler(12年天津 Splay tree)

转载请注明出处,谢谢http://blog.csdn.net/acm_cxlove/article/details/7854526       by---cxlove

题目:有一串数字,两个指针,然后一些添加,删除,反转,以及移动操作,最后 输出序列

http://acm.hdu.edu.cn/showproblem.php?pid=4286 

比赛的时候最后半小时才发现这题。

一看到题目就觉得是Splay tree。因为有一个反转操作,想都没想,就开始写了,不过最后只剩半小时,勉强只能过了样例。

其实这题的插入和删除都只是一个数字,Splay是很不沾优的,双向链表模拟一下更加方便,也比较好写。

不过对于我这种傻X就只能坚持撸Splay了。

各种悲剧,一开始是RE。因为Splay并不是严格的平衡树,极有可能退化,深度很大,在递归的时候,可能会出现堆栈溢出,一开始都是G++交的,没注意,苦逼调试很久。

之后便是一直TLE,不断得改变姿势,其实我的Splay效率还是可以的。哭瞎

最后也不知道是怎么就过了,加上外挂排在第一

#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<algorithm>
#define N 1000005
#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))
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int n,q,a[N],pl,pr;
int size[N],pre[N],rev[N],sum[5],val[N];
int ch[N][2],tot1,root,s[N],tot2;
inline 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;
    size[r]=1;
    rev[r]=0;
    val[r]=k;
}
inline void Push_Up(int x){
    int l=ch[x][0],r=ch[x][1];
    size[x]=size[l]+size[r]+1;
}
inline void Push_Down(int r){
    if(rev[r]){
        rev[ch[r][0]]^= 1;  
        rev[ch[r][1]]^= 1;  
        swap(ch[r][0],ch[r][1]);  
        rev[r]=0;  
    }
}
inline 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]=0;
    NewNode(root,-100,0);
    NewNode(ch[root][1],-1,root);
    Push_Up(root);
    Bulid(Key_value,1,n,ch[root][1]);
    Push_Up(ch[root][1]);
    Push_Up(root);
}
inline 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);
}
inline 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;
}
inline void RotateTo(int k, int goal) {  
    int x=root;  
    Push_Down(x);  
    while(k!=size[ch[x][0]]+1){  
        if (k<=size[ch[x][0]]){  
            x=ch[x][0];  
        }else{  
            k-=(size[ch[x][0]]+1);  
            x=ch[x][1];  
        }  
        Push_Down(x);  
    }  
    Splay(x,goal);  
}  
void Print();
inline void Reversal(int pos,int k){
    RotateTo(pos,0);
    RotateTo(pos+k+1,root);
    rev[Key_value]^=1;
}
inline void Insert(int pos,int k,int num){
    RotateTo(pos,0);
    RotateTo(pos+1,root);
    NewNode(Key_value,num,ch[root][1]);
    Push_Up(ch[root][1]);
    Push_Up(root);
}
inline void Delete(int pos,int k){
    RotateTo(pos,0);
    RotateTo(pos+k+1,root);
    //删除节点,并更新
    Key_value=0;
    Push_Up(ch[root][1]);
}
int cnt,ans[N];
//外挂忽略
inline void scanf_(int &num){
    char in;
    bool neg=false;
    while(((in=getchar()) > '9' || in<'0') && in!='-') ;
    if(in=='-'){
        neg=true;
        while((in=getchar()) >'9' || in<'0');
    }
    num=in-'0';
    while(in=getchar(),in>='0'&&in<='9')
        num*=10,num+=in-'0';
    if(neg)
        num=0-num;
}
inline void printf_(int num){
    bool flag=false;
    if(num<0){
        putchar('-');
        num=-num;
    }
    int ans[10],top=0;
    while(num!=0){
        ans[top++]=num%10;
        num/=10;
    }
    if(top==0)
        putchar('0');
    for(int i=top-1;i>=0;i--){
        char ch=ans[i]+'0';
        putchar(ch);
    }
}
void InOrder(int r){
    if(r==0)
        return;
    Push_Down(r);
    InOrder(ch[r][0]);
    if(cnt) putchar(' ');
    printf_(val[r]);
    cnt++;
    InOrder(ch[r][1]);
}
void Print(){
    //旋转后,便于递归输出,避免输出头尾冗余点
    RotateTo(1,0);
    RotateTo(n+2,root);
    cnt=0;
    InOrder(Key_value);
    putchar('\n');
}
int main(){
    int t;
    scanf_(t);
    while(t--){
        scanf_(n);
        for(int i=1;i<=n;i++)
            scanf_(a[i]);
        Init();
        char str[20];
        int k=1,pl,r1;
        scanf_(pl);scanf_(pr);scanf_(q);
        while(q--){
            scanf("%s",str);
            if(str[0]=='M'&&str[4]=='R'){
                scanf("%s",str);
                if(str[0]=='L') pl++;
                else pr++;

            }
            else if(str[0]=='M'&&str[4]=='L'){
                scanf("%s",str);
                if(str[0]=='L') pl--;
                else pr--;
            }
            else if(str[0]=='I'){
                scanf("%s",str);
                scanf_(a[1]);
                if(str[0]=='L') {Insert(pl,k,a[1]);}
                else {Insert(pr+1,k,a[1]);}
                //插入的话,总字符数量增加1个,而且右边的指针后移
                n++;pr++;
            }
            else if(str[0]=='D'){
                scanf("%s",&str);
                if(str[0]=='L') {Delete(pl,k);}
                else {Delete(pr,k);}
                //删除的话,总字符减1,右边的指针前移一个
                n--;pr--;
            }
            else if(str[0]=='R')
                Reversal(pl,pr-pl+1);
        }
        Print();
    }
    return 0;
}


  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
根据提供的引用内容,这是一道关于二叉树的问题,需要输出每个二叉树的层序遍历。如果二叉树没有完全给出,则输出“not complete”。而且,这道题目是一个ACM竞赛的题目,需要使用Java语言进行编写。 以下是Java语言的代码实现: ```java import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); if (s.equals("")) { continue; } String[] arr = s.split(" "); int len = arr.length; int[] tree = new int[len]; boolean[] flag = new boolean[len]; for (int i = 0; i < len; i++) { if (arr[i].equals("()")) { tree[i] = -1; flag[i] = true; } else { tree[i] = Integer.parseInt(arr[i].substring(1, arr[i].length() - 1)); } } int root = 0; for (int i = 0; i < len; i++) { if (!flag[i]) { root = i; break; } } boolean isComplete = true; Queue<Integer> queue = new LinkedList<>(); queue.offer(root); int index = 1; while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { int cur = queue.poll(); if (tree[cur] == -1) { if (index < len && !flag[index]) { isComplete = false; } } else { if (cur * 2 + 1 < len) { queue.offer(cur * 2 + 1); if (tree[cur * 2 + 1] != -1) { flag[cur * 2 + 1] = true; } } if (cur * 2 + 2 < len) { queue.offer(cur * 2 + 2); if (tree[cur * 2 + 2] != -1) { flag[cur * 2 + 2] = true; } } } index++; } } if (!isComplete) { System.out.println("not complete"); continue; } queue.offer(root); StringBuilder sb = new StringBuilder(); while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { int cur = queue.poll(); sb.append(tree[cur]).append(" "); if (cur * 2 + 1 < len && tree[cur * 2 + 1] != -1) { queue.offer(cur * 2 + 1); } if (cur * 2 + 2 < len && tree[cur * 2 + 2] != -1) { queue.offer(cur * 2 + 2); } } } System.out.println(sb.toString().trim()); } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值