zoukankan      html  css  js  c++  java
  • Treap仿set 模板

    Treap仿set 模板

    蓝书232

    &代码:

    #include <cstdio>
    #include <bitset>
    #include <iostream>
    #include <set>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <vector>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define ll long long
    const int maxn = 1e3 + 7;
    struct Node {
       Node *ch[2];
       int v, r;
       bool operator <(const Node& A) const {
          return r < A.r;
       }
       int cmp(int x) {
          if(v == x) return -1;
          return x < v ? 0 : 1;
       }
    };
    void rotate(Node *&o, int d) {
       Node *k = o->ch[d ^ 1];
       o->ch[d ^ 1] = k->ch[d];
       k->ch[d] = o;
       o = k;
    }
    void insert(Node *&o, int x) {
       if(o == NULL) {
          o = new Node();
          o->ch[0] = o->ch[1] = NULL;
          o->v = x;
          o->r = rand();
       }
       else {
          int d = o->cmp(x);
          insert(o->ch[d], x);
          if(o->ch[d] > o) {
             rotate(o, d ^ 1);
          }
       }
    }
    void remove(Node *&o, int x) {
       int d = o->cmp(x);
       if(d == -1) {
          if(o->ch[0] == NULL) o = o->ch[1];
          else if(o->ch[1] == NULL) o = o->ch[0];
          else {
             int d2 = o->ch[0] > o->ch[1] ? 1 : 0;
             rotate(o, d2);
             remove(o->ch[d2], x);
          }
       }
       else {
          remove(o->ch[d], x);
       }
    }
    
    int find(Node *o, int x) {
       while(o != NULL) {
          int d = o->cmp(x);
          if(d == -1) return 1;
          o = o->ch[d];
       }
       return 0;
    }
    
    
    int main() {
       freopen("E:1.in", "r", stdin);
       Node *s = NULL;
       for(int i = 1; i < 11; i++) {
          if(!find(s, i))
             insert(s, i);
       }
       for(int i = 1; i < 11; i++) {
          if(!find(s, i))
             insert(s, i);
       }
       for(int i = 0; i < 31; i++) {
          printf("%d
    ", find(s, i));
       }
       return 0;
    }
    
  • 相关阅读:
    UBUntu 软件 源配置方法
    oracle 11G direct path read 非常美也非常伤人
    iOS多线程编程(四)------ GCD(Grand Central Dispatch)
    Yii之路(第八)
    UVA
    我喜欢的网易云音乐
    LeetCode_DP_Word Break II
    管理Java垃圾回收的五个建议
    const成员函数总结
    算法之经典排序算法小归纳
  • 原文地址:https://www.cnblogs.com/s1124yy/p/6840761.html
Copyright © 2011-2022 走看看