lmori's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub lmorinn/library

:heavy_check_mark: verify/AizuOnlineJudge/data-structure/balanced-binary-search-tree/ITP2_7_B.test.cpp

Depends on

Code

#include "../../../../template/template.hpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/8/ITP2/7/ITP2_7_B"
#include "../../../../data-structure/balanced-binary-search-tree/RedBlackTree.hpp"
int main() {
    cin.tie(0)->sync_with_stdio(0);
    RedBlackTree t;
    int q;
    in(q);
    int siz = 0;
    rep(i, q) {
        int com, x;
        in(com, x);
        if (com == 0) {
            if (!t.contains(x)) {
                t.insert(x);
                siz++;
            }
            out(siz);
        } else if (com == 1) {
            out((t.contains(x) ? 1 : 0));
        } else {
            if (t.contains(x)) {
                t.erase(x);
                siz--;
            }
        }
    }
}
#line 2 "template/template.hpp"
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using ull = unsigned long long;
using ld = long double;
using int128 = __int128_t;
#define all(x) (x).begin(), (x).end()
#define uniqv(v) v.erase(unique(all(v)), v.end())
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define logfixed(x) cout << fixed << setprecision(10) << x << endl;

ostream &operator<<(ostream &dest, __int128_t value) {
  ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(ios_base::badbit);
    }
  }
  return dest;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != (int)v.size() ? " " : "");
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const unordered_map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pair_var) {
  os << pair_var.first << " " << pair_var.second;
  return os;
}

void out() { cout << '\n'; }
template <class T, class... Ts>
void out(const T &a, const Ts &...b) {
  cout << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

void outf() { cout << '\n'; }
template <class T, class... Ts>
void outf(const T &a, const Ts &...b) {
  cout << fixed << setprecision(14) << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}

inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
  cin >> first;
  in(rest...);
  return;
}

template <typename T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};
vector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<lint> dx4 = {1, 0, -1, 0};
vector<lint> dy4 = {0, 1, 0, -1};

#pragma endregion
#line 2 "verify/AizuOnlineJudge/data-structure/balanced-binary-search-tree/ITP2_7_B.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/8/ITP2/7/ITP2_7_B"
#line 1 "data-structure/balanced-binary-search-tree/RedBlackTree.hpp"
class RedBlackTree {
 private:
  const int RED = 0;
  const int BLACK = 1;
  struct Node {
    Node *p, *left, *right;
    int color, key;
  };

  Node *NIL;
  Node *root;

  void left_rotate(Node *x) {
    assert(x->right);
    Node *y = x->right;
    x->right = y->left;
    if (y->left != NIL) y->left->p = x;
    y->p = x->p;
    if (x->p == NIL) {
      root = y;
    } else if (x == x->p->left) {
      x->p->left = y;
    } else {
      x->p->right = y;
    }
    y->left = x;
    x->p = y;
  }

  void right_rotate(Node *y) {
    assert(y->left);
    Node *x = y->left;
    y->left = x->right;
    if (x->right != NIL) x->right->p = y;
    x->p = y->p;
    if (y->p == NIL) {
      root = x;
    } else if (y == y->p->left) {
      y->p->left = x;
    } else {
      y->p->right = x;
    }
    x->right = y;
    y->p = x;
  }

  void insert(Node *z) {
    Node *y = NIL;
    Node *x = root;
    while (x != NIL) {
      y = x;
      if (z->key < x->key) {
        x = x->left;
      } else {
        x = x->right;
      }
    }
    z->p = y;
    if (y == NIL) {
      root = z;
    } else if (z->key < y->key) {
      y->left = z;
    } else {
      y->right = z;
    }
    z->left = NIL;
    z->right = NIL;
    z->color = RED;
    insert_fixup(z);
  }

  void insert_fixup(Node *z) {
    while (z->p->color == RED) {
      if (z->p == z->p->p->left) {
        Node *y = z->p->p->right;
        if (y->color == RED) {
          z->p->color = BLACK;
          y->color = BLACK;
          z->p->p->color = RED;
          z = z->p->p;
        } else if (z == z->p->right) {
          z = z->p;
          left_rotate(z);
        } else if (z == z->p->left) {
          z->p->color = BLACK;
          z->p->p->color = RED;
          right_rotate(z->p->p);
        }
      } else if (z->p == z->p->p->right) {
        Node *y = z->p->p->left;
        if (y->color == RED) {
          z->p->color = BLACK;
          y->color = BLACK;
          z->p->p->color = RED;
          z = z->p->p;
        } else if (z == z->p->left) {
          z = z->p;
          right_rotate(z);
        } else if (z == z->p->right) {
          z->p->color = BLACK;
          z->p->p->color = RED;
          left_rotate(z->p->p);
        }
      }
    }
    root->color = BLACK;
  }

  void transplant(Node *u, Node *v) {
    if (u->p == NIL) {
      root = v;
    } else if (u == u->p->left) {
      u->p->left = v;
    } else {
      u->p->right = v;
    }
    v->p = u->p;
  }

  void erase(Node *z) {
    Node *x;
    Node *y = z;
    int y_original_color = y->color;
    if (z->left == NIL) {
      x = z->right;
      transplant(z, z->right);
    } else if (z->right == NIL) {
      x = z->left;
      transplant(z, z->left);
    } else {
      y = min_element(z->right);
      y_original_color = y->color;
      x = y->right;
      if (y->p == z) {
        x->p = y;
      } else {
        transplant(y, y->right);
        y->right = z->right;
        y->right->p = y;
      }
      transplant(z, y);
      y->left = z->left;
      y->left->p = y;
      y->color = z->color;
    }

    if (y_original_color == BLACK) {
      erase_fixup(x);
    }
    delete z;
  }

  void erase_fixup(Node *x) {
    while (x != root and x->color == BLACK) {
      if (x == x->p->left) {
        Node *w = x->p->right;
        if (w->color == RED) {
          w->color = BLACK;
          x->p->color = RED;
          left_rotate(x->p);
          w = w->p->right;
        } else if (w->left->color == BLACK and w->right->color == BLACK) {
          w->color = RED;
          x = x->p;
        } else if (w->right->color == BLACK) {
          w->left->color = BLACK;
          w->color = RED;
          right_rotate(w);
          w = x->p->right;
        } else {
          w->color = x->p->color;
          x->p->color = BLACK;
          w->right->color = BLACK;
          left_rotate(x->p);
          x = root;
        }
      } else {
        Node *w = x->p->left;
        if (w->color == RED) {
          w->color = BLACK;
          x->p->color = RED;
          right_rotate(x->p);
          w = w->p->left;
        } else if (w->left->color == BLACK and w->right->color == BLACK) {
          w->color = RED;
          x = x->p;
        } else if (w->left->color == BLACK) {
          w->right->color = BLACK;
          w->color = RED;
          left_rotate(w);
          w = x->p->left;
        } else {
          w->color = x->p->color;
          x->p->color = BLACK;
          w->left->color = BLACK;
          right_rotate(x->p);
          x = root;
        }
      }
    }
    x->color = BLACK;
  }

  Node *min_element(Node *cur) {
    while (cur->left != NIL) cur = cur->left;
    return cur;
  }

  Node *max_element(Node *cur) {
    while (cur->right != NIL) cur = cur->right;
    return cur;
  }

  Node *contains(Node *cur, int key) {
    while (cur != NIL and cur->key != key) {
      if (key < cur->key) {
        cur = cur->left;
      } else {
        cur = cur->right;
      }
    }
    return cur;
  }

  Node *lower_bound(Node *cur, int key) {
    Node *res = NIL;
    while (cur != NIL) {
      if (cur->key >= key) {
        res = cur;
        cur = cur->left;
      } else {
        cur = cur->right;
      }
    }
    return res;
  }

 public:
  RedBlackTree() {
    NIL = new Node();
    NIL->color = BLACK;
    NIL->key = 0;
    NIL->p = NIL->left = NIL->right = NIL;
    root = NIL;
  }

  void insert(int value) {
    Node *z = new Node();
    z->color = RED;
    z->key = value;
    insert(z);
  }

  void erase(int value) {
    Node *z = contains(root, value);
    if (z != NIL) erase(z);
  }

  bool contains(int value) {
    Node *z = contains(root, value);
    return z != NIL;
  }

  int min_element() {
    Node *z = min_element(root);
    return z->key;
  }

  int max_element() {
    Node *z = max_element(root);
    return z->key;
  }

  int lower_bound(int value) {
    Node *z = lower_bound(root, value);
    if (z == NIL) return -1;
    return z->key;
  }

  void debug(Node *cur = nullptr) {
    if (cur == nullptr) cur = root;
    if (cur == NIL) return;
    cout << "key: " << cur->key << ", color: " << cur->color << ", P:" << (cur->p == NIL ? "NIL" : to_string(cur->p->key)) << ", L:" << (cur->left == NIL ? "NIL" : to_string(cur->left->key)) << ' ' << ", R:" << (cur->right == NIL ? "NIL" : to_string(cur->right->key)) << endl;
    debug(cur->left);
    debug(cur->right);
  }
};
#line 4 "verify/AizuOnlineJudge/data-structure/balanced-binary-search-tree/ITP2_7_B.test.cpp"
int main() {
    cin.tie(0)->sync_with_stdio(0);
    RedBlackTree t;
    int q;
    in(q);
    int siz = 0;
    rep(i, q) {
        int com, x;
        in(com, x);
        if (com == 0) {
            if (!t.contains(x)) {
                t.insert(x);
                siz++;
            }
            out(siz);
        } else if (com == 1) {
            out((t.contains(x) ? 1 : 0));
        } else {
            if (t.contains(x)) {
                t.erase(x);
                siz--;
            }
        }
    }
}
Back to top page