From c1ebbefb170376f58e2f4d89a898ea98b190c243 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 14 Dec 2022 17:01:15 +0800 Subject: [PATCH] =?UTF-8?q?2733.=20[HNOI2012]=20=E6=B0=B8=E6=97=A0?= =?UTF-8?q?=E4=B9=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://hydro.ac/d/bzoj/record/639990b17e5c9216cb39a349 --- BZOJ/2733/2733.cpp | 228 ++++++++++++++++++++++++++++++++++++++++++ BZOJ/2733/data/1.in | 3 + BZOJ/2733/data/1.out | 3 + BZOJ/2733/data/10.in | 3 + BZOJ/2733/data/10.out | 3 + BZOJ/2733/data/2.in | 3 + BZOJ/2733/data/2.out | 3 + BZOJ/2733/data/3.in | 3 + BZOJ/2733/data/3.out | 3 + BZOJ/2733/data/4.in | 3 + BZOJ/2733/data/4.out | 3 + BZOJ/2733/data/5.in | 3 + BZOJ/2733/data/5.out | 3 + BZOJ/2733/data/6.in | 3 + BZOJ/2733/data/6.out | 3 + BZOJ/2733/data/7.in | 3 + BZOJ/2733/data/7.out | 3 + BZOJ/2733/data/8.in | 3 + BZOJ/2733/data/8.out | 3 + BZOJ/2733/data/9.in | 3 + BZOJ/2733/data/9.out | 3 + 21 files changed, 288 insertions(+) create mode 100644 BZOJ/2733/2733.cpp create mode 100644 BZOJ/2733/data/1.in create mode 100644 BZOJ/2733/data/1.out create mode 100644 BZOJ/2733/data/10.in create mode 100644 BZOJ/2733/data/10.out create mode 100644 BZOJ/2733/data/2.in create mode 100644 BZOJ/2733/data/2.out create mode 100644 BZOJ/2733/data/3.in create mode 100644 BZOJ/2733/data/3.out create mode 100644 BZOJ/2733/data/4.in create mode 100644 BZOJ/2733/data/4.out create mode 100644 BZOJ/2733/data/5.in create mode 100644 BZOJ/2733/data/5.out create mode 100644 BZOJ/2733/data/6.in create mode 100644 BZOJ/2733/data/6.out create mode 100644 BZOJ/2733/data/7.in create mode 100644 BZOJ/2733/data/7.out create mode 100644 BZOJ/2733/data/8.in create mode 100644 BZOJ/2733/data/8.out create mode 100644 BZOJ/2733/data/9.in create mode 100644 BZOJ/2733/data/9.out diff --git a/BZOJ/2733/2733.cpp b/BZOJ/2733/2733.cpp new file mode 100644 index 00000000..1003673a --- /dev/null +++ b/BZOJ/2733/2733.cpp @@ -0,0 +1,228 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +class Treap { + private: + struct node { + node *left, *right; + int size, val, key; + + node(const int &_val = 0) + : left(nullptr), right(nullptr), size(1), val(_val), key(rand()) {} + + ~node() { + delete left; + delete right; + } + + void pushup() { + size = 1; + + if (left != nullptr) size += left->size; + if (right != nullptr) size += right->size; + } + } * root; + + inline int getNodeSize(node *node) { + return node == nullptr ? 0 : node->size; + } + + node *find(node *p, int val) { + if (p == nullptr) return nullptr; + if (p->val == val) return p; + if (p->val > val) return find(p->left, val); + return find(p->right, val); + } + + std::pair split(node *p, int k) { + if (p == nullptr) return std::make_pair(nullptr, nullptr); + + std::pair o; + + if (k <= getNodeSize(p->left)) { + o = split(p->left, k); + p->left = o.second; + p->pushup(); + o.second = p; + } else { + o = split(p->right, k - getNodeSize(p->left) - 1); + p->right = o.first; + p->pushup(); + o.first = p; + } + + return o; + } + + std::pair splitByValue(node *p, int val) { + if (p == nullptr) return std::make_pair(nullptr, nullptr); + + std::pair o; + + if (p->val < val) { + o = splitByValue(p->right, val); + p->right = o.first; + p->pushup(); + o.first = p; + } else { + o = splitByValue(p->left, val); + p->left = o.second; + p->pushup(); + o.second = p; + } + + return o; + } + + node *merge(node *x, node *y) { + if (x == nullptr) return y; + if (y == nullptr) return x; + + if (x->key > y->key) { + x->right = merge(x->right, y); + x->pushup(); + return x; + } + + y->left = merge(x, y->left); + y->pushup(); + + return y; + } + + void _merge(node *cur) { + if (cur->left != nullptr) _merge(cur->left); + if (cur->right != nullptr) _merge(cur->right); + + insert(cur->val); + } + + public: + Treap() + : root(nullptr) {} + + ~Treap() { + delete root; + } + + void insert(int val) { + auto o = splitByValue(root, val); + o.first = merge(o.first, new Treap::node(val)); + root = merge(o.first, o.second); + } + + void erase(int val) { + auto o = splitByValue(root, val); + auto t = o; + + if (find(o.second, val) != nullptr) { + t = split(o.second, 1); + delete t.first; + } + + root = merge(o.first, t.second); + } + + int getRank(int val) { + auto x = splitByValue(root, val); + int r = getNodeSize(x.first) + 1; + + root = merge(x.first, x.second); + + return r; + } + + int getKth(int k) { + auto x = split(root, k - 1); + auto y = split(x.second, 1); + + Treap::node *o = y.first; + + root = merge(x.first, merge(y.first, y.second)); + + return o == nullptr ? 0 : o->val; + } + + int size() { + return getNodeSize(root); + } + + void merge(Treap &b) { + _merge(b.root); + } +} tr[N]; + +int n, m, q, a[N], fa[N]; + +int find(int x) { + return fa[x] == x ? x : fa[x] = find(fa[x]); +} + +void merge(int x, int y) { + x = find(x); + y = find(y); + + if (x != y) { + if (tr[x].size() < tr[y].size()) { + fa[x] = y; + tr[y].merge(tr[x]); + } else { + fa[y] = x; + tr[x].merge(tr[y]); + } + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + a[0] = -1; + + for (int i = 1, x; i <= n; i++) { + cin >> x; + + a[x] = i; + fa[i] = i; + tr[i].insert(x); + } + + for (int i = 1, u, v; i <= m; i++) { + cin >> u >> v; + + merge(u, v); + } + + cin >> q; + + while (q--) { + char op; + int x, y; + + cin >> op >> x >> y; + + if (op == 'Q') { + auto &t = tr[find(x)]; + + if (t.size() < y) { + cout << -1 << endl; + } else { + cout << a[t.getKth(y)] << endl; + } + } else { // op == 'B' + merge(x, y); + } + } + + return 0; +} diff --git a/BZOJ/2733/data/1.in b/BZOJ/2733/data/1.in new file mode 100644 index 00000000..9b781f9e --- /dev/null +++ b/BZOJ/2733/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:23a8de72d842a52b9420ecc4a46b1d2f71535e32a481814a8f88d517f656423f +size 1446891 diff --git a/BZOJ/2733/data/1.out b/BZOJ/2733/data/1.out new file mode 100644 index 00000000..125f0445 --- /dev/null +++ b/BZOJ/2733/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8db5989285f42acf41881b4fdb6006eac72c1aa86fcda318c9a3b4a80b27bead +size 291780 diff --git a/BZOJ/2733/data/10.in b/BZOJ/2733/data/10.in new file mode 100644 index 00000000..23c5ceb4 --- /dev/null +++ b/BZOJ/2733/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:74008637ec07b228db5117a2d8ab760a44e5ab9918713d8cf3fa95f2bad21fc4 +size 162341 diff --git a/BZOJ/2733/data/10.out b/BZOJ/2733/data/10.out new file mode 100644 index 00000000..e28b0598 --- /dev/null +++ b/BZOJ/2733/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:512ba05a93d544c57aaf5c9c6729defb24fe2b6791ad41069a89af8beb48abfb +size 24829 diff --git a/BZOJ/2733/data/2.in b/BZOJ/2733/data/2.in new file mode 100644 index 00000000..67a5ee07 --- /dev/null +++ b/BZOJ/2733/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a85cc60832a49bcbf5a51c14262ac1ba095adfa10b0f9cc51dcfd0a3ed45b24a +size 13309 diff --git a/BZOJ/2733/data/2.out b/BZOJ/2733/data/2.out new file mode 100644 index 00000000..37f43c14 --- /dev/null +++ b/BZOJ/2733/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:848b0bc0ce4ecda7d1f2612cfbede5c687599d5e6e66423e5906cece25ae9663 +size 1742 diff --git a/BZOJ/2733/data/3.in b/BZOJ/2733/data/3.in new file mode 100644 index 00000000..14fe15cb --- /dev/null +++ b/BZOJ/2733/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:57388844d787098fad0f2fcd6bbc49dcd0e374d32f7a06ef90817d090112d361 +size 4052672 diff --git a/BZOJ/2733/data/3.out b/BZOJ/2733/data/3.out new file mode 100644 index 00000000..49378683 --- /dev/null +++ b/BZOJ/2733/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:266e73477a19e9ce7aeae7849e665ea595696e310c406a4250ab9c6a5bfcce6e +size 1256245 diff --git a/BZOJ/2733/data/4.in b/BZOJ/2733/data/4.in new file mode 100644 index 00000000..4ddf1492 --- /dev/null +++ b/BZOJ/2733/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:24e98abe3cf1453cba43813e9ecb0693c48c2af03ff85c7582a2216e78fd568b +size 73521 diff --git a/BZOJ/2733/data/4.out b/BZOJ/2733/data/4.out new file mode 100644 index 00000000..cba208a3 --- /dev/null +++ b/BZOJ/2733/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cc06c78cb45a42bfe2e16db9ac1045ca77c8c6d8446958cb42f471609de5cfd9 +size 11940 diff --git a/BZOJ/2733/data/5.in b/BZOJ/2733/data/5.in new file mode 100644 index 00000000..cde541d8 --- /dev/null +++ b/BZOJ/2733/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f25fa28fc2a5cd2769f7684d9a79d4fb08b86fd1692cebfebd3acf54ae986bd1 +size 5838163 diff --git a/BZOJ/2733/data/5.out b/BZOJ/2733/data/5.out new file mode 100644 index 00000000..460f5df6 --- /dev/null +++ b/BZOJ/2733/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9a2a345cfbe8a40f7d04c71d7c415f9b42b6518532a570ab407fae7666cd249a +size 1766142 diff --git a/BZOJ/2733/data/6.in b/BZOJ/2733/data/6.in new file mode 100644 index 00000000..9db5db22 --- /dev/null +++ b/BZOJ/2733/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:79d5d6f005105674a084bba7f82c57a3bc0b0c7a82e1a86c2c7bf31ab1f4c85f +size 354929 diff --git a/BZOJ/2733/data/6.out b/BZOJ/2733/data/6.out new file mode 100644 index 00000000..a3f978f4 --- /dev/null +++ b/BZOJ/2733/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2537c2faae372f91ca58db419255943d6b75934de9236c012bbe148b9fb818b6 +size 105580 diff --git a/BZOJ/2733/data/7.in b/BZOJ/2733/data/7.in new file mode 100644 index 00000000..974110f4 --- /dev/null +++ b/BZOJ/2733/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d60ac228425b719d78226267b50af5ba581fad9078813a8f493da7c4d7b83e2e +size 13256 diff --git a/BZOJ/2733/data/7.out b/BZOJ/2733/data/7.out new file mode 100644 index 00000000..ea776bac --- /dev/null +++ b/BZOJ/2733/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:29a537eb7553b1894e48e5ffca1f2e1446a0c21ed8834446f4ed96b7d1fde30e +size 1995 diff --git a/BZOJ/2733/data/8.in b/BZOJ/2733/data/8.in new file mode 100644 index 00000000..9d6189d4 --- /dev/null +++ b/BZOJ/2733/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c9a632b71104eb5bd368e726066dd03d3d255663c7b705dd201a72baa787e120 +size 2970521 diff --git a/BZOJ/2733/data/8.out b/BZOJ/2733/data/8.out new file mode 100644 index 00000000..3ea5019c --- /dev/null +++ b/BZOJ/2733/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e844b16b1df3b12ee1ca783b0fc8925bc907644190c88c1dccb236c620410d2b +size 725243 diff --git a/BZOJ/2733/data/9.in b/BZOJ/2733/data/9.in new file mode 100644 index 00000000..f2c6d5a8 --- /dev/null +++ b/BZOJ/2733/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3542b1ce0e14788416aa8c7feec0f41df8c79dbfb1f09a190f0758ca14d7424e +size 3916212 diff --git a/BZOJ/2733/data/9.out b/BZOJ/2733/data/9.out new file mode 100644 index 00000000..332cfd67 --- /dev/null +++ b/BZOJ/2733/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fd6005c164e6dc3603caad6cf26f61ea71b3e91515b67b1c71b527eeb4066684 +size 1197509