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;
    }
    
  • 相关阅读:
    springboot: 使web项目支持jsp
    springboot: 集成freemark模板引擎
    Springboot的优点和实现
    Spring的两种动态代理:Jdk和Cglib 的区别和实现
    JAVA单例模式:懒汉式,饿汉式
    java的内存管理 对象的分配与释放
    JAVA反射调用方法
    JAVA内置注解 基本注解
    图——图的定义与操作
    树——二叉树的线索化
  • 原文地址:https://www.cnblogs.com/s1124yy/p/6840761.html
Copyright © 2011-2022 走看看