From 7579397dc7896fcc36213808b08dc1f93d795c04 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 15 Sep 2022 17:00:51 +0800 Subject: [PATCH] =?UTF-8?q?3506.=20[Cqoi2014]=E6=8E=92=E5=BA=8F=E6=9C=BA?= =?UTF-8?q?=E6=A2=B0=E8=87=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://hydro.ac/d/bzoj/record/6322e9bc32ae6c3198cc971e --- BZOJ/3506/3506.cpp | 183 ++++++++++++++++++++++++++++++++++++++++++ BZOJ/3506/data/1.in | 3 + BZOJ/3506/data/1.out | 3 + BZOJ/3506/data/10.in | 3 + BZOJ/3506/data/10.out | 3 + BZOJ/3506/data/2.in | 3 + BZOJ/3506/data/2.out | 3 + BZOJ/3506/data/3.in | 3 + BZOJ/3506/data/3.out | 3 + BZOJ/3506/data/4.in | 3 + BZOJ/3506/data/4.out | 3 + BZOJ/3506/data/5.in | 3 + BZOJ/3506/data/5.out | 3 + BZOJ/3506/data/6.in | 3 + BZOJ/3506/data/6.out | 3 + BZOJ/3506/data/7.in | 3 + BZOJ/3506/data/7.out | 3 + BZOJ/3506/data/8.in | 3 + BZOJ/3506/data/8.out | 3 + BZOJ/3506/data/9.in | 3 + BZOJ/3506/data/9.out | 3 + 21 files changed, 243 insertions(+) create mode 100644 BZOJ/3506/3506.cpp create mode 100644 BZOJ/3506/data/1.in create mode 100644 BZOJ/3506/data/1.out create mode 100644 BZOJ/3506/data/10.in create mode 100644 BZOJ/3506/data/10.out create mode 100644 BZOJ/3506/data/2.in create mode 100644 BZOJ/3506/data/2.out create mode 100644 BZOJ/3506/data/3.in create mode 100644 BZOJ/3506/data/3.out create mode 100644 BZOJ/3506/data/4.in create mode 100644 BZOJ/3506/data/4.out create mode 100644 BZOJ/3506/data/5.in create mode 100644 BZOJ/3506/data/5.out create mode 100644 BZOJ/3506/data/6.in create mode 100644 BZOJ/3506/data/6.out create mode 100644 BZOJ/3506/data/7.in create mode 100644 BZOJ/3506/data/7.out create mode 100644 BZOJ/3506/data/8.in create mode 100644 BZOJ/3506/data/8.out create mode 100644 BZOJ/3506/data/9.in create mode 100644 BZOJ/3506/data/9.out diff --git a/BZOJ/3506/3506.cpp b/BZOJ/3506/3506.cpp new file mode 100644 index 00000000..d71502d1 --- /dev/null +++ b/BZOJ/3506/3506.cpp @@ -0,0 +1,183 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +struct node { + node *lchild, *rchild; + size_t size; + unsigned key; + int value, min; + bool reversed; + + node() + : lchild(nullptr), + rchild(nullptr), + size(0), + key(rand()), + value(0), + min(std::numeric_limits::max()), + reversed(false) {} + + node(int _value) + : lchild(nullptr), + rchild(nullptr), + size(1), + key(rand()), + value(_value), + min(_value), + reversed(false) {} + + ~node() { + if (lchild != nullptr) delete lchild; + if (rchild != nullptr) delete rchild; + } + + inline size_t lsize() { + return lchild == nullptr ? 0 : lchild->size; + } + + inline size_t rsize() { + return rchild == nullptr ? 0 : rchild->size; + } + + inline void pushup() { + size = lsize() + 1 + rsize(); + min = value; + + if (lchild != nullptr) { + min = std::min(min, lchild->min); + } + + if (rchild != nullptr) { + min = std::min(min, rchild->min); + } + } + + inline void pushdown() { + if (reversed) { + std::swap(lchild, rchild); + if (lchild != nullptr) lchild->reversed = !lchild->reversed; + if (rchild != nullptr) rchild->reversed = !rchild->reversed; + reversed = false; + } + } +}; + +int n, b[N]; +std::pair a[N]; +node *root; + +std::pair split(node *u, int k) { + if (u == nullptr) return std::make_pair(nullptr, nullptr); + + u->pushdown(); + + 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; +} + +node *merge(node *x, node *y) { + if (x == nullptr) return y; + if (y == nullptr) return x; + + if (x->key < y->key) { + x->pushdown(); + x->rchild = merge(x->rchild, y); + x->pushup(); + + return x; + } + + y->pushdown(); + y->lchild = merge(x, y->lchild); + y->pushup(); + + return y; +} + +void reverse(int k) { + auto x = split(root, k); + auto y = split(x.first, k - 1); + if (y.first != nullptr) y.first->reversed = !y.first->reversed; + delete y.second; + root = merge(y.first, x.second); +} + +int find(node *p) { + int k = 1; + + while (p != nullptr) { + p->pushdown(); + + if (p->lchild != nullptr && p->min == p->lchild->min) { + p = p->lchild; + } else if (p->rchild != nullptr && p->min == p->rchild->min) { + k += p->lsize() + 1; + p = p->rchild; + } else { + return k + p->lsize(); + } + } + + return -1; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> a[i].first; + + a[i].second = i; + } + + std::sort(a + 1, a + 1 + n); + + for (int i = 1; i <= n; i++) { + b[a[i].second] = i; + } + + for (int i = 1; i <= n; i++) { + root = merge(root, new node(b[i])); + } + + for (int i = 1; i <= n; i++) { + int k = find(root); + + reverse(k); + + cout << k + i - 1 << ' '; + } + + cout << endl; + + // Cleanup + delete root; + + return 0; +} diff --git a/BZOJ/3506/data/1.in b/BZOJ/3506/data/1.in new file mode 100644 index 00000000..475d9023 --- /dev/null +++ b/BZOJ/3506/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3874b1063389916356f5b3b78d86ec9111501d11e5c9da17ed1647e07f03bde1 +size 3892 diff --git a/BZOJ/3506/data/1.out b/BZOJ/3506/data/1.out new file mode 100644 index 00000000..951bc8d2 --- /dev/null +++ b/BZOJ/3506/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b882e71a7ec7596357acc1853b2553ceb0e8c65de762423b5d55c7d0a9a82911 +size 3999 diff --git a/BZOJ/3506/data/10.in b/BZOJ/3506/data/10.in new file mode 100644 index 00000000..c6b226d8 --- /dev/null +++ b/BZOJ/3506/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1b285f76e80eb54b63d330a389ce611a8f349424469a911bf875a5113d7eb6cb +size 488854 diff --git a/BZOJ/3506/data/10.out b/BZOJ/3506/data/10.out new file mode 100644 index 00000000..63d255c3 --- /dev/null +++ b/BZOJ/3506/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:85b6f9b66385094636f478dbf4993b9545fed1ab11b996ffa4293c41cb6c118d +size 599417 diff --git a/BZOJ/3506/data/2.in b/BZOJ/3506/data/2.in new file mode 100644 index 00000000..b871a73e --- /dev/null +++ b/BZOJ/3506/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1ab8bfd128e4e4e0df50a449060ee4641d1df5270e0438654b9c9e5a932d54d7 +size 488842 diff --git a/BZOJ/3506/data/2.out b/BZOJ/3506/data/2.out new file mode 100644 index 00000000..f483b219 --- /dev/null +++ b/BZOJ/3506/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e11b6773250663d8187ad157bd1539eadb67f1cb7f3e4f8125ca8cac44893946 +size 599480 diff --git a/BZOJ/3506/data/3.in b/BZOJ/3506/data/3.in new file mode 100644 index 00000000..e9b7af98 --- /dev/null +++ b/BZOJ/3506/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0d309d5155e905687ac2919174f33fc53ffc6b770152e29808f4ae16ce60b1cc +size 788788 diff --git a/BZOJ/3506/data/3.out b/BZOJ/3506/data/3.out new file mode 100644 index 00000000..12433cce --- /dev/null +++ b/BZOJ/3506/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c08524a8101f3381ef6fffb50de0b15a1788a9d174654af8324b840482cd3bd0 +size 599488 diff --git a/BZOJ/3506/data/4.in b/BZOJ/3506/data/4.in new file mode 100644 index 00000000..a76100d8 --- /dev/null +++ b/BZOJ/3506/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:af3e71daf5b13711cd02d6b2ea8aab257563ed1120843d5e62c9d67f7b67eb3e +size 788715 diff --git a/BZOJ/3506/data/4.out b/BZOJ/3506/data/4.out new file mode 100644 index 00000000..d6d4da8f --- /dev/null +++ b/BZOJ/3506/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b9a8f27a55dc36653d755cc198aa474878f8525fbe85cbdc7eb01edc8029e908 +size 599479 diff --git a/BZOJ/3506/data/5.in b/BZOJ/3506/data/5.in new file mode 100644 index 00000000..d9cab351 --- /dev/null +++ b/BZOJ/3506/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fce122109c607e2aa581bfb57e75dbefb50594736bae661c7d6c99501b7f8226 +size 488855 diff --git a/BZOJ/3506/data/5.out b/BZOJ/3506/data/5.out new file mode 100644 index 00000000..4ee05595 --- /dev/null +++ b/BZOJ/3506/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:78b7f5c83bae3197e8662b7c209458a4879ceffdae594056ff1b73a198e8d3a2 +size 599444 diff --git a/BZOJ/3506/data/6.in b/BZOJ/3506/data/6.in new file mode 100644 index 00000000..f388c3cc --- /dev/null +++ b/BZOJ/3506/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a7426adc774438bce1cec7cb17f6cc957a6fa46d8cd81ec89f239ea19c376556 +size 788903 diff --git a/BZOJ/3506/data/6.out b/BZOJ/3506/data/6.out new file mode 100644 index 00000000..5e66a52b --- /dev/null +++ b/BZOJ/3506/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:96150fd5e20bf0b886d8bca4ae28f48942d7e7209209abbf027827b730391465 +size 599493 diff --git a/BZOJ/3506/data/7.in b/BZOJ/3506/data/7.in new file mode 100644 index 00000000..928f66a7 --- /dev/null +++ b/BZOJ/3506/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e8c8af04ea7284fcd85a02ba2ee9a44c9ca7380177f8508210cbe69544303ca7 +size 3904 diff --git a/BZOJ/3506/data/7.out b/BZOJ/3506/data/7.out new file mode 100644 index 00000000..7ece4b6b --- /dev/null +++ b/BZOJ/3506/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c6a104c2b293106775c7e77c453afae94cd2c39091d3e032344da55579d6a6c8 +size 4008 diff --git a/BZOJ/3506/data/8.in b/BZOJ/3506/data/8.in new file mode 100644 index 00000000..8270c958 --- /dev/null +++ b/BZOJ/3506/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f0a797bed8d30ea64d34b0c3eff6924fa484f0302bd4857a31f0cd94c3f60358 +size 488711 diff --git a/BZOJ/3506/data/8.out b/BZOJ/3506/data/8.out new file mode 100644 index 00000000..a5a652a2 --- /dev/null +++ b/BZOJ/3506/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:01dcdd72d5619830274cb76e2f901e074af1705982dae54ae9e61e18b3269131 +size 599474 diff --git a/BZOJ/3506/data/9.in b/BZOJ/3506/data/9.in new file mode 100644 index 00000000..6d0608a2 --- /dev/null +++ b/BZOJ/3506/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9c78b002d9a01f42fefd1198bb85657f0a7c303de4863041d9ac584948fd2be6 +size 5900 diff --git a/BZOJ/3506/data/9.out b/BZOJ/3506/data/9.out new file mode 100644 index 00000000..f7d2c8dc --- /dev/null +++ b/BZOJ/3506/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:763f36d5f38c28b0e74a1667e839f55f0ca6e7c198fa6a365fdca9df5881258d +size 3996