zoukankan      html  css  js  c++  java
  • uva 11922

    splay的题;

    学习白书上和网上的代码敲的;

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <cstdlib>
      4 #include <algorithm>
      5 using namespace std;
      6 int n,m;
      7 struct node
      8 {
      9     node *ch[2];
     10     int s,v;
     11     int flip;
     12     node(int v):v(v)
     13     {
     14         ch[1]=ch[0]=NULL;
     15         s=1;
     16         flip=0;
     17     }
     18     void maintain()
     19     {
     20         s=1;
     21         if(ch[0]!=NULL)s+=ch[0]->s;
     22         if(ch[1]!=NULL)s+=ch[1]->s;
     23     }
     24     void pushdown()
     25     {
     26         if(flip)
     27         {
     28             flip=0;
     29             swap(ch[0],ch[1]);
     30             if(ch[0]!=NULL)ch[0]->flip=!ch[0]->flip;
     31             if(ch[1]!=NULL)ch[1]->flip=!ch[1]->flip;
     32         }
     33     }
     34     int cmp(int x)const
     35     {
     36         int t=(ch[0]==NULL)?0:ch[0]->s;
     37         if(t>=x)return 0;
     38         if(t+1==x)return -1;
     39         return 1;
     40     }
     41 };
     42 void rotate(node* &root,int d)
     43 {
     44     node *k=root->ch[d^1];
     45     root->ch[d^1]=k->ch[d];
     46     k->ch[d]=root;
     47     root=k;
     48     root->ch[d]->maintain();
     49     root->maintain();
     50 }
     51 
     52 void build(node* &root,int l,int r)
     53 {
     54     int mid=(l+r)>>1;
     55     root=new node(mid);
     56     if(l<mid)build(root->ch[0],l,mid-1);
     57     if(r>mid)build(root->ch[1],mid+1,r);
     58     root->maintain();
     59 }
     60 
     61 void splay(node* &root,int k)
     62 {
     63     root->pushdown();
     64     int d=root->cmp(k);
     65     if(d==1)
     66     {
     67         if(root->ch[0]!=NULL)
     68             k-=root->ch[0]->s;
     69         k--;
     70     }
     71     if(d!=-1)
     72     {
     73         node *p=root->ch[d];
     74         p->pushdown();
     75         int d2=p->cmp(k);
     76         int k2=k;
     77         if(d2==1)
     78         {
     79             if(p->ch[0]!=NULL)
     80                 k2-=p->ch[0]->s;
     81             k2--;
     82         }
     83         if(d2!=-1)
     84         {
     85             splay(p->ch[d2],k2);
     86             if(d==d2) rotate(root,d^1);
     87             else rotate(root->ch[d],d);
     88         }
     89         rotate(root,d^1);
     90     }
     91     return;
     92 }
     93 
     94 void split(node *root,int k,node* &left,node* &right)
     95 {
     96     splay(root,k);
     97     left=root;
     98     right=root->ch[1];
     99     root->ch[1]=NULL;
    100     left->maintain();//左边的要维护,右边不需要;
    101 }
    102 
    103 node *merge(node *left,node *right)
    104 {
    105     splay(left,left->s);//
    106     left->ch[1]=right;
    107     left->maintain();
    108     return left;
    109 }
    110 
    111 void dfs(node *root)
    112 {
    113     root->pushdown();
    114     if(root->ch[0])dfs(root->ch[0]);
    115     if(root->v&&root->v!=n+1)printf("%d
    ",root->v);
    116     if(root->ch[1])dfs(root->ch[1]);
    117 }
    118 
    119 void del(node *root)
    120 {
    121     if(root->ch[0])del(root->ch[0]);
    122     if(root->ch[1])del(root->ch[1]);
    123     delete root;
    124 }
    125 
    126 
    127 node *root;
    128 int main()
    129 {
    130     int a,b;
    131     while(scanf("%d%d",&n,&m)!=EOF)
    132     {
    133         root=NULL;
    134         build(root,0,n+1);
    135         while(m--)
    136         {
    137             scanf("%d%d",&a,&b);
    138             node *right,*mid,*left,*tmp,*o;
    139             split(root,a,left,o);
    140             split(o,b-a+1,mid,right);
    141             if(right->s>1)
    142             {
    143                 split(right,right->s-1,tmp,o);
    144                 mid->flip^=1;
    145                 root=merge(left,merge(merge(tmp,mid),o));
    146             }
    147             else
    148             {
    149                 mid->flip^=1;
    150                 root=merge(left,merge(mid,right));
    151             }
    152         }
    153         dfs(root);
    154         del(root);
    155     }
    156     return 0;
    157 }
    View Code
  • 相关阅读:
    海量数据中,寻找最小的k个数。
    快速排序
    反转一个单链表,分别以迭代和递归的形式来实现
    N个大小不等的自然数排序,时间复杂度为O(n),空间复杂度为O(1)
    堆排序
    两个已经排好序的链表合并为一个有序链表
    字符串过滤空格、回车、tab
    求一个浮点数的连续子序列最大乘积 (2013 小米校园招聘笔试题)
    单向循环链表队列,从头开始报数,当报到m或者m的倍数的元素出列
    给一个数组,元素都是整数(有正数也有负数),寻找连续的元素相加之和为最大的序列。
  • 原文地址:https://www.cnblogs.com/yours1103/p/3402554.html
Copyright © 2011-2022 走看看