zoukankan      html  css  js  c++  java
  • codevs 3303

    还没写过splay树上的lazy标记。写得还算顺利,不过自己出数据错了浪费了好多时间QAQ

    在debug过程中深刻体会到了gdb的好处orz

     1 #include<bits/stdc++.h>
     2 #define inc(i,l,r) for(i=l;i<=r;i++)
     3 #define dec(i,l,r) for(i=l;i>=r;i--)
     4 #define mem(a) memset(a,0,sizeof(a))
     5 #define NM 100000+10
     6 #define inf 1e9
     7 using namespace std;
     8 int n,m,i,x,y;
     9 struct splautree{
    10     int root,c[NM][2],f[NM],a[NM],tot,z[NM],b[NM];
    11     void newnode(int &r,int k,int fa){
    12         a[r=++tot]=k;f[r]=fa;c[r][0]=c[r][1]=0;
    13     }
    14     void rot(int x,int kind){
    15         int y=f[x];
    16         c[y][!kind]=c[x][kind];
    17         f[c[x][kind]]=y;
    18         c[x][kind]=y;
    19         f[x]=f[y];
    20         if(f[x])c[f[y]][c[f[y]][1]==y]=x;
    21         f[y]=x;
    22         b[y]=b[c[y][0]]+b[c[y][1]]+1;
    23         b[x]=b[c[x][0]]+b[c[x][1]]+1;
    24     }
    25     void splay(int x,int goal){
    26         while(f[x]!=goal){
    27             if(f[f[x]]==goal)rot(x,c[f[x]][0]==x);
    28             else{
    29                 int y=f[x],kind=c[f[y]][0]==y;
    30                 if(c[y][kind]==x){
    31                     rot(x,!kind);
    32                     rot(x,kind);
    33                 }else{
    34                     rot(y,kind);
    35                     rot(x,kind);
    36                 }
    37             }
    38         }
    39         if(goal==0)root=x;
    40     }
    41     void build(){
    42         int i;
    43         newnode(root,0,0);b[1]=n+2;
    44         int r=root;
    45         inc(i,1,n){
    46             newnode(c[r][1],i,r);
    47             r=c[r][1];
    48             b[r]=n+2-i;
    49         }
    50         newnode(c[r][1],0,r);r=c[r][1];b[r]=1;
    51     }
    52     void pushdown(int x){
    53         if(z[x]%2){
    54             swap(c[x][1],c[x][0]);
    55             z[c[x][1]]^=1;z[c[x][0]]^=1;
    56             z[x]=0;
    57         }
    58     }
    59     int find(int x){
    60         int r=root;x++;
    61         while(x>0){
    62             pushdown(r);
    63             if(x==b[c[r][0]]+1)return r;
    64             else
    65             if(x>b[c[r][0]]+1){
    66                 x-=b[c[r][0]]+1;
    67                 r=c[r][1];
    68             }else r=c[r][0];
    69         }
    70     }
    71     void flip(int x,int y){
    72         int t;
    73         t=find(x-1);
    74         splay(t,0);
    75         int v=find(y+1);
    76         splay(v,t);
    77         t=c[v][0];
    78         z[t]^=1;
    79         pushdown(t);
    80     }
    81     void output(int x){
    82         pushdown(x);
    83         if(c[x][0])output(c[x][0]);
    84         if(a[x])printf("%d ",a[x]);
    85         if(c[x][1])output(c[x][1]);
    86     }
    87 }tree;
    88 int main(){
    89     scanf("%d%d",&n,&m);
    90     tree.build();
    91     inc(i,1,m){
    92         scanf("%d%d",&x,&y);
    93         tree.flip(x,y);
    94     }
    95     tree.output(tree.root);
    96     return 0;
    97 }
    View Code
  • 相关阅读:
    PCB 规则引擎之脚本语言JavaScript应用评测
    PCB 挺有意思的基数排序----C#代码实现
    PCB NOSQL MongoDb MI流程指示数据存储结构
    PCB javascript解析Gerber274X格式实现方法
    PCB javascript解析钻孔(Excellon)格式实现方法
    PCB 围绕CAM自动化,打造PCB规则引擎
    PCB Genesis拼SET画工艺边 实现方法(一)
    约瑟夫环的三种解法
    Microsoft edge真香!
    商城规格参数
  • 原文地址:https://www.cnblogs.com/onlyRP/p/4725991.html
Copyright © 2011-2022 走看看