From a28fec3bca1b0f4fd44df93f966daec11c26ac51 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 13 Jul 2022 13:38:33 +0800 Subject: [PATCH] =?UTF-8?q?1861.=20[ZJOI2006]Book=20=E4=B9=A6=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://hydro.ac/d/bzoj/record/62ce59cb740191919aed44ec --- BZOJ/1861/1861.cpp | 215 ++++++++++++++++++++++++++++++++++++++++++ BZOJ/1861/data/1.in | 3 + BZOJ/1861/data/1.out | 3 + BZOJ/1861/data/10.in | 3 + BZOJ/1861/data/10.out | 3 + BZOJ/1861/data/2.in | 3 + BZOJ/1861/data/2.out | 3 + BZOJ/1861/data/3.in | 3 + BZOJ/1861/data/3.out | 3 + BZOJ/1861/data/4.in | 3 + BZOJ/1861/data/4.out | 3 + BZOJ/1861/data/5.in | 3 + BZOJ/1861/data/5.out | 3 + BZOJ/1861/data/6.in | 3 + BZOJ/1861/data/6.out | 3 + BZOJ/1861/data/7.in | 3 + BZOJ/1861/data/7.out | 3 + BZOJ/1861/data/8.in | 3 + BZOJ/1861/data/8.out | 3 + BZOJ/1861/data/9.in | 3 + BZOJ/1861/data/9.out | 3 + 21 files changed, 275 insertions(+) create mode 100644 BZOJ/1861/1861.cpp create mode 100644 BZOJ/1861/data/1.in create mode 100644 BZOJ/1861/data/1.out create mode 100644 BZOJ/1861/data/10.in create mode 100644 BZOJ/1861/data/10.out create mode 100644 BZOJ/1861/data/2.in create mode 100644 BZOJ/1861/data/2.out create mode 100644 BZOJ/1861/data/3.in create mode 100644 BZOJ/1861/data/3.out create mode 100644 BZOJ/1861/data/4.in create mode 100644 BZOJ/1861/data/4.out create mode 100644 BZOJ/1861/data/5.in create mode 100644 BZOJ/1861/data/5.out create mode 100644 BZOJ/1861/data/6.in create mode 100644 BZOJ/1861/data/6.out create mode 100644 BZOJ/1861/data/7.in create mode 100644 BZOJ/1861/data/7.out create mode 100644 BZOJ/1861/data/8.in create mode 100644 BZOJ/1861/data/8.out create mode 100644 BZOJ/1861/data/9.in create mode 100644 BZOJ/1861/data/9.out diff --git a/BZOJ/1861/1861.cpp b/BZOJ/1861/1861.cpp new file mode 100644 index 00000000..cd075f8c --- /dev/null +++ b/BZOJ/1861/1861.cpp @@ -0,0 +1,215 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 8e4 + 5; + +struct node { + node *lchild, *rchild, *fa; + std::size_t size; + int value, key; + + node() + : lchild(nullptr), rchild(nullptr), fa(nullptr), size(0), value(0), key(rand()) {} + + node(int _value) + : lchild(nullptr), rchild(nullptr), fa(nullptr), size(1), value(_value), key(rand()) {} + + ~node() { + if (lchild != nullptr) delete lchild; + if (rchild != nullptr) delete rchild; + } + + inline std::size_t lsize() { + return lchild == nullptr ? 0 : lchild->size; + } + + inline std::size_t rsize() { + return rchild == nullptr ? 0 : rchild->size; + } + + inline void pushup() { + size = 1; + + if (lchild != nullptr) { + size += lchild->size; + lchild->fa = this; + } + + if (rchild != nullptr) { + size += rchild->size; + rchild->fa = this; + } + } + + inline std::size_t pos() { + std::size_t ret = lsize() + 1; + node *cur = this; + + while (cur->fa != nullptr) { + if (cur->fa->rchild == cur) { + ret += cur->fa->lsize() + 1; + } + + cur = cur->fa; + } + + return ret; + } +}; + +int n, m; +node *root, *p[N]; + +std::pair split(node *u, int k) { + if (u == nullptr) return std::make_pair(nullptr, nullptr); + + if (k <= u->lsize()) { + auto o = split(u->lchild, k); + + u->lchild = o.second; + u->pushup(); + o.second = u; + + return o; + } + + auto o = split(u->rchild, k - u->lsize() - 1); + + u->rchild = o.first; + u->pushup(); + o.first = u; + + return o; +} + +template +node *merge(node *x, Args... args) { + return merge(x, merge(args...)); +} + +template <> +node *merge(node *x, node *y) { + if (x == nullptr) return y; + if (y == nullptr) return x; + + if (x->key < y->key) { + x->rchild = merge(x->rchild, y); + x->pushup(); + + return x; + } + + y->lchild = merge(x, y->lchild); + y->pushup(); + + return y; +} + +inline void top(const int &x) { + int k = p[x]->pos(); + + auto p = split(root, k - 1); + auto q = split(p.second, 1); + + root = merge(q.first, p.first, q.second); +} + +inline void bottom(const int &x) { + int k = p[x]->pos(); + + auto p = split(root, k - 1); + auto q = split(p.second, 1); + + root = merge(p.first, q.second, q.first); +} + +inline void insert(const int &x, const int &y) { + if (!y) return; + + int k = p[x]->pos(); + auto p = split(root, k - 1); + auto q = split(p.second, 1); + + if (y > 0) { + auto t = split(q.second, y); + root = merge(p.first, t.first, q.first, t.second); + } else { // y < 0 + auto t = split(p.first, k + y - 1); + root = merge(t.first, q.first, t.second, q.second); + } +} + +inline int ask(const int &x) { + return p[x]->pos() - 1; +} + +inline int query(const int &x) { + auto p = split(root, x - 1); + auto q = split(p.second, 1); + + int res = q.first->value; + + root = merge(p.first, q.first, q.second); + + return res; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1, x; i <= n; i++) { + cin >> x; + + root = merge(root, p[x] = new node(x)); + } + + while (m--) { + std::string op; + int s, t; + + cin >> op >> s; + + switch (op[0]) { + case 'T': { // op == "Top" + top(s); + + break; + } + case 'B': { // op == "Bottom" + bottom(s); + + break; + } + case 'I': { // op == "Insert" + cin >> t; + + insert(s, t); + + break; + } + case 'A': { // op == "Ask" + cout << ask(s) << endl; + + break; + } + case 'Q': { // op == "Query" + cout << query(s) << endl; + + break; + } + } + } + + // Cleanup + delete root; + + return 0; +} diff --git a/BZOJ/1861/data/1.in b/BZOJ/1861/data/1.in new file mode 100644 index 00000000..1c97c9c3 --- /dev/null +++ b/BZOJ/1861/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2074b29c2dd81b5d864241b321e2ceb75e84dc98fb636fe123f19c5b10041a72 +size 1279660 diff --git a/BZOJ/1861/data/1.out b/BZOJ/1861/data/1.out new file mode 100644 index 00000000..7b86da1c --- /dev/null +++ b/BZOJ/1861/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ede17ff35aaf7c0c3805be0394012c1e30400a1624bfe139a7094f7948b904fa +size 154779 diff --git a/BZOJ/1861/data/10.in b/BZOJ/1861/data/10.in new file mode 100644 index 00000000..c6fff163 --- /dev/null +++ b/BZOJ/1861/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a8ffb099f3abb8db1743637a39b5c7994456c0a1f03a3d557b993bbc825b4711 +size 15109 diff --git a/BZOJ/1861/data/10.out b/BZOJ/1861/data/10.out new file mode 100644 index 00000000..93b6502b --- /dev/null +++ b/BZOJ/1861/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b62d44bc7a656a4101ddbd35361efb31e3d6c4d5fea0e4188579075e4289d54f +size 2587 diff --git a/BZOJ/1861/data/2.in b/BZOJ/1861/data/2.in new file mode 100644 index 00000000..8bb83397 --- /dev/null +++ b/BZOJ/1861/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2e5a5024ada99ba4e8d6573ad09e1cd9d68b4f025628c4c8a4a99dfff399c1f1 +size 422972 diff --git a/BZOJ/1861/data/2.out b/BZOJ/1861/data/2.out new file mode 100644 index 00000000..a1b19021 --- /dev/null +++ b/BZOJ/1861/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6c648242ede6e898de2daa884622b7cf2ae75f37636b34a27c9140f0ce95c8c4 +size 56491 diff --git a/BZOJ/1861/data/3.in b/BZOJ/1861/data/3.in new file mode 100644 index 00000000..f84d3e61 --- /dev/null +++ b/BZOJ/1861/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:27b7dc55286e18da90d570823c7fbba913b3d0205e4cffff2d4bf23679551d0b +size 1466766 diff --git a/BZOJ/1861/data/3.out b/BZOJ/1861/data/3.out new file mode 100644 index 00000000..e8eb1ff4 --- /dev/null +++ b/BZOJ/1861/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d74bcd16fc7209b1fb3d791ecf490b387e0c019c74de8aa3b75cdf17aab205e1 +size 155172 diff --git a/BZOJ/1861/data/4.in b/BZOJ/1861/data/4.in new file mode 100644 index 00000000..fe5d6dd1 --- /dev/null +++ b/BZOJ/1861/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:860a71149a224e628cd6208aa9a248eff657b296b65e0a1967706268863cf066 +size 87 diff --git a/BZOJ/1861/data/4.out b/BZOJ/1861/data/4.out new file mode 100644 index 00000000..a64e07f7 --- /dev/null +++ b/BZOJ/1861/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5eb7586d47fe193d2f9e02659d0180042401880f93044b4089123304c9ae7c8e +size 8 diff --git a/BZOJ/1861/data/5.in b/BZOJ/1861/data/5.in new file mode 100644 index 00000000..fcda66a2 --- /dev/null +++ b/BZOJ/1861/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9ab7d955746cda0d8064adc38c0a533e65696e1d94c8b29922b88bf75ea4e755 +size 1126491 diff --git a/BZOJ/1861/data/5.out b/BZOJ/1861/data/5.out new file mode 100644 index 00000000..f5c91e6e --- /dev/null +++ b/BZOJ/1861/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cbd0a579d32d08f4bec4929eabdc51e9b8553ba7ada0a7e4ebea83a7b8922ba9 +size 60930 diff --git a/BZOJ/1861/data/6.in b/BZOJ/1861/data/6.in new file mode 100644 index 00000000..ad930956 --- /dev/null +++ b/BZOJ/1861/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:afeaaeb6ba15457f4ecb5f369d13c965d8c7c11a234b4dc9797aa1e267f2189b +size 1467779 diff --git a/BZOJ/1861/data/6.out b/BZOJ/1861/data/6.out new file mode 100644 index 00000000..fad439f6 --- /dev/null +++ b/BZOJ/1861/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7e8d86d21a05da910197cd2358d9c3b31030e5dd837cd3f562268f61bf0ffead +size 155634 diff --git a/BZOJ/1861/data/7.in b/BZOJ/1861/data/7.in new file mode 100644 index 00000000..a4126c68 --- /dev/null +++ b/BZOJ/1861/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3143fb5fa4c00817b5ba8ac5c9ef6ab21e397e1e4c667def97a6478bbb371976 +size 577 diff --git a/BZOJ/1861/data/7.out b/BZOJ/1861/data/7.out new file mode 100644 index 00000000..bd48dfe2 --- /dev/null +++ b/BZOJ/1861/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2fd3612b3b295473f99afaf81d1c7afc433a96b2719e9c26994fca95c146721d +size 73 diff --git a/BZOJ/1861/data/8.in b/BZOJ/1861/data/8.in new file mode 100644 index 00000000..dc87a4e1 --- /dev/null +++ b/BZOJ/1861/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b7160f2e8ed452e5ea1d391105b425e0c16a0c411b5cbfe048063255db655632 +size 1247268 diff --git a/BZOJ/1861/data/8.out b/BZOJ/1861/data/8.out new file mode 100644 index 00000000..b53bcf62 --- /dev/null +++ b/BZOJ/1861/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:493802d2b810863c41deee3636f85706715ee0d8609766202fb1c0b091e41fcb +size 231137 diff --git a/BZOJ/1861/data/9.in b/BZOJ/1861/data/9.in new file mode 100644 index 00000000..0ba46ada --- /dev/null +++ b/BZOJ/1861/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ba6a192cfaf7bdecb619f71f0790e9b8111a4060046dbd3592fe95a0669c4c45 +size 1467809 diff --git a/BZOJ/1861/data/9.out b/BZOJ/1861/data/9.out new file mode 100644 index 00000000..29238f4e --- /dev/null +++ b/BZOJ/1861/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:35ed10dae68ebe085d306cf819ef5e5e8d20d223733edb901d22f351f6ccbac3 +size 155359