From 08b27afab566a0c3fc7baeed356669e1e09972e5 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 22 Dec 2022 21:16:58 +0800 Subject: [PATCH] =?UTF-8?q?2141.=20=E6=8E=92=E9=98=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://hydro.ac/d/bzoj/record/63a45898cf75a716a1672f72 --- BZOJ/2141/2141.cpp | 147 ++++++++++++++++++++++++++++++++++++++++++ BZOJ/2141/data/1.in | 3 + BZOJ/2141/data/1.out | 3 + BZOJ/2141/data/10.in | 3 + BZOJ/2141/data/10.out | 3 + BZOJ/2141/data/11.in | 3 + BZOJ/2141/data/11.out | 3 + BZOJ/2141/data/12.in | 3 + BZOJ/2141/data/12.out | 3 + BZOJ/2141/data/13.in | 3 + BZOJ/2141/data/13.out | 3 + BZOJ/2141/data/14.in | 3 + BZOJ/2141/data/14.out | 3 + BZOJ/2141/data/15.in | 3 + BZOJ/2141/data/15.out | 3 + BZOJ/2141/data/16.in | 3 + BZOJ/2141/data/16.out | 3 + BZOJ/2141/data/17.in | 3 + BZOJ/2141/data/17.out | 3 + BZOJ/2141/data/18.in | 3 + BZOJ/2141/data/18.out | 3 + BZOJ/2141/data/19.in | 3 + BZOJ/2141/data/19.out | 3 + BZOJ/2141/data/2.in | 3 + BZOJ/2141/data/2.out | 3 + BZOJ/2141/data/20.in | 3 + BZOJ/2141/data/20.out | 3 + BZOJ/2141/data/3.in | 3 + BZOJ/2141/data/3.out | 3 + BZOJ/2141/data/4.in | 3 + BZOJ/2141/data/4.out | 3 + BZOJ/2141/data/5.in | 3 + BZOJ/2141/data/5.out | 3 + BZOJ/2141/data/6.in | 3 + BZOJ/2141/data/6.out | 3 + BZOJ/2141/data/7.in | 3 + BZOJ/2141/data/7.out | 3 + BZOJ/2141/data/8.in | 3 + BZOJ/2141/data/8.out | 3 + BZOJ/2141/data/9.in | 3 + BZOJ/2141/data/9.out | 3 + 41 files changed, 267 insertions(+) create mode 100644 BZOJ/2141/2141.cpp create mode 100644 BZOJ/2141/data/1.in create mode 100644 BZOJ/2141/data/1.out create mode 100644 BZOJ/2141/data/10.in create mode 100644 BZOJ/2141/data/10.out create mode 100644 BZOJ/2141/data/11.in create mode 100644 BZOJ/2141/data/11.out create mode 100644 BZOJ/2141/data/12.in create mode 100644 BZOJ/2141/data/12.out create mode 100644 BZOJ/2141/data/13.in create mode 100644 BZOJ/2141/data/13.out create mode 100644 BZOJ/2141/data/14.in create mode 100644 BZOJ/2141/data/14.out create mode 100644 BZOJ/2141/data/15.in create mode 100644 BZOJ/2141/data/15.out create mode 100644 BZOJ/2141/data/16.in create mode 100644 BZOJ/2141/data/16.out create mode 100644 BZOJ/2141/data/17.in create mode 100644 BZOJ/2141/data/17.out create mode 100644 BZOJ/2141/data/18.in create mode 100644 BZOJ/2141/data/18.out create mode 100644 BZOJ/2141/data/19.in create mode 100644 BZOJ/2141/data/19.out create mode 100644 BZOJ/2141/data/2.in create mode 100644 BZOJ/2141/data/2.out create mode 100644 BZOJ/2141/data/20.in create mode 100644 BZOJ/2141/data/20.out create mode 100644 BZOJ/2141/data/3.in create mode 100644 BZOJ/2141/data/3.out create mode 100644 BZOJ/2141/data/4.in create mode 100644 BZOJ/2141/data/4.out create mode 100644 BZOJ/2141/data/5.in create mode 100644 BZOJ/2141/data/5.out create mode 100644 BZOJ/2141/data/6.in create mode 100644 BZOJ/2141/data/6.out create mode 100644 BZOJ/2141/data/7.in create mode 100644 BZOJ/2141/data/7.out create mode 100644 BZOJ/2141/data/8.in create mode 100644 BZOJ/2141/data/8.out create mode 100644 BZOJ/2141/data/9.in create mode 100644 BZOJ/2141/data/9.out diff --git a/BZOJ/2141/2141.cpp b/BZOJ/2141/2141.cpp new file mode 100644 index 00000000..e5821a0a --- /dev/null +++ b/BZOJ/2141/2141.cpp @@ -0,0 +1,147 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 2e4 + 5; + +int n, m, h[N], ans; +std::vector nums; + +class SegmentTree { + private: + struct node { + int l, r; + int sum; + + node *lchild, *rchild; + + node(const int &_l = 0, const int &_r = 0) + : l(_l), r(_r), sum(0), lchild(nullptr), rchild(nullptr) {} + + void pushup() { + sum = 0; + + if (lchild != nullptr) sum += lchild->sum; + if (rchild != nullptr) sum += rchild->sum; + } + }; + + node *root; + + void modify(node *&cur, int l, int r, int pos, int val) { + if (cur == nullptr) cur = new node(l, r); + + if (l == r) { + cur->sum += val; + + return; + } + + int mid = l + r >> 1; + + if (pos <= mid) modify(cur->lchild, l, mid, pos, val); + else modify(cur->rchild, mid + 1, r, pos, val); + + cur->pushup(); + } + + int query(node *cur, int l, int r, int ql, int qr) { + if (cur == nullptr) return 0; + if (ql <= l && r <= qr) return cur->sum; + + int mid = l + r >> 1, + res = 0; + + if (ql <= mid) res += query(cur->lchild, l, mid, ql, qr); + if (qr > mid) res += query(cur->rchild, mid + 1, r, ql, qr); + + return res; + } + + public: + SegmentTree() + : root(nullptr) {} + + void modify(int pos, int val) { + modify(root, 1, n, pos, val); + } + + int query(int ql, int qr) { + return query(root, 1, n, ql, qr); + } +} tr[N]; + +int lowbit(int x) { + return x & -x; +} + +void add(int x, int y, int z) { + for (; x <= n; x += lowbit(x)) tr[x].modify(y, z); +} + +int sum(int x, int l, int r) { + int res = 0; + + for (; x; x -= lowbit(x)) res += tr[x].query(l, r); + + return res; +} + +int get(int x) { + return sum(x - 1, h[x] + 1, n) + sum(n, 1, h[x] - 1) - sum(x - 1, 1, h[x] - 1); +} + +int find(int x) { + return std::lower_bound(nums.begin(), nums.end(), x) - nums.begin() + 1; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> h[i]; + + nums.emplace_back(h[i]); + } + + std::sort(nums.begin(), nums.end()); + std::transform(h + 1, h + 1 + n, h + 1, find); + + for (int i = 1; i <= n; i++) { + add(i, h[i], 1); + ans += sum(i - 1, h[i] + 1, n); + } + + cout << ans << endl; + + cin >> m; + + while (m--) { + int x, y; + + cin >> x >> y; + + if (x > y) std::swap(x, y); + if (h[x] > h[y]) ans++; + + ans -= get(x) + get(y); + add(x, h[x], -1); + add(y, h[y], -1); + std::swap(h[x], h[y]); + if (h[x] > h[y]) ans--; + add(x, h[x], 1); + add(y, h[y], 1); + ans += get(x) + get(y); + + cout << ans << endl; + } + + return 0; +} diff --git a/BZOJ/2141/data/1.in b/BZOJ/2141/data/1.in new file mode 100644 index 00000000..ff872e8d --- /dev/null +++ b/BZOJ/2141/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d4b0ad8aeea88643aa1197995f31a628304f7aa6109ee4b558e50dc5ffad382f +size 168433 diff --git a/BZOJ/2141/data/1.out b/BZOJ/2141/data/1.out new file mode 100644 index 00000000..831b7bd4 --- /dev/null +++ b/BZOJ/2141/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7199eb489bf2fde6f5c89fa5f48ebd619cc816ef4e1bcccd38fcc0d4c236502e +size 15795 diff --git a/BZOJ/2141/data/10.in b/BZOJ/2141/data/10.in new file mode 100644 index 00000000..bac8bceb --- /dev/null +++ b/BZOJ/2141/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9d334b030b3fdac3465c27e665d3b2f2d2d9206e9db685a0993ac6b3c15e31a9 +size 214828 diff --git a/BZOJ/2141/data/10.out b/BZOJ/2141/data/10.out new file mode 100644 index 00000000..ad4ed09b --- /dev/null +++ b/BZOJ/2141/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f1801e644f547ef6b942dbe9e74230f2003ffe4d6ffedae19ef769b049db6913 +size 16794 diff --git a/BZOJ/2141/data/11.in b/BZOJ/2141/data/11.in new file mode 100644 index 00000000..7c1b4c79 --- /dev/null +++ b/BZOJ/2141/data/11.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b83cfe68c1d0100e7db45e0c62d877eba98aeacc9b5e6f715100fcb3638ba1d9 +size 195111 diff --git a/BZOJ/2141/data/11.out b/BZOJ/2141/data/11.out new file mode 100644 index 00000000..57ae4df2 --- /dev/null +++ b/BZOJ/2141/data/11.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0dae584249aa21676f165588951c65618850ac505f401558aa5f175f64bed3c1 +size 12114 diff --git a/BZOJ/2141/data/12.in b/BZOJ/2141/data/12.in new file mode 100644 index 00000000..c6bd051c --- /dev/null +++ b/BZOJ/2141/data/12.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4a61ab75c02dd33afb44ac3f4da2a38c85bc5341bd222b988424571ab02e4c13 +size 209605 diff --git a/BZOJ/2141/data/12.out b/BZOJ/2141/data/12.out new file mode 100644 index 00000000..774ead8a --- /dev/null +++ b/BZOJ/2141/data/12.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8f34bcf88629c5691b045dce1eac37130fd89c6a3bb57103aff3fe12326bd0d9 +size 19000 diff --git a/BZOJ/2141/data/13.in b/BZOJ/2141/data/13.in new file mode 100644 index 00000000..0331c85b --- /dev/null +++ b/BZOJ/2141/data/13.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9e09999f4483c0c6ce6457c90749b52337e72cf9e845d6f5f712aa6c7cc55d10 +size 184945 diff --git a/BZOJ/2141/data/13.out b/BZOJ/2141/data/13.out new file mode 100644 index 00000000..76683106 --- /dev/null +++ b/BZOJ/2141/data/13.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:899e04d5e0a7877a05aea51f37dbe199a2049ce360cd79323145332dd2ecae31 +size 17001 diff --git a/BZOJ/2141/data/14.in b/BZOJ/2141/data/14.in new file mode 100644 index 00000000..f12aff59 --- /dev/null +++ b/BZOJ/2141/data/14.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7bb950becfdeeac3de5f9fe2cde14a7b39471f1e2c98afa4ee17f99d1a5f0340 +size 122769 diff --git a/BZOJ/2141/data/14.out b/BZOJ/2141/data/14.out new file mode 100644 index 00000000..06613df9 --- /dev/null +++ b/BZOJ/2141/data/14.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:485a16b63e5996a8ea55eec6a947f89c9c09554ca461ae71c17d3b5000e1370a +size 14121 diff --git a/BZOJ/2141/data/15.in b/BZOJ/2141/data/15.in new file mode 100644 index 00000000..b679e0df --- /dev/null +++ b/BZOJ/2141/data/15.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bc9c1ad014d8e033fc5ba3340247388fdff1adeb581d0788d40c84afe09b4688 +size 187 diff --git a/BZOJ/2141/data/15.out b/BZOJ/2141/data/15.out new file mode 100644 index 00000000..87536797 --- /dev/null +++ b/BZOJ/2141/data/15.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e5585ab2e5c6724fe338e6e41a564084401dc53cbc29b9cf4ccccadae6a3d5e3 +size 24 diff --git a/BZOJ/2141/data/16.in b/BZOJ/2141/data/16.in new file mode 100644 index 00000000..5f46c3a0 --- /dev/null +++ b/BZOJ/2141/data/16.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:352a2300975d3669b3adf37511313317c57f9b9be3d5c530399ea1c1b19dd51c +size 42362 diff --git a/BZOJ/2141/data/16.out b/BZOJ/2141/data/16.out new file mode 100644 index 00000000..325702e4 --- /dev/null +++ b/BZOJ/2141/data/16.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6071592ba6250a6dfa12ca9c7c0515c1cbf28f32457d1492be496f94591b2008 +size 14328 diff --git a/BZOJ/2141/data/17.in b/BZOJ/2141/data/17.in new file mode 100644 index 00000000..1c1ecff0 --- /dev/null +++ b/BZOJ/2141/data/17.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:51d6249df6da39d9402fb9c92b0ce12fa8c645f4eb412cbc0bc5a1bac7b4b454 +size 87493 diff --git a/BZOJ/2141/data/17.out b/BZOJ/2141/data/17.out new file mode 100644 index 00000000..6cfdc468 --- /dev/null +++ b/BZOJ/2141/data/17.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e6cf2424bbf3422cb5fa3648475bd40496955e8322cb577df439bd4535e340a9 +size 15120 diff --git a/BZOJ/2141/data/18.in b/BZOJ/2141/data/18.in new file mode 100644 index 00000000..598ecfc9 --- /dev/null +++ b/BZOJ/2141/data/18.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:898a17840d99a28b3dfae4df28608c4c46238bd5ef9c39a7324e22a9dbf4251a +size 209694 diff --git a/BZOJ/2141/data/18.out b/BZOJ/2141/data/18.out new file mode 100644 index 00000000..02df25af --- /dev/null +++ b/BZOJ/2141/data/18.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d4abfafa2056c5fab3b4b239c063188be03854c160b0a312b453e261fde3a066 +size 11451 diff --git a/BZOJ/2141/data/19.in b/BZOJ/2141/data/19.in new file mode 100644 index 00000000..3ad06162 --- /dev/null +++ b/BZOJ/2141/data/19.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:357049296d1af0ce819688ddd56f8614ee4d7615291f3ca5a3afc1c9d3f9ed0b +size 215014 diff --git a/BZOJ/2141/data/19.out b/BZOJ/2141/data/19.out new file mode 100644 index 00000000..e3acb14b --- /dev/null +++ b/BZOJ/2141/data/19.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:87885749ef014df9206e0920867109e5eff5dc70362dcc487a646e305bdf3fba +size 18000 diff --git a/BZOJ/2141/data/2.in b/BZOJ/2141/data/2.in new file mode 100644 index 00000000..ae488c99 --- /dev/null +++ b/BZOJ/2141/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:344b49a740c1dc857bf5b94015010090a79c74516a15db1f6757e44bfedbcd61 +size 184 diff --git a/BZOJ/2141/data/2.out b/BZOJ/2141/data/2.out new file mode 100644 index 00000000..99016905 --- /dev/null +++ b/BZOJ/2141/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5205ee0ef2aafea968159d7e53929aed2a4d557f82616ad25cb4419681f2a875 +size 30 diff --git a/BZOJ/2141/data/20.in b/BZOJ/2141/data/20.in new file mode 100644 index 00000000..23ece8be --- /dev/null +++ b/BZOJ/2141/data/20.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:183e61d2cfbb1e10442cca6f0bb19df5a80f5ef43fac268f465e484dcff96c1b +size 197151 diff --git a/BZOJ/2141/data/20.out b/BZOJ/2141/data/20.out new file mode 100644 index 00000000..05830a55 --- /dev/null +++ b/BZOJ/2141/data/20.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8da698bd57fa8029b0aea8d0c2aefa717b6cf4e7b7bc0635a9260399cb0f38c1 +size 17919 diff --git a/BZOJ/2141/data/3.in b/BZOJ/2141/data/3.in new file mode 100644 index 00000000..ac6b80eb --- /dev/null +++ b/BZOJ/2141/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e02940cf6784d695d03585de995d745bcc37654e979e40885dec292db3e98871 +size 50995 diff --git a/BZOJ/2141/data/3.out b/BZOJ/2141/data/3.out new file mode 100644 index 00000000..7ab0dc28 --- /dev/null +++ b/BZOJ/2141/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ce0776f4d3fc69db486a4669fd3bcb865ed865450b20e1c5f7de02df024658f7 +size 12352 diff --git a/BZOJ/2141/data/4.in b/BZOJ/2141/data/4.in new file mode 100644 index 00000000..addb64d3 --- /dev/null +++ b/BZOJ/2141/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c43652a02c7d53972fcceeec82500f359b76bff571a335aff591e3a1293435e2 +size 141630 diff --git a/BZOJ/2141/data/4.out b/BZOJ/2141/data/4.out new file mode 100644 index 00000000..19a24ee1 --- /dev/null +++ b/BZOJ/2141/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a9a961e15a30b41f2e56dd7d147a16d60d24b2802e7e2330ac59a01dc61105d9 +size 18009 diff --git a/BZOJ/2141/data/5.in b/BZOJ/2141/data/5.in new file mode 100644 index 00000000..310f3a1e --- /dev/null +++ b/BZOJ/2141/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:73d1c960ae2795b9dc6091cef13b8668bd8cfe9524827d6d37cd31d712684002 +size 3109 diff --git a/BZOJ/2141/data/5.out b/BZOJ/2141/data/5.out new file mode 100644 index 00000000..fb3d76e2 --- /dev/null +++ b/BZOJ/2141/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6acad52707f59016de5870d52eca2e6dffcaae605a877541921c0fc7926a6b76 +size 876 diff --git a/BZOJ/2141/data/6.in b/BZOJ/2141/data/6.in new file mode 100644 index 00000000..62385cf3 --- /dev/null +++ b/BZOJ/2141/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e14a59cd2face015b3eeb133bcdc302497a8fd70b0f93666eec00dc8f39c41d2 +size 101548 diff --git a/BZOJ/2141/data/6.out b/BZOJ/2141/data/6.out new file mode 100644 index 00000000..b0bff05b --- /dev/null +++ b/BZOJ/2141/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:faa2b49e98aec2aa7b667fb3de2471c1eea89fdbdb2c4f83db6eb1b538a8abe0 +size 17793 diff --git a/BZOJ/2141/data/7.in b/BZOJ/2141/data/7.in new file mode 100644 index 00000000..56ba5939 --- /dev/null +++ b/BZOJ/2141/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0f3422cc98c8931c32b1e02ced6f959caacba61f36e91803898332b9aebe1ca3 +size 67726 diff --git a/BZOJ/2141/data/7.out b/BZOJ/2141/data/7.out new file mode 100644 index 00000000..b2133ef9 --- /dev/null +++ b/BZOJ/2141/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:309a839ed06da2f95fd437bbaa2f7af41bce4eeec78e4554bc6138a94a6db307 +size 12544 diff --git a/BZOJ/2141/data/8.in b/BZOJ/2141/data/8.in new file mode 100644 index 00000000..fa535eef --- /dev/null +++ b/BZOJ/2141/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1e4694f5f4d75eb247b2322c8c7e11a1e0776e3575811dbd3d8ff76c3ae22a37 +size 3306 diff --git a/BZOJ/2141/data/8.out b/BZOJ/2141/data/8.out new file mode 100644 index 00000000..6d13ca29 --- /dev/null +++ b/BZOJ/2141/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b35874d0dede879ad8400d2282cf8c98cde376ef47e28dd4c1803e6ce34ca9f8 +size 1037 diff --git a/BZOJ/2141/data/9.in b/BZOJ/2141/data/9.in new file mode 100644 index 00000000..372801fb --- /dev/null +++ b/BZOJ/2141/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b43caf050a70fdac41d5a279775dd3e70b91bf18edc4162814260fe7134d77f3 +size 3087 diff --git a/BZOJ/2141/data/9.out b/BZOJ/2141/data/9.out new file mode 100644 index 00000000..593e9c0f --- /dev/null +++ b/BZOJ/2141/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:612f17a98600b6ed3f359ecdab65a1ef2d8ca7c5afda0e425ab27630cf7be8e8 +size 945