Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <cmath>
- #include <stack>
- #include <iomanip>
- #include <fstream>
- #include <string>
- #include <set>
- #include <deque>
- #include <queue>
- #include <map>
- #include <bitset>
- #include <random>
- #include <list>
- #include <unordered_map>
- #include <unordered_set>
- #include <cassert>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double ld;
- typedef string str;
- //typedef __int128 ultraint;
- #define endl "\n"
- #define sqrt sqrtl
- #define all(vc666) vc666.begin(), vc666.end()
- //#define pow fast_pow
- const ll inf = (ll)1e18 + 7;
- ld eps = 1e-6;
- ld Pi = 3.1415926535897932384;
- const ll mod = 1e9 + 7;
- const ll max_sz = 1e3;
- struct Node {
- ll key, pr;
- ll left = -1, right = -1;
- ll sz = 1;
- };
- struct dd {
- //Djinchuriki Djubi
- Node nodes[max_sz];
- ll nodesNumber = 0;
- ll kor = -1;
- ll makeNewNode(ll x) {
- nodes[nodesNumber] = { x,rand(), -1,-1,1 };
- return nodesNumber++;
- }
- ll getSZ(ll v) { return (v >= 0) ? nodes[v].sz : 0; }
- void recalculate(ll v) {
- nodes[v].sz = getSZ(nodes[v].left) + getSZ(nodes[v].right) + 1;
- }
- bool exists(ll root, ll x) {
- ll v = root;
- while (v != -1) {
- Node& nd = nodes[v];
- if (nd.key == x)
- return true;
- else if (nd.key > x)
- v = nodes[v].left;
- else
- v = nodes[v].right;
- }
- return false;
- }
- pair <ll, ll> split(ll root, ll x) {
- if (root == -1)
- return { -1,-1 };
- if (x > nodes[root].key) {
- pair <ll, ll> p = split(nodes[root].right, x);
- nodes[root].right = p.first;
- recalculate(root);
- return { root,p.second };
- }
- else {
- pair <ll, ll> p = split(nodes[root].left, x);
- nodes[root].left = p.second;
- recalculate(root);
- return { p.first, root };
- }
- }
- ll merge(ll root1, ll root2) {
- if (root2 == -1) {
- return root1;
- }
- if (root1 == -1) {
- return root2;
- }
- if (nodes[root1].pr < nodes[root2].pr) {
- nodes[root1].right = merge(nodes[root1].right, root2);
- recalculate(root1);
- return root1;
- }
- else {
- nodes[root2].left = merge(root1, nodes[root2].left);
- recalculate(root2);
- return root2;
- }
- }
- ll insert(ll root, ll x) {
- ll newNode = makeNewNode(x);
- pair <ll, ll> p = split(root, x);
- return merge(p.first, merge(p.second, newNode));
- }
- ll erase(ll root, ll x) {
- pair <ll, ll> p = split(root, x);
- pair <ll, ll> pp = split(p.first, x - 1);
- return merge(pp.first, p.second);
- }
- ll fndKth(ll root, ll k) {
- if (getSZ(nodes[root].left) >= k) {
- return fndKth(nodes[root].left, k);
- }
- else if (getSZ(nodes[root].left) == k - 1) {
- return nodes[root].key;
- }
- else {
- return fndKth(nodes[root].right, k - getSZ(nodes[root].left) - 1);
- }
- }
- };
- signed main() {
- #ifdef _DEBUG
- freopen("in.txt", "r", stdin);
- freopen("out.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(0);
- cin.tie(NULL);
- cout.tie(NULL);
- ll t = 1;
- //cin >> t;
- while (t--) {
- string s;
- dd tree;
- ll x;
- while (getline(cin, s)) {
- if (s.size() != 0) {
- ll x = atoi(s.substr(s.find(' ') + 1, s.size() - s.find(' ') - 1).c_str());
- if (s[0] == 'i') {
- if (!tree.exists(tree.kor, x)) {
- tree.kor = tree.insert(tree.kor, x);
- }
- //insert
- }
- else if (s[0] == 'd') {
- tree.erase(tree.kor, x);
- //erase
- }
- else {
- if (tree.exists(tree.kor, x)) {
- cout << "true" << endl;
- }
- else {
- cout << "false" << endl;
- }
- //exists
- }
- }
- }
- }
- }
- //痛みを受け入れる。 痛みを知っています。 痛みを感じる。 痛みを参照してください。 真の痛みを知らなければ、真の世界を理解することは不可能です。 今、あなたは痛みを知るでしょう。 神の罰!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement