From bf2d7108542d08437ad1a4ec0b370d727cd38a96 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 3 Jan 2023 11:34:01 +0800 Subject: [PATCH] =?UTF-8?q?2001.=20[HNOI2010]=20City=20=E5=9F=8E=E5=B8=82?= =?UTF-8?q?=E5=BB=BA=E8=AE=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://hydro.ac/d/bzoj/record/63b3a21498c0c23d298667ca --- BZOJ/2001/2001.cpp | 312 ++++++++++++++++++++++++++++++++++++++++++ BZOJ/2001/data/1.in | 3 + BZOJ/2001/data/1.out | 3 + BZOJ/2001/data/10.in | 3 + BZOJ/2001/data/10.out | 3 + BZOJ/2001/data/2.in | 3 + BZOJ/2001/data/2.out | 3 + BZOJ/2001/data/3.in | 3 + BZOJ/2001/data/3.out | 3 + BZOJ/2001/data/4.in | 3 + BZOJ/2001/data/4.out | 3 + BZOJ/2001/data/5.in | 3 + BZOJ/2001/data/5.out | 3 + BZOJ/2001/data/6.in | 3 + BZOJ/2001/data/6.out | 3 + BZOJ/2001/data/7.in | 3 + BZOJ/2001/data/7.out | 3 + BZOJ/2001/data/8.in | 3 + BZOJ/2001/data/8.out | 3 + BZOJ/2001/data/9.in | 3 + BZOJ/2001/data/9.out | 3 + 21 files changed, 372 insertions(+) create mode 100644 BZOJ/2001/2001.cpp create mode 100644 BZOJ/2001/data/1.in create mode 100644 BZOJ/2001/data/1.out create mode 100644 BZOJ/2001/data/10.in create mode 100644 BZOJ/2001/data/10.out create mode 100644 BZOJ/2001/data/2.in create mode 100644 BZOJ/2001/data/2.out create mode 100644 BZOJ/2001/data/3.in create mode 100644 BZOJ/2001/data/3.out create mode 100644 BZOJ/2001/data/4.in create mode 100644 BZOJ/2001/data/4.out create mode 100644 BZOJ/2001/data/5.in create mode 100644 BZOJ/2001/data/5.out create mode 100644 BZOJ/2001/data/6.in create mode 100644 BZOJ/2001/data/6.out create mode 100644 BZOJ/2001/data/7.in create mode 100644 BZOJ/2001/data/7.out create mode 100644 BZOJ/2001/data/8.in create mode 100644 BZOJ/2001/data/8.out create mode 100644 BZOJ/2001/data/9.in create mode 100644 BZOJ/2001/data/9.out diff --git a/BZOJ/2001/2001.cpp b/BZOJ/2001/2001.cpp new file mode 100644 index 00000000..b08b7595 --- /dev/null +++ b/BZOJ/2001/2001.cpp @@ -0,0 +1,312 @@ +#include +#include +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1.5e5 + 5; + +int n, m, q, cnt, qr[N], lst[N]; +long long res, w[N]; +std::tuple edges[N]; +std::vector ans; + +class LinkCutTree { + private: + struct node { + size_t l, r, f; + int m; + bool rev; + + node(const int &_p = 0, const size_t &_f = 0) + : l(0), r(0), f(_f), m(_p), rev(false) {} + + size_t &child(unsigned x) { + return !x ? l : r; + } + } tr[N]; + + inline void pushup(size_t u) { + tr[u].m = u; + + if (tr[u].l && w[tr[tr[u].l].m] > w[tr[u].m]) + tr[u].m = tr[tr[u].l].m; + + if (tr[u].r && w[tr[tr[u].r].m] > w[tr[u].m]) + tr[u].m = tr[tr[u].r].m; + } + + 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: + bool check(int x, int y) { + return findRoot(x) == findRoot(y); + } + + void set(int p, int v) { + tr[p].m = v; + } + + unsigned query(int x, int y) { + split(x, y); + + return tr[y].m; + } + + 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; + } + } +} lct; + +struct node { + int l, r; + std::vector vec; +} tr[N << 2]; + +void build(int u, int l, int r) { + tr[u].l = l; + tr[u].r = r; + + if (l == r) return; + + int mid = (l + r) >> 1; + + build(u << 1, l, mid); + build(u << 1 | 1, mid + 1, r); +} + +void insert(int u, int l, int r, int id) { + if (l <= tr[u].l && tr[u].r <= r) { + tr[u].vec.emplace_back(id); + + return; + } + + int mid = (tr[u].l + tr[u].r) >> 1; + + if (l <= mid) insert(u << 1, l, r, id); + if (r > mid) insert(u << 1 | 1, l, r, id); +} + +void solve(int u) { + std::stack> st; + + for (int id : tr[u].vec) { + int x, y; + long long w; + + std::tie(x, y, w) = edges[id]; + + if (lct.check(x, y)) { + int id2 = lct.query(x, y) - n, + x2, y2; + long long w2; + + std::tie(x2, y2, w2) = edges[id2]; + + if (w2 <= w) continue; + + lct.cut(x2, id2 + n); + lct.cut(id2 + n, y2); + st.emplace(id2, true); + res -= w2; + } + + lct.link(x, id + n); + lct.link(id + n, y); + st.emplace(id, false); + res += w; + } + + if (tr[u].l == tr[u].r) { + ans.emplace_back(res); + } else { + solve(u << 1); + solve(u << 1 | 1); + } + + while (!st.empty()) { + int id; + bool type; + + std::tie(id, type) = st.top(); + st.pop(); + + int x, y; + long long w; + + std::tie(x, y, w) = edges[id]; + + if (type) { + lct.link(x, id + n); + lct.link(id + n, y); + res += w; + } else { + lct.cut(x, id + n); + lct.cut(id + n, y); + res -= w; + } + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m >> q; + + for (int i = 1; i <= m; i++) { + int x, y; + long long z; + + cin >> x >> y >> z; + + w[n + (++cnt)] = z; + edges[cnt] = {x, y, z}; + lst[i] = q + 1; + } + + for (int i = 1; i <= q; i++) { + int k; + long long d; + + cin >> k >> d; + + qr[i] = k; + w[n + (++cnt)] = d; + edges[cnt] = {std::get<0>(edges[k]), std::get<1>(edges[k]), d}; + } + + build(1, 1, q); + + for (int i = q; i; i--) { + insert(1, i, lst[qr[i]] - 1, m + i); + lst[qr[i]] = i; + } + + for (int i = 1; i <= m; i++) { + if (lst[i] > 1) { + insert(1, 1, lst[i] - 1, i); + } + } + + solve(1); + + for (auto x : ans) cout << x << endl; + + return 0; +} diff --git a/BZOJ/2001/data/1.in b/BZOJ/2001/data/1.in new file mode 100644 index 00000000..cb8d0ddd --- /dev/null +++ b/BZOJ/2001/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:14895acdb30f07a49a9856e247b6fe108d3e51860a250a2796a395be05e45b67 +size 1355109 diff --git a/BZOJ/2001/data/1.out b/BZOJ/2001/data/1.out new file mode 100644 index 00000000..ec68fd52 --- /dev/null +++ b/BZOJ/2001/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1e2f86b632c10173febf6eeb89bbaec13fb3ce69ed60e56e9adbb6fb5006854b +size 520000 diff --git a/BZOJ/2001/data/10.in b/BZOJ/2001/data/10.in new file mode 100644 index 00000000..e586919d --- /dev/null +++ b/BZOJ/2001/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1077fba77ebf35a63f81500c53845703712693d5c1c7a41b80fd0f8efefad218 +size 948696 diff --git a/BZOJ/2001/data/10.out b/BZOJ/2001/data/10.out new file mode 100644 index 00000000..3a88ea2f --- /dev/null +++ b/BZOJ/2001/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2a2c270b8c37a7f07012add5d56cb3b9654d53148940c668c01588cb1b2423a6 +size 80000 diff --git a/BZOJ/2001/data/2.in b/BZOJ/2001/data/2.in new file mode 100644 index 00000000..d706b8c4 --- /dev/null +++ b/BZOJ/2001/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f67d81173f7a30d54dcd60ce4b8ac0df3a7576b15c4d5496ad9ce7041b74668e +size 1712494 diff --git a/BZOJ/2001/data/2.out b/BZOJ/2001/data/2.out new file mode 100644 index 00000000..436bbd4e --- /dev/null +++ b/BZOJ/2001/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:00a3267554b48408e7f02dab28f67d32aafdcd70009951fedf94eef36e934452 +size 650000 diff --git a/BZOJ/2001/data/3.in b/BZOJ/2001/data/3.in new file mode 100644 index 00000000..ad81ca33 --- /dev/null +++ b/BZOJ/2001/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:31fcdde7c67333f008aa273ee63d82b8e261b2f18c55f927d7b8026b2bd50462 +size 181141 diff --git a/BZOJ/2001/data/3.out b/BZOJ/2001/data/3.out new file mode 100644 index 00000000..d64e4eae --- /dev/null +++ b/BZOJ/2001/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cabea2839b7a7427513cde139d6b6aea2a4c54d7a0e8b13da42a1cd432933274 +size 71104 diff --git a/BZOJ/2001/data/4.in b/BZOJ/2001/data/4.in new file mode 100644 index 00000000..6e40b238 --- /dev/null +++ b/BZOJ/2001/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:65edcbf03d16f4d43b4d809a804d474490cf0eb3a74d969b7e8952608acb4c2c +size 987937 diff --git a/BZOJ/2001/data/4.out b/BZOJ/2001/data/4.out new file mode 100644 index 00000000..be5f3287 --- /dev/null +++ b/BZOJ/2001/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:82cea6c984f630bdf8c082635ccf1dc7951b748eb7407191548cdbf5f93a7107 +size 360000 diff --git a/BZOJ/2001/data/5.in b/BZOJ/2001/data/5.in new file mode 100644 index 00000000..39d35821 --- /dev/null +++ b/BZOJ/2001/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:05680087b6efdb2804a61f2ad803b4b42f729c86af764b5c5ed7df5bd1de0034 +size 753027 diff --git a/BZOJ/2001/data/5.out b/BZOJ/2001/data/5.out new file mode 100644 index 00000000..a213db7f --- /dev/null +++ b/BZOJ/2001/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:64671d6b9a1b2e45da742136277a83535737b931522e18a191162a2d27e52795 +size 65714 diff --git a/BZOJ/2001/data/6.in b/BZOJ/2001/data/6.in new file mode 100644 index 00000000..fe3f8deb --- /dev/null +++ b/BZOJ/2001/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:76a2eaa20abe3ba282a3a4c8c8485566dbcc9e6ee609b7268e282720b0bca808 +size 1712223 diff --git a/BZOJ/2001/data/6.out b/BZOJ/2001/data/6.out new file mode 100644 index 00000000..2a16cd48 --- /dev/null +++ b/BZOJ/2001/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c019051ee7f270811e68ab0265c1b5809c025d5f2048fd3b91c29154c092102f +size 650000 diff --git a/BZOJ/2001/data/7.in b/BZOJ/2001/data/7.in new file mode 100644 index 00000000..bd199aed --- /dev/null +++ b/BZOJ/2001/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:79e07dca36ce29cd86512610af0e49d00ed90564d8b12d63e15cdcd9906a1f62 +size 1712404 diff --git a/BZOJ/2001/data/7.out b/BZOJ/2001/data/7.out new file mode 100644 index 00000000..255dcc4f --- /dev/null +++ b/BZOJ/2001/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:83b6039f446c2a2df6ca5e5e7871c8b1d82e5ee0e21ef7a97d3174a33778725a +size 650000 diff --git a/BZOJ/2001/data/8.in b/BZOJ/2001/data/8.in new file mode 100644 index 00000000..e3fcb5ee --- /dev/null +++ b/BZOJ/2001/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6511bd64d8cad69facd3dfdd568e6b78595ee72f5b9b77369e5343f5fbfe7029 +size 1711892 diff --git a/BZOJ/2001/data/8.out b/BZOJ/2001/data/8.out new file mode 100644 index 00000000..d39d467a --- /dev/null +++ b/BZOJ/2001/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5901fc381c290b2bfaf659cf4a9d9eaf467364e52684f8aa783fa0c478a227be +size 650000 diff --git a/BZOJ/2001/data/9.in b/BZOJ/2001/data/9.in new file mode 100644 index 00000000..1d582ffe --- /dev/null +++ b/BZOJ/2001/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4c7420e969a20a607337117a9013bf3573ab450c3cb0e192c58cbc769f6eb0a7 +size 90001 diff --git a/BZOJ/2001/data/9.out b/BZOJ/2001/data/9.out new file mode 100644 index 00000000..59b76c44 --- /dev/null +++ b/BZOJ/2001/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cf2a31ebfd92018e9a614d923bf014b50534de42bcb9bcd2ebdbdf51785ef20c +size 36000