From 9a5d30c834b6065e4ac7d6428f9d513c70730b8e Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 2 Jan 2023 10:27:06 +0800 Subject: [PATCH] 1180. [Croatian2009] OTOCI https://hydro.ac/d/bzoj/record/63b240eb246ae133e6cb84e9 --- BZOJ/1180/1180.cpp | 214 ++++++++++++++++++++++++++++++++++++++++++ BZOJ/1180/data/1.in | 3 + BZOJ/1180/data/1.out | 3 + BZOJ/1180/data/10.in | 3 + BZOJ/1180/data/10.out | 3 + BZOJ/1180/data/2.in | 3 + BZOJ/1180/data/2.out | 3 + BZOJ/1180/data/3.in | 3 + BZOJ/1180/data/3.out | 3 + BZOJ/1180/data/4.in | 3 + BZOJ/1180/data/4.out | 3 + BZOJ/1180/data/5.in | 3 + BZOJ/1180/data/5.out | 3 + BZOJ/1180/data/6.in | 3 + BZOJ/1180/data/6.out | 3 + BZOJ/1180/data/7.in | 3 + BZOJ/1180/data/7.out | 3 + BZOJ/1180/data/8.in | 3 + BZOJ/1180/data/8.out | 3 + BZOJ/1180/data/9.in | 3 + BZOJ/1180/data/9.out | 3 + 21 files changed, 274 insertions(+) create mode 100644 BZOJ/1180/1180.cpp create mode 100644 BZOJ/1180/data/1.in create mode 100644 BZOJ/1180/data/1.out create mode 100644 BZOJ/1180/data/10.in create mode 100644 BZOJ/1180/data/10.out create mode 100644 BZOJ/1180/data/2.in create mode 100644 BZOJ/1180/data/2.out create mode 100644 BZOJ/1180/data/3.in create mode 100644 BZOJ/1180/data/3.out create mode 100644 BZOJ/1180/data/4.in create mode 100644 BZOJ/1180/data/4.out create mode 100644 BZOJ/1180/data/5.in create mode 100644 BZOJ/1180/data/5.out create mode 100644 BZOJ/1180/data/6.in create mode 100644 BZOJ/1180/data/6.out create mode 100644 BZOJ/1180/data/7.in create mode 100644 BZOJ/1180/data/7.out create mode 100644 BZOJ/1180/data/8.in create mode 100644 BZOJ/1180/data/8.out create mode 100644 BZOJ/1180/data/9.in create mode 100644 BZOJ/1180/data/9.out diff --git a/BZOJ/1180/1180.cpp b/BZOJ/1180/1180.cpp new file mode 100644 index 00000000..521fe0f8 --- /dev/null +++ b/BZOJ/1180/1180.cpp @@ -0,0 +1,214 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 3e4 + 5; + +class LinkCutTree { + private: + struct node { + size_t l, r, f; + unsigned v, s; + bool rev; + + node() + : l(0), r(0), f(0), s(0), v(0), rev(false) {} + + node(unsigned _v, size_t _f) + : l(0), r(0), f(_f), s(_v), v(_v), rev(false) {} + + size_t &child(unsigned x) { + return !x ? l : r; + } + } tr[N]; + + inline void pushup(size_t u) { + tr[u].s = tr[tr[u].l].s + tr[u].v + tr[tr[u].r].s; + } + + inline void pushdown(const size_t &u) { + if (!tr[u].rev) return; + + std::swap(tr[u].l, tr[u].r); + tr[tr[u].l].rev = !tr[tr[u].l].rev; + tr[tr[u].r].rev = !tr[tr[u].r].rev; + tr[u].rev = false; + } + + unsigned relation(const size_t &u) { + return u == tr[tr[u].f].l ? 0 : 1; + } + + bool isRoot(const size_t &u) { + return tr[tr[u].f].l != u && tr[tr[u].f].r != u; + } + + void rotate(size_t u) { + size_t p = tr[u].f; + unsigned x = relation(u); + + if (!isRoot(p)) { + tr[tr[p].f].child(relation(p)) = u; + } + tr[u].f = tr[p].f; + + if (tr[u].child(x ^ 1)) { + tr[tr[u].child(x ^ 1)].f = p; + } + tr[p].child(x) = tr[u].child(x ^ 1); + + tr[u].child(x ^ 1) = p; + tr[p].f = u; + + pushup(p); + pushup(u); + } + + void splay(size_t u) { + std::stack st; + + size_t cur = u; + st.push(cur); + while (!isRoot(cur)) { + st.push(tr[cur].f); + cur = tr[cur].f; + } + + while (!st.empty()) { + pushdown(st.top()); + st.pop(); + } + + while (!isRoot(u)) { + if (isRoot(tr[u].f)) { + rotate(u); + } else if (relation(u) == relation(tr[u].f)) { + rotate(tr[u].f); + rotate(u); + } else { + rotate(u); + rotate(u); + } + } + } + + void access(size_t u) { + for (size_t f = 0; u; u = tr[f = u].f) { + splay(u); + tr[u].r = f; + pushup(u); + } + } + + void makeRoot(const size_t &u) { + access(u); + splay(u); + tr[u].rev = !tr[u].rev; + } + + size_t findRoot(size_t u) { + access(u); + splay(u); + + while (tr[u].l) { + u = tr[u].l; + } + + return u; + } + + void split(const size_t &x, const size_t &y) { + makeRoot(x); + access(y); + splay(y); + } + + public: + void set(int p, int v) { + tr[p].s = tr[p].v = v; + } + + unsigned query(int x, int y) { + split(x, y); + + return tr[y].s; + } + + bool check(int x, int y) { + return findRoot(x) == findRoot(y); + } + + void link(const int &x, const int &y) { + makeRoot(x); + + if (findRoot(y) != x) { + tr[x].f = y; + } + } + + void cut(int x, int y) { + split(x, y); + + if (tr[y].l == x) { + tr[y].l = 0; + tr[x].f = 0; + } + } + + void change(int p, int v) { + access(p); + splay(p); + tr[p].v = v; + pushup(p); + } +} lct; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + + cin >> n; + + for (int i = 1, x; i <= n; i++) { + cin >> x; + + lct.set(i, x); + } + + int q; + + cin >> q; + + while (q--) { + std::string op; + int x, y; + + cin >> op >> x >> y; + + if (op == "bridge") { + if (lct.check(x, y)) { + cout << "no" << endl; + } else { + lct.link(x, y); + + cout << "yes" << endl; + } + } else if (op == "penguins") { + lct.change(x, y); + } else { // op == "excursion" + if (lct.check(x, y)) { + cout << lct.query(x, y) << endl; + } else { + cout << "impossible" << endl; + } + } + } + + return 0; +} diff --git a/BZOJ/1180/data/1.in b/BZOJ/1180/data/1.in new file mode 100644 index 00000000..9605654a --- /dev/null +++ b/BZOJ/1180/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:853fd16c11ed82d79681e7173f5134d0d2330294ced185837e11a9542f67611d +size 5286949 diff --git a/BZOJ/1180/data/1.out b/BZOJ/1180/data/1.out new file mode 100644 index 00000000..9b9e5d84 --- /dev/null +++ b/BZOJ/1180/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:be72a36e98795c8bd4c270955c83eea2973cbe222c53cc88df1bf55749f4b803 +size 1746943 diff --git a/BZOJ/1180/data/10.in b/BZOJ/1180/data/10.in new file mode 100644 index 00000000..2a241815 --- /dev/null +++ b/BZOJ/1180/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:64c6c02e18d1760c1fbaa8d55c20fa4c5a720a9b6e21bdbb636beb2872e18247 +size 294 diff --git a/BZOJ/1180/data/10.out b/BZOJ/1180/data/10.out new file mode 100644 index 00000000..b4e5c134 --- /dev/null +++ b/BZOJ/1180/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:073b2b5d508a0c39823daad1e047d75e6dcce1a5b2b461a6c35d46cd599e3bb5 +size 80 diff --git a/BZOJ/1180/data/2.in b/BZOJ/1180/data/2.in new file mode 100644 index 00000000..68165a04 --- /dev/null +++ b/BZOJ/1180/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:72608a41d0f5b8c6d3ef5de65b97cc91b27dafd0d413a2d57796d96a38e68912 +size 7703 diff --git a/BZOJ/1180/data/2.out b/BZOJ/1180/data/2.out new file mode 100644 index 00000000..f32e9726 --- /dev/null +++ b/BZOJ/1180/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d2022244bf5b04d26c044ec3800b6c7a1bf41654ec5acd246a93bda5c3df7276 +size 3174 diff --git a/BZOJ/1180/data/3.in b/BZOJ/1180/data/3.in new file mode 100644 index 00000000..6fa95edd --- /dev/null +++ b/BZOJ/1180/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1d1145ad9e6849c0eaff75be41496b0dc66e84588b903ca2344c5cdaf2d5768e +size 3075568 diff --git a/BZOJ/1180/data/3.out b/BZOJ/1180/data/3.out new file mode 100644 index 00000000..c40aa384 --- /dev/null +++ b/BZOJ/1180/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a09c8b4ac0ad3a839ccf9b38801af8e08f37496bcaa11fceed8f6fd5e9a8d622 +size 1047941 diff --git a/BZOJ/1180/data/4.in b/BZOJ/1180/data/4.in new file mode 100644 index 00000000..b5b74e83 --- /dev/null +++ b/BZOJ/1180/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:11a09c8088b5c765d4c3091e35270c0ee0bd340484509924129d87d61a620966 +size 6405850 diff --git a/BZOJ/1180/data/4.out b/BZOJ/1180/data/4.out new file mode 100644 index 00000000..b2372494 --- /dev/null +++ b/BZOJ/1180/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:af4b7ff9255d89fac3bb02567fcc46827d20fe50ac8616b7512947882b226566 +size 2095274 diff --git a/BZOJ/1180/data/5.in b/BZOJ/1180/data/5.in new file mode 100644 index 00000000..f37dd84d --- /dev/null +++ b/BZOJ/1180/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:44ebd4d086c769ce2e9b80c7db36bc27e803970f713ca36a8bd4aefabbaf30a4 +size 2950336 diff --git a/BZOJ/1180/data/5.out b/BZOJ/1180/data/5.out new file mode 100644 index 00000000..b9999b5c --- /dev/null +++ b/BZOJ/1180/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c5e3751931ccb64e25aa60acb76594dab18ccd30d74731d451ab9d349eab524c +size 1065705 diff --git a/BZOJ/1180/data/6.in b/BZOJ/1180/data/6.in new file mode 100644 index 00000000..a9f185e1 --- /dev/null +++ b/BZOJ/1180/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0baa0e0e2da1c142e6252357f6737bda6a2aa5f9864301bdfbd152f324cd5da1 +size 207865 diff --git a/BZOJ/1180/data/6.out b/BZOJ/1180/data/6.out new file mode 100644 index 00000000..93ee9c01 --- /dev/null +++ b/BZOJ/1180/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a6ec06bb99850ebac7b3d0293785634d3491b7f5c7c2371bf13a15ffb374bc25 +size 43745 diff --git a/BZOJ/1180/data/7.in b/BZOJ/1180/data/7.in new file mode 100644 index 00000000..1b8c71a3 --- /dev/null +++ b/BZOJ/1180/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:beefb68d77fe6f8aa9d5ddc383dd845dda00f647ca41f1c8aebcf1f2451020dd +size 209472 diff --git a/BZOJ/1180/data/7.out b/BZOJ/1180/data/7.out new file mode 100644 index 00000000..c1a8361f --- /dev/null +++ b/BZOJ/1180/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a80841fc2813c0cd754ac363552dbb6e71380982ba37b82aaa5496a9b311ffe9 +size 45067 diff --git a/BZOJ/1180/data/8.in b/BZOJ/1180/data/8.in new file mode 100644 index 00000000..8380b3b6 --- /dev/null +++ b/BZOJ/1180/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c0bc2407d6a751484a8e856e93714588a7c0a34f6118ffca4e2ef60c9f1b89b9 +size 5208027 diff --git a/BZOJ/1180/data/8.out b/BZOJ/1180/data/8.out new file mode 100644 index 00000000..f371f26a --- /dev/null +++ b/BZOJ/1180/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a05b5711139fb16b388b7580d2181888c3fe2ef751cfe9b90a61cdb1bd0016fa +size 1662554 diff --git a/BZOJ/1180/data/9.in b/BZOJ/1180/data/9.in new file mode 100644 index 00000000..75623480 --- /dev/null +++ b/BZOJ/1180/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0175f3269a3f1b752321378899b3984c3962dd0147cf7229c077144608566c7b +size 6243319 diff --git a/BZOJ/1180/data/9.out b/BZOJ/1180/data/9.out new file mode 100644 index 00000000..b44b14e8 --- /dev/null +++ b/BZOJ/1180/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8d2521234fefe874d1ca07fb66d706ab5f504e796a1e77bb04936d08adeba5f1 +size 1672462