diff --git a/BZOJ/1552/1552.cpp b/BZOJ/1552/1552.cpp new file mode 100644 index 00000000..d71502d1 --- /dev/null +++ b/BZOJ/1552/1552.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/1552/data/1.in b/BZOJ/1552/data/1.in new file mode 100644 index 00000000..1926440a --- /dev/null +++ b/BZOJ/1552/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e4091f40adcc89e5300007dcaa8562baea2624e596de875613604f9cb38c2e0d +size 14 diff --git a/BZOJ/1552/data/1.out b/BZOJ/1552/data/1.out new file mode 100644 index 00000000..d49e175a --- /dev/null +++ b/BZOJ/1552/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a83d4a4e6ea49da52a3affa7741ddbd9384cd719813b120e35d2db09157d2a02 +size 12 diff --git a/BZOJ/1552/data/10.in b/BZOJ/1552/data/10.in new file mode 100644 index 00000000..74d0d617 --- /dev/null +++ b/BZOJ/1552/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b95d0196014090a4ee2d87e9c0d30a00656464aef3e503d1e2d0ff8727c5bc43 +size 176703 diff --git a/BZOJ/1552/data/10.out b/BZOJ/1552/data/10.out new file mode 100644 index 00000000..b88aaa4b --- /dev/null +++ b/BZOJ/1552/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:00c1c780ac5e7a07da08ff0f699298f50e7b3e98495b460bf9f7553c9b712e31 +size 178077 diff --git a/BZOJ/1552/data/11.in b/BZOJ/1552/data/11.in new file mode 100644 index 00000000..af7a918d --- /dev/null +++ b/BZOJ/1552/data/11.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:98329aa95ea4c9f8106eb83a04f63f7dc8f5b663e59e4a4b0e4e4687b9310219 +size 290127 diff --git a/BZOJ/1552/data/11.out b/BZOJ/1552/data/11.out new file mode 100644 index 00000000..c3e1df86 --- /dev/null +++ b/BZOJ/1552/data/11.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1a19103195173cc04cfd3149dc3ba2ce5b39a88f3c9e9ec3a2b13ce6f18c83f4 +size 595723 diff --git a/BZOJ/1552/data/2.in b/BZOJ/1552/data/2.in new file mode 100644 index 00000000..edc941c2 --- /dev/null +++ b/BZOJ/1552/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d53adf676ef38fd5150263b2424f4205a44a195b85e54de69f8dfbd623630cbf +size 323879 diff --git a/BZOJ/1552/data/2.out b/BZOJ/1552/data/2.out new file mode 100644 index 00000000..df201245 --- /dev/null +++ b/BZOJ/1552/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:46078f40233f85e37b8bed372cee838e45315e52d3ae783b301ee702b5ddc315 +size 329062 diff --git a/BZOJ/1552/data/3.in b/BZOJ/1552/data/3.in new file mode 100644 index 00000000..e54d2fae --- /dev/null +++ b/BZOJ/1552/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:299cf06c495d2340f931acaa229127b76930e01f4a894014c717bff701af6e40 +size 588896 diff --git a/BZOJ/1552/data/3.out b/BZOJ/1552/data/3.out new file mode 100644 index 00000000..c223aa44 --- /dev/null +++ b/BZOJ/1552/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d79e359c3bbfc7079c4de89b99d5690f5a9b368d0c527ef5d07e9f81f8f77afe +size 599457 diff --git a/BZOJ/1552/data/4.in b/BZOJ/1552/data/4.in new file mode 100644 index 00000000..9ac2f863 --- /dev/null +++ b/BZOJ/1552/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f643d6dd4011dc4f300d6cee6443f3b5cb032ff9e0ff0ec2b70e7119d605b831 +size 275433 diff --git a/BZOJ/1552/data/4.out b/BZOJ/1552/data/4.out new file mode 100644 index 00000000..c79423e8 --- /dev/null +++ b/BZOJ/1552/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b0448bb92bab31c22adac7138bbf9d02fdd620c459364219596607aff0b914b2 +size 238641 diff --git a/BZOJ/1552/data/5.in b/BZOJ/1552/data/5.in new file mode 100644 index 00000000..2d03a494 --- /dev/null +++ b/BZOJ/1552/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6e86709f9a329198dcab3f6aef0d599a773978dc1765f7999b564cf713b8a3ed +size 482329 diff --git a/BZOJ/1552/data/5.out b/BZOJ/1552/data/5.out new file mode 100644 index 00000000..7f747856 --- /dev/null +++ b/BZOJ/1552/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e3692a0e1d7e0dc81dcf488f1afc976c785564aeeffb6d05d4ed886100fc486e +size 419234 diff --git a/BZOJ/1552/data/6.in b/BZOJ/1552/data/6.in new file mode 100644 index 00000000..90b95d47 --- /dev/null +++ b/BZOJ/1552/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e99a76a6b698f734208a8a9188a880016490e721eab71c5933fd96d1788e8f88 +size 58904 diff --git a/BZOJ/1552/data/6.out b/BZOJ/1552/data/6.out new file mode 100644 index 00000000..521c6cd2 --- /dev/null +++ b/BZOJ/1552/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b107b7d5f0bfb54514b598e036c87b32ec865a1039a44bb81f46c27e1beeddfb +size 49957 diff --git a/BZOJ/1552/data/7.in b/BZOJ/1552/data/7.in new file mode 100644 index 00000000..7fbd00b6 --- /dev/null +++ b/BZOJ/1552/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:68e3dc3ca50389c3d208dfcb9720430d1af0083e63a3e8e7526fa5d6baffef21 +size 588902 diff --git a/BZOJ/1552/data/7.out b/BZOJ/1552/data/7.out new file mode 100644 index 00000000..b21c0851 --- /dev/null +++ b/BZOJ/1552/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:02c26fc170cc9e11332a8e6ba7279f6f61f63111dd18ca9eb0f731cc55c46b11 +size 700000 diff --git a/BZOJ/1552/data/8.in b/BZOJ/1552/data/8.in new file mode 100644 index 00000000..ae94181d --- /dev/null +++ b/BZOJ/1552/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:15e5d764fa9c4a93193f55425b52eb975f97901bdcc730a0bbb789b9e1688268 +size 788862 diff --git a/BZOJ/1552/data/8.out b/BZOJ/1552/data/8.out new file mode 100644 index 00000000..460c7c27 --- /dev/null +++ b/BZOJ/1552/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ba166833d472f0064d27b95941ce7e836c0ee2ceaf272dd3abecc7a10e02fff6 +size 599468 diff --git a/BZOJ/1552/data/9.in b/BZOJ/1552/data/9.in new file mode 100644 index 00000000..f76afdf8 --- /dev/null +++ b/BZOJ/1552/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c9a702b7b994c94fbe81d63a5b61e608c7b6688bd2203afd978bea4c26798eec +size 4889 diff --git a/BZOJ/1552/data/9.out b/BZOJ/1552/data/9.out new file mode 100644 index 00000000..6c4bde5f --- /dev/null +++ b/BZOJ/1552/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3644a7183cc94c075e52e806cc5b209c96e2ca2f09ae58484a06c2de2a750040 +size 4002