From 9324d4aaf80da9c90129a97650730653af15ef0a Mon Sep 17 00:00:00 2001 From: Baoshuo Ren Date: Wed, 16 Feb 2022 22:09:57 +0800 Subject: [PATCH] =?UTF-8?q?#104.=20=E6=99=AE=E9=80=9A=E5=B9=B3=E8=A1=A1?= =?UTF-8?q?=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://loj.ac/s/1382920 --- LibreOJ/104/104.cpp | 176 ++++++++++++++++++++++++++++++++---- LibreOJ/104/data/input0.in | 3 + LibreOJ/104/data/input0.out | 3 + LibreOJ/104/data/input1.in | 3 + LibreOJ/104/data/input1.out | 3 + LibreOJ/104/data/input2.in | 3 + LibreOJ/104/data/input2.out | 3 + LibreOJ/104/data/input3.in | 3 + LibreOJ/104/data/input3.out | 3 + LibreOJ/104/data/input4.in | 3 + LibreOJ/104/data/input4.out | 3 + LibreOJ/104/data/input5.in | 3 + LibreOJ/104/data/input5.out | 3 + LibreOJ/104/data/input6.in | 3 + LibreOJ/104/data/input6.out | 3 + LibreOJ/104/data/input7.in | 3 + LibreOJ/104/data/input7.out | 3 + LibreOJ/104/data/input8.in | 3 + LibreOJ/104/data/input8.out | 3 + LibreOJ/104/data/input9.in | 3 + LibreOJ/104/data/input9.out | 3 + 21 files changed, 217 insertions(+), 19 deletions(-) create mode 100644 LibreOJ/104/data/input0.in create mode 100644 LibreOJ/104/data/input0.out create mode 100644 LibreOJ/104/data/input1.in create mode 100644 LibreOJ/104/data/input1.out create mode 100644 LibreOJ/104/data/input2.in create mode 100644 LibreOJ/104/data/input2.out create mode 100644 LibreOJ/104/data/input3.in create mode 100644 LibreOJ/104/data/input3.out create mode 100644 LibreOJ/104/data/input4.in create mode 100644 LibreOJ/104/data/input4.out create mode 100644 LibreOJ/104/data/input5.in create mode 100644 LibreOJ/104/data/input5.out create mode 100644 LibreOJ/104/data/input6.in create mode 100644 LibreOJ/104/data/input6.out create mode 100644 LibreOJ/104/data/input7.in create mode 100644 LibreOJ/104/data/input7.out create mode 100644 LibreOJ/104/data/input8.in create mode 100644 LibreOJ/104/data/input8.out create mode 100644 LibreOJ/104/data/input9.in create mode 100644 LibreOJ/104/data/input9.out diff --git a/LibreOJ/104/104.cpp b/LibreOJ/104/104.cpp index 8f161d01..252276bd 100644 --- a/LibreOJ/104/104.cpp +++ b/LibreOJ/104/104.cpp @@ -1,26 +1,164 @@ -#include +#include +#include -using namespace std; +using std::cin; +using std::cout; +#define endl '\n' + +const int N = 1e5 + 5; + +class Treap { + private: + struct node { + node *left, *right; + int size, val, key; + + node() + : left(nullptr), right(nullptr), size(1), val(0), key(rand()) {} + node(int _val) + : left(nullptr), right(nullptr), size(1), val(_val), key(rand()) {} + + ~node() { + delete left, right; + } + + inline void pushup() { + this->size = 1; + if (this->left != nullptr) this->size += this->left->size; + if (this->right != nullptr) this->size += this->right->size; + } + } *root = nullptr; + + int getNodeSize(node *); + node *find(node *, int); + std::pair split(node *, int); + std::pair splitByValue(node *, int); + node *merge(node *, node *); + int _getRank(node *, int); + + public: + Treap() + : root(nullptr) {} + + void insert(int); + void erase(int); + int getRank(int); + int getKth(int); +} tree; + +inline int Treap::getNodeSize(Treap::node *node) { + return node == nullptr ? 0 : node->size; +} + +std::pair Treap::split(Treap::node *p, int k) { + if (p == nullptr) return std::make_pair(nullptr, nullptr); + std::pair o; + if (k <= this->getNodeSize(p->left)) { + o = this->split(p->left, k); + p->left = o.second; + p->pushup(); + o.second = p; + } else { + o = this->split(p->right, k - this->getNodeSize(p->left) - 1); + p->right = o.first; + p->pushup(); + o.first = p; + } + return o; +} + +std::pair Treap::splitByValue(Treap::node *p, int val) { + if (p == nullptr) return std::make_pair(nullptr, nullptr); + std::pair o; + if (p->val < val) { + o = this->splitByValue(p->right, val); + p->right = o.first; + p->pushup(); + o.first = p; + } else { + o = this->splitByValue(p->left, val); + p->left = o.second; + p->pushup(); + o.second = p; + } + return o; +} + +Treap::node *Treap::merge(Treap::node *x, Treap::node *y) { + if (x == nullptr) return y; + if (y == nullptr) return x; + if (x->val > y->val) std::swap(x, y); + if (x->key > y->key) { + x->right = this->merge(x->right, y); + x->pushup(); + return x; + } + y->left = this->merge(x, y->left); + y->pushup(); + return y; +} + +Treap::node *Treap::find(Treap::node *p, int val) { + if (p == nullptr) return nullptr; + if (p->val == val) return p; + if (p->val > val) return this->find(p->left, val); + return this->find(p->right, val); +} + +void Treap::insert(int val) { + auto o = this->splitByValue(this->root, val); + o.first = this->merge(o.first, new Treap::node(val)); + this->root = this->merge(o.first, o.second); +} + +void Treap::erase(int val) { + auto o = this->splitByValue(this->root, val); + auto t = o; + if (this->find(o.second, val) != nullptr) { + t = this->split(o.second, 1); + delete t.first; + } + this->root = this->merge(o.first, t.second); +} + +int Treap::_getRank(Treap::node *p, int val) { + if (p == nullptr) return 1; + if (val <= p->val) return this->_getRank(p->left, val); + return this->getNodeSize(p->left) + 1 + this->_getRank(p->right, val); +} + +inline int Treap::getRank(int val) { + return this->_getRank(this->root, val); +} + +int Treap::getKth(int k) { + auto x = this->split(this->root, k - 1); + auto y = this->split(x.second, 1); + Treap::node *o = y.first; + this->root = this->merge(x.first, this->merge(y.first, y.second)); + return o == nullptr ? -1 : o->val; +} + +int n, op, x; int main() { - int n, x, opt; - vector a; + std::ios::sync_with_stdio(false); cin >> n; - for (int i = 0; i < n; i++) { - cin >> opt >> x; - if (opt == 1) { - a.insert(lower_bound(a.begin(), a.end(), x), x); - } else if (opt == 2) { - a.erase(lower_bound(a.begin(), a.end(), x)); - } else if (opt == 3) { - cout << lower_bound(a.begin(), a.end(), x) - a.begin() + 1 << endl; - } else if (opt == 4) { - cout << a[x - 1] << endl; - } else if (opt == 5) { - cout << *(lower_bound(a.begin(), a.end(), x) - 1) << endl; - } else if (opt == 6) { - cout << *upper_bound(a.begin(), a.end(), x) << endl; + while (n--) { + cin >> op >> x; + if (op == 1) { + tree.insert(x); + } else if (op == 2) { + tree.erase(x); + } else if (op == 3) { + cout << tree.getRank(x) << endl; + } else if (op == 4) { + cout << tree.getKth(x) << endl; + } else if (op == 5) { + cout << tree.getKth(tree.getRank(x) - 1) << endl; + } else { + cout << tree.getKth(tree.getRank(x + 1) /* + 1*/) << endl; } } return 0; -} \ No newline at end of file +} diff --git a/LibreOJ/104/data/input0.in b/LibreOJ/104/data/input0.in new file mode 100644 index 00000000..491b7f38 --- /dev/null +++ b/LibreOJ/104/data/input0.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d4c29ee404830673e7f315266b99ec4b7899ca8aa53d68c825a5008d927dc037 +size 85 diff --git a/LibreOJ/104/data/input0.out b/LibreOJ/104/data/input0.out new file mode 100644 index 00000000..a7acf6ce --- /dev/null +++ b/LibreOJ/104/data/input0.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3260692cc92706e436fff46e3161694d64d023e7d35cb4fc4c6a4d674203f951 +size 20 diff --git a/LibreOJ/104/data/input1.in b/LibreOJ/104/data/input1.in new file mode 100644 index 00000000..f24224d7 --- /dev/null +++ b/LibreOJ/104/data/input1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c65fa18a4db5fab4a5a9b266f34e8f83b76daeae082d30ef09c605649e546d4e +size 174 diff --git a/LibreOJ/104/data/input1.out b/LibreOJ/104/data/input1.out new file mode 100644 index 00000000..bd977828 --- /dev/null +++ b/LibreOJ/104/data/input1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:521e1535888bc661b78a5600b512694be897b4417d52136e9bc9e5153acfef62 +size 43 diff --git a/LibreOJ/104/data/input2.in b/LibreOJ/104/data/input2.in new file mode 100644 index 00000000..9593dbae --- /dev/null +++ b/LibreOJ/104/data/input2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0aae52735a2d785d57890873c67bfeb6150a368e1a8294f34d9bdbdc109e0e64 +size 427 diff --git a/LibreOJ/104/data/input2.out b/LibreOJ/104/data/input2.out new file mode 100644 index 00000000..98407f8f --- /dev/null +++ b/LibreOJ/104/data/input2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a448af4d59c6f482d708e662dfb2169ef5b0157f25bdcee0858e390f2f11c47d +size 98 diff --git a/LibreOJ/104/data/input3.in b/LibreOJ/104/data/input3.in new file mode 100644 index 00000000..9b6545cf --- /dev/null +++ b/LibreOJ/104/data/input3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d326919b4eeab5a901dcb36bfe990cb5fe794aa772da322d3317642fdbdf8f21 +size 853 diff --git a/LibreOJ/104/data/input3.out b/LibreOJ/104/data/input3.out new file mode 100644 index 00000000..0f4d4b49 --- /dev/null +++ b/LibreOJ/104/data/input3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fd8766e9b4ebf44a9d90a3f385bcd175cb49e2610fc91f6e9618cd36759610d4 +size 230 diff --git a/LibreOJ/104/data/input4.in b/LibreOJ/104/data/input4.in new file mode 100644 index 00000000..c6788fce --- /dev/null +++ b/LibreOJ/104/data/input4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0f3d6ba05e8217d65e6757076a9e08f735098134abeb10702d90c24c332397b6 +size 8554 diff --git a/LibreOJ/104/data/input4.out b/LibreOJ/104/data/input4.out new file mode 100644 index 00000000..ec9250fe --- /dev/null +++ b/LibreOJ/104/data/input4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dba179917b6fdce896a1641cab27adadc5d7bfd687157321cc40ae667d1d978f +size 2542 diff --git a/LibreOJ/104/data/input5.in b/LibreOJ/104/data/input5.in new file mode 100644 index 00000000..8787544c --- /dev/null +++ b/LibreOJ/104/data/input5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:147f2246f2e582dc3cc3815e913d3befed38a928837823eeaa0d35197f221d5a +size 86429 diff --git a/LibreOJ/104/data/input5.out b/LibreOJ/104/data/input5.out new file mode 100644 index 00000000..85f72d07 --- /dev/null +++ b/LibreOJ/104/data/input5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:98b0824e026366d4f843166299eb2a20b0d6a20066f4392b11fe30194e0a2aef +size 24489 diff --git a/LibreOJ/104/data/input6.in b/LibreOJ/104/data/input6.in new file mode 100644 index 00000000..dcf33751 --- /dev/null +++ b/LibreOJ/104/data/input6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:10268ab64b85c5f4c56e7d737295b467d623b01df9bd2e8cc1d0de45c0b3496a +size 434504 diff --git a/LibreOJ/104/data/input6.out b/LibreOJ/104/data/input6.out new file mode 100644 index 00000000..94669030 --- /dev/null +++ b/LibreOJ/104/data/input6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:15594fca68f47b0f38f30134477b1dd705414709c0e4b7882fe328f8fab6a257 +size 127452 diff --git a/LibreOJ/104/data/input7.in b/LibreOJ/104/data/input7.in new file mode 100644 index 00000000..b971cd1c --- /dev/null +++ b/LibreOJ/104/data/input7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:537da906ad44d9b901fc6ac2bb0949090e6654731082d431389c0be93d6d18b6 +size 872115 diff --git a/LibreOJ/104/data/input7.out b/LibreOJ/104/data/input7.out new file mode 100644 index 00000000..23b5d5cd --- /dev/null +++ b/LibreOJ/104/data/input7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9c15bcf6c4aeea4879c1da7b2fbeb819f7a94405a3e39381b31d42608752f177 +size 256586 diff --git a/LibreOJ/104/data/input8.in b/LibreOJ/104/data/input8.in new file mode 100644 index 00000000..52406491 --- /dev/null +++ b/LibreOJ/104/data/input8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:da4b1c68a9b2986fc189e739defc65c30ce544b00b95e0d91716298f8782218f +size 872508 diff --git a/LibreOJ/104/data/input8.out b/LibreOJ/104/data/input8.out new file mode 100644 index 00000000..96d793aa --- /dev/null +++ b/LibreOJ/104/data/input8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:579c6dfb9289542566fffaff5673591f60ddc22d0ae56bebbea5e3da181559bc +size 257833 diff --git a/LibreOJ/104/data/input9.in b/LibreOJ/104/data/input9.in new file mode 100644 index 00000000..dba729aa --- /dev/null +++ b/LibreOJ/104/data/input9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:21abaf30343b7ed8d9bce141b58838634b2096b311601617cbfe448740df786a +size 872259 diff --git a/LibreOJ/104/data/input9.out b/LibreOJ/104/data/input9.out new file mode 100644 index 00000000..fdbebeb6 --- /dev/null +++ b/LibreOJ/104/data/input9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a7cf9c09bcfea939402f748b52773a2d9cf2da7aad16566b543f8f5b9d89f54d +size 256129