From a273b46d3459db19b076c9fce3ee2d6a95c2a5ac Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 8 Dec 2022 17:12:44 +0800 Subject: [PATCH] =?UTF-8?q?#1790.=20=E3=80=902017.3=20=E9=95=BF=E4=B9=90?= =?UTF-8?q?=E7=9C=81=E9=80=89=E9=9B=86=E8=AE=AD=20Day15=20T2=E3=80=81?= =?UTF-8?q?=E9=9B=85=E7=A4=BC=E9=9B=86=E8=AE=AD=202017=20Day5=E3=80=91?= =?UTF-8?q?=E8=BF=9C=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/65558 --- S2OJ/1790/1790.cpp | 240 ++++++++++++++++++++++++++++++++++++ S2OJ/1790/data/hike1.in | 3 + S2OJ/1790/data/hike1.out | 3 + S2OJ/1790/data/hike10.in | 3 + S2OJ/1790/data/hike10.out | 3 + S2OJ/1790/data/hike2.in | 3 + S2OJ/1790/data/hike2.out | 3 + S2OJ/1790/data/hike3.in | 3 + S2OJ/1790/data/hike3.out | 3 + S2OJ/1790/data/hike4.in | 3 + S2OJ/1790/data/hike4.out | 3 + S2OJ/1790/data/hike5.in | 3 + S2OJ/1790/data/hike5.out | 3 + S2OJ/1790/data/hike6.in | 3 + S2OJ/1790/data/hike6.out | 3 + S2OJ/1790/data/hike7.in | 3 + S2OJ/1790/data/hike7.out | 3 + S2OJ/1790/data/hike8.in | 3 + S2OJ/1790/data/hike8.out | 3 + S2OJ/1790/data/hike9.in | 3 + S2OJ/1790/data/hike9.out | 3 + S2OJ/1790/data/problem.conf | 3 + 22 files changed, 303 insertions(+) create mode 100644 S2OJ/1790/1790.cpp create mode 100644 S2OJ/1790/data/hike1.in create mode 100644 S2OJ/1790/data/hike1.out create mode 100644 S2OJ/1790/data/hike10.in create mode 100644 S2OJ/1790/data/hike10.out create mode 100644 S2OJ/1790/data/hike2.in create mode 100644 S2OJ/1790/data/hike2.out create mode 100644 S2OJ/1790/data/hike3.in create mode 100644 S2OJ/1790/data/hike3.out create mode 100644 S2OJ/1790/data/hike4.in create mode 100644 S2OJ/1790/data/hike4.out create mode 100644 S2OJ/1790/data/hike5.in create mode 100644 S2OJ/1790/data/hike5.out create mode 100644 S2OJ/1790/data/hike6.in create mode 100644 S2OJ/1790/data/hike6.out create mode 100644 S2OJ/1790/data/hike7.in create mode 100644 S2OJ/1790/data/hike7.out create mode 100644 S2OJ/1790/data/hike8.in create mode 100644 S2OJ/1790/data/hike8.out create mode 100644 S2OJ/1790/data/hike9.in create mode 100644 S2OJ/1790/data/hike9.out create mode 100644 S2OJ/1790/data/problem.conf diff --git a/S2OJ/1790/1790.cpp b/S2OJ/1790/1790.cpp new file mode 100644 index 00000000..95d9565c --- /dev/null +++ b/S2OJ/1790/1790.cpp @@ -0,0 +1,240 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 3e5 + 5; + +// Link-Cut Tree +class LinkCutTree { + private: + std::stack st; + + struct node { + int p, // 父亲节点 + l, // 左儿子 + r; // 右儿子 + int pre; + int val, // 节点值 + sum; // 权值和 + int key; // 权值 + bool rev; // 翻转标记 + + node() + : p(0), l(0), r(0), pre(0), val(0), sum(0), key(rand()), rev(false) {} + } tr[N]; + + void pushup(int u) { + // 计算异或和 + tr[u].sum = tr[tr[u].l].sum + tr[u].val + tr[tr[u].r].sum; + + // 标记父亲节点 + if (tr[u].l) tr[tr[u].l].p = u; + if (tr[u].r) tr[tr[u].r].p = u; + } + + void pushdown(int u) { + if (!tr[u].rev) return; + + tr[u].rev = false; + std::swap(tr[u].l, tr[u].r); + tr[tr[u].l].rev ^= 1; + tr[tr[u].r].rev ^= 1; + } + + std::pair split(int u) { + if (st.empty()) { + pushdown(u); + auto t = std::make_pair(u, tr[u].r); + tr[u].r = 0; + pushup(u); + + return t; + } + + bool d = st.top() ^ tr[u].rev; + st.pop(); + + pushdown(u); + + if (d) { + auto t = split(tr[u].l); + tr[u].l = t.second; + pushup(u); + + return std::make_pair(t.first, u); + } + + auto t = split(tr[u].r); + tr[u].r = t.first; + pushup(u); + + return std::make_pair(u, t.second); + } + + // 合并 + int merge(int x, int y) { + if (!x || !y) return x | y; + + if (tr[x].key < tr[y].key) { + pushdown(x); + tr[x].r = merge(tr[x].r, y); + pushup(x); + return x; + } + + pushdown(y); + tr[y].l = merge(x, tr[y].l); + pushup(y); + return y; + } + + // 是否是根节点 + bool isRoot(int u) { + return !tr[u].p || (tr[tr[u].p].l != u && tr[tr[u].p].r != u); + } + + // 查找根节点 + int findRoot(int u) { + while (!st.empty()) st.pop(); + while (!isRoot(u)) { + st.push(tr[tr[u].p].l == u); + u = tr[u].p; + } + return u; + } + + int findLeft(int u) { + u = findRoot(u); + pushdown(u); + while (tr[u].l) { + u = tr[u].l; + pushdown(u); + } + return u; + } + + int access(int u) { + int lst = 0; + + while (u) { + auto t = split(findRoot(u)); + tr[findLeft(lst)].pre = 0; + lst = merge(t.first, lst); + tr[findLeft(t.second)].pre = u; + u = tr[findLeft(lst)].pre; + } + + return lst; + } + + void makeRoot(int u) { + tr[access(u)].rev ^= 1; + } + + public: + int getRoot(int u) { + return findLeft(access(u)); + } + + void link(int x, int y) { + makeRoot(x); + tr[x].pre = y; + } + + int query(int x, int y) { + makeRoot(x); + access(y); + + auto t = split(findRoot(y)); + int res = tr[t.first].sum - 1; + merge(t.first, t.second); + + return res; + } + + void set(int u, int val) { + tr[u].sum = tr[u].val = val; + } +} lct; + +int type, n, q, fa[N], ans; +std::tuple t[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) return; + + fa[x] = y; + + int a, b, c, d; + + std::tie(a, b, std::ignore) = t[x]; + std::tie(c, d, std::ignore) = t[y]; + + t[y] = std::max({ + t[x], + t[y], + std::make_tuple(a, c, lct.query(a, c)), + std::make_tuple(a, d, lct.query(a, d)), + std::make_tuple(b, c, lct.query(b, c)), + std::make_tuple(b, d, lct.query(b, d)), + }, + [](const auto &a, const auto &b) { + return std::get<2>(a) < std::get<2>(b); + }); +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> type >> n >> q; + + for (int i = 1; i <= n; i++) { + fa[i] = i; + t[i] = std::make_tuple(i, i, 0); + lct.set(i, 1); + } + + while (q--) { + int op; + + cin >> op; + + if (op == 1) { + int u, v; + + cin >> u >> v; + + if (type) u ^= ans, v ^= ans; + + lct.link(u, v); + merge(u, v); + } else { // op == 2 + int u; + + cin >> u; + + if (type) u ^= ans; + + int x = find(u); + ans = std::max(lct.query(u, std::get<0>(t[x])), lct.query(u, std::get<1>(t[x]))); + + cout << ans << endl; + } + } + + return 0; +} diff --git a/S2OJ/1790/data/hike1.in b/S2OJ/1790/data/hike1.in new file mode 100644 index 00000000..66ea08c5 --- /dev/null +++ b/S2OJ/1790/data/hike1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1732fef196def3ed6f330a500a0ea0c36a437e004e879a0a269d133d15d0e315 +size 72704 diff --git a/S2OJ/1790/data/hike1.out b/S2OJ/1790/data/hike1.out new file mode 100644 index 00000000..3b0b2006 --- /dev/null +++ b/S2OJ/1790/data/hike1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c1574070937138759f12f3d98ef4ee72100b786160be46fe712cc98b49add60e +size 9445 diff --git a/S2OJ/1790/data/hike10.in b/S2OJ/1790/data/hike10.in new file mode 100644 index 00000000..aecbb532 --- /dev/null +++ b/S2OJ/1790/data/hike10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:70397cbcf7704bb08618d10a6a98aa768235d1de7b1e0e80dbcd2728a13951c2 +size 6272259 diff --git a/S2OJ/1790/data/hike10.out b/S2OJ/1790/data/hike10.out new file mode 100644 index 00000000..4f19a567 --- /dev/null +++ b/S2OJ/1790/data/hike10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:588f358f8370d1bbfdb4ac583cc354e44d169e79b4941d2dd15add9ca481e524 +size 474906 diff --git a/S2OJ/1790/data/hike2.in b/S2OJ/1790/data/hike2.in new file mode 100644 index 00000000..dcd37ef4 --- /dev/null +++ b/S2OJ/1790/data/hike2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d9638c405866f8437871ae0100887b570e542564a04a0b6ec450cbea2e800c4b +size 91663 diff --git a/S2OJ/1790/data/hike2.out b/S2OJ/1790/data/hike2.out new file mode 100644 index 00000000..d3412e2d --- /dev/null +++ b/S2OJ/1790/data/hike2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5f27ba73531a71e20a36bfae135c2d2262154541a20386fd99b6b7a527bc01f1 +size 11662 diff --git a/S2OJ/1790/data/hike3.in b/S2OJ/1790/data/hike3.in new file mode 100644 index 00000000..3e9c017d --- /dev/null +++ b/S2OJ/1790/data/hike3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:12a7eb0468b6d890f2cd1f47813aa4a6a566497904f4a480c32b65d772f12e29 +size 1650421 diff --git a/S2OJ/1790/data/hike3.out b/S2OJ/1790/data/hike3.out new file mode 100644 index 00000000..38259399 --- /dev/null +++ b/S2OJ/1790/data/hike3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:49276f482cc3fd63e1b96cb48b4076ab7f2f856bdedc9fbd6d77b25ce1f560e6 +size 172438 diff --git a/S2OJ/1790/data/hike4.in b/S2OJ/1790/data/hike4.in new file mode 100644 index 00000000..03b1727f --- /dev/null +++ b/S2OJ/1790/data/hike4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:76080df7af60d1bcedfa8bb64aa1d70f83ed8cb94f6d384fed9b749631c26524 +size 2166407 diff --git a/S2OJ/1790/data/hike4.out b/S2OJ/1790/data/hike4.out new file mode 100644 index 00000000..ecbefbdb --- /dev/null +++ b/S2OJ/1790/data/hike4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cf00bceee1ae59b2989b04dea6439edb622f1cd73c42b6f1d589c6566539a31c +size 238170 diff --git a/S2OJ/1790/data/hike5.in b/S2OJ/1790/data/hike5.in new file mode 100644 index 00000000..50a772e0 --- /dev/null +++ b/S2OJ/1790/data/hike5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2021fdd2803a0fce59293ebb9579fd6aa3facba8e18ebd6c41a514cb7e3c9b9d +size 2168704 diff --git a/S2OJ/1790/data/hike5.out b/S2OJ/1790/data/hike5.out new file mode 100644 index 00000000..7e49170b --- /dev/null +++ b/S2OJ/1790/data/hike5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ae241563ec28f9a8d0c7a18a8ba4d6bb7283dd9afb7b892f858d6f16170995f2 +size 244056 diff --git a/S2OJ/1790/data/hike6.in b/S2OJ/1790/data/hike6.in new file mode 100644 index 00000000..251f0dc6 --- /dev/null +++ b/S2OJ/1790/data/hike6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d08bfebbc52fd6fac115064d23d7c4afa0309522c241ac112ae33302989bdeec +size 6264680 diff --git a/S2OJ/1790/data/hike6.out b/S2OJ/1790/data/hike6.out new file mode 100644 index 00000000..7034d84e --- /dev/null +++ b/S2OJ/1790/data/hike6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e29bf0c4108e0922ead27c11b9b8be2ad84088df1141bd7df754842ddb701b51 +size 479829 diff --git a/S2OJ/1790/data/hike7.in b/S2OJ/1790/data/hike7.in new file mode 100644 index 00000000..7749b2f6 --- /dev/null +++ b/S2OJ/1790/data/hike7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8bb7ef3b8625380aa75b9fdb49c3f4d90762bd21335ecf1992f4fda6f9f72723 +size 6248974 diff --git a/S2OJ/1790/data/hike7.out b/S2OJ/1790/data/hike7.out new file mode 100644 index 00000000..542b66fd --- /dev/null +++ b/S2OJ/1790/data/hike7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bee9d59f379a98790dba746f375cdf068b289aa33ca16da879c3129dbc8c2f78 +size 481779 diff --git a/S2OJ/1790/data/hike8.in b/S2OJ/1790/data/hike8.in new file mode 100644 index 00000000..6e298bf6 --- /dev/null +++ b/S2OJ/1790/data/hike8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0b004885fb79816207159e6f64d19e0447a54e622b1d1e8e09595ddaa4489c12 +size 6233851 diff --git a/S2OJ/1790/data/hike8.out b/S2OJ/1790/data/hike8.out new file mode 100644 index 00000000..1178d4da --- /dev/null +++ b/S2OJ/1790/data/hike8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:589a541c6a782d1d87b0e23b17f9cca555a74985c21d2e2853df11d00fe1e204 +size 481095 diff --git a/S2OJ/1790/data/hike9.in b/S2OJ/1790/data/hike9.in new file mode 100644 index 00000000..6e684321 --- /dev/null +++ b/S2OJ/1790/data/hike9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2beece37405a8b64cf10acffc09a3f9cf447e6d9a78113fdb7b14987e36cb364 +size 6238218 diff --git a/S2OJ/1790/data/hike9.out b/S2OJ/1790/data/hike9.out new file mode 100644 index 00000000..7c4fd511 --- /dev/null +++ b/S2OJ/1790/data/hike9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1def8a2a05926a4c766c3f9647051ee1a249d93fd70efe421b3c435301d15aa0 +size 481161 diff --git a/S2OJ/1790/data/problem.conf b/S2OJ/1790/data/problem.conf new file mode 100644 index 00000000..992f7be0 --- /dev/null +++ b/S2OJ/1790/data/problem.conf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8bc88926404bddb5ad9cfa95f44e0b547091696aef6dba9aa27e948683087f2c +size 177