From 419289fb94eb2c3207116945b8b1ac654e33e151 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 15 Jul 2022 22:02:13 +0800 Subject: [PATCH] =?UTF-8?q?2002.=20[HNOI2010]=20Bounce=20=E5=BC=B9?= =?UTF-8?q?=E9=A3=9E=E7=BB=B5=E7=BE=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://hydro.ac/d/bzoj/record/62d1735ab36e4a12382a3e0d --- BZOJ/2002/2002.cpp | 189 ++++++++++++++++++++++++++++++++++++++++++ BZOJ/2002/data/1.in | 3 + BZOJ/2002/data/1.out | 3 + BZOJ/2002/data/10.in | 3 + BZOJ/2002/data/10.out | 3 + BZOJ/2002/data/2.in | 3 + BZOJ/2002/data/2.out | 3 + BZOJ/2002/data/3.in | 3 + BZOJ/2002/data/3.out | 3 + BZOJ/2002/data/4.in | 3 + BZOJ/2002/data/4.out | 3 + BZOJ/2002/data/5.in | 3 + BZOJ/2002/data/5.out | 3 + BZOJ/2002/data/6.in | 3 + BZOJ/2002/data/6.out | 3 + BZOJ/2002/data/7.in | 3 + BZOJ/2002/data/7.out | 3 + BZOJ/2002/data/8.in | 3 + BZOJ/2002/data/8.out | 3 + BZOJ/2002/data/9.in | 3 + BZOJ/2002/data/9.out | 3 + 21 files changed, 249 insertions(+) create mode 100644 BZOJ/2002/2002.cpp create mode 100644 BZOJ/2002/data/1.in create mode 100644 BZOJ/2002/data/1.out create mode 100644 BZOJ/2002/data/10.in create mode 100644 BZOJ/2002/data/10.out create mode 100644 BZOJ/2002/data/2.in create mode 100644 BZOJ/2002/data/2.out create mode 100644 BZOJ/2002/data/3.in create mode 100644 BZOJ/2002/data/3.out create mode 100644 BZOJ/2002/data/4.in create mode 100644 BZOJ/2002/data/4.out create mode 100644 BZOJ/2002/data/5.in create mode 100644 BZOJ/2002/data/5.out create mode 100644 BZOJ/2002/data/6.in create mode 100644 BZOJ/2002/data/6.out create mode 100644 BZOJ/2002/data/7.in create mode 100644 BZOJ/2002/data/7.out create mode 100644 BZOJ/2002/data/8.in create mode 100644 BZOJ/2002/data/8.out create mode 100644 BZOJ/2002/data/9.in create mode 100644 BZOJ/2002/data/9.out diff --git a/BZOJ/2002/2002.cpp b/BZOJ/2002/2002.cpp new file mode 100644 index 00000000..1b4bd5e6 --- /dev/null +++ b/BZOJ/2002/2002.cpp @@ -0,0 +1,189 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 2e5 + 5; + +int n, m, a[N]; + +class LinkCutTree { + private: + struct node { + size_t l, r, f; + unsigned s; + bool rev; + + node() + : l(0), r(0), f(0), s(0), rev(false) {} + + node(size_t _f) + : l(0), r(0), f(_f), s(1), 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 + 1 + 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: + unsigned query(int x, int y) { + split(x, y); + + return tr[y].s; + } + + 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; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + + lct.link(i, std::min(i + a[i], n + 1)); + } + + cin >> m; + + while (m--) { + int op, x, y; + + cin >> op; + + if (op == 1) { + cin >> x; + + cout << lct.query(x + 1, n + 1) - 1 << endl; + } else { // op == 2 + cin >> x >> y; + + x++; + lct.cut(x, std::min(x + a[x], n + 1)); + lct.link(x, std::min(x + (a[x] = y), n + 1)); + } + } + + return 0; +} diff --git a/BZOJ/2002/data/1.in b/BZOJ/2002/data/1.in new file mode 100644 index 00000000..9e6b2ce6 --- /dev/null +++ b/BZOJ/2002/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8b4a30f5970ff8eb6203b7601bfe1e7d8ae1d1bcd92c756e8eba9502f531c0ad +size 1106122 diff --git a/BZOJ/2002/data/1.out b/BZOJ/2002/data/1.out new file mode 100644 index 00000000..133ccc8c --- /dev/null +++ b/BZOJ/2002/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0ceee89c693cb8769b67fbe472a37cc786fb3bd4c16bf99dc48cb435986c0118 +size 250065 diff --git a/BZOJ/2002/data/10.in b/BZOJ/2002/data/10.in new file mode 100644 index 00000000..a6f29866 --- /dev/null +++ b/BZOJ/2002/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cebe80660e74d3939b9b89d47d494f8533820f54b7bcc9a80836e68506de78b2 +size 1141481 diff --git a/BZOJ/2002/data/10.out b/BZOJ/2002/data/10.out new file mode 100644 index 00000000..ef2ba7db --- /dev/null +++ b/BZOJ/2002/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:574260fe0997140946fb1235b34c1db4c89556f99a4283edb33ad8dff9ced3d4 +size 250295 diff --git a/BZOJ/2002/data/2.in b/BZOJ/2002/data/2.in new file mode 100644 index 00000000..57dcf78e --- /dev/null +++ b/BZOJ/2002/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d85c41728b2454613b12def3ee9afa9b93b65ffe09d517bee6d6b60c18320858 +size 1066339 diff --git a/BZOJ/2002/data/2.out b/BZOJ/2002/data/2.out new file mode 100644 index 00000000..504958d2 --- /dev/null +++ b/BZOJ/2002/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:476e7597430add02665790aeeea424474f0c3c12bd65cf694d3bdc935ba24f8e +size 300078 diff --git a/BZOJ/2002/data/3.in b/BZOJ/2002/data/3.in new file mode 100644 index 00000000..7f44944c --- /dev/null +++ b/BZOJ/2002/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c3e883e543b62824fe5179ad18dbea4d042e7ee8cfd5816fb87bc519215ed1e8 +size 1075377 diff --git a/BZOJ/2002/data/3.out b/BZOJ/2002/data/3.out new file mode 100644 index 00000000..bc441440 --- /dev/null +++ b/BZOJ/2002/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:aad6c9603ef141cdaefdf81ced5e66922edb7a25312ec0a99725bcd69abcf80f +size 299832 diff --git a/BZOJ/2002/data/4.in b/BZOJ/2002/data/4.in new file mode 100644 index 00000000..6158d84b --- /dev/null +++ b/BZOJ/2002/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e13319277e71e87d9d821e7d59b1c87999b33f679542a3eea047b9eeeaea07ec +size 1421232 diff --git a/BZOJ/2002/data/4.out b/BZOJ/2002/data/4.out new file mode 100644 index 00000000..bbb4f514 --- /dev/null +++ b/BZOJ/2002/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:902077a2d6726a77379716a72ed7f1c755868f23d64d437b7e7b816260e5d5f2 +size 249940 diff --git a/BZOJ/2002/data/5.in b/BZOJ/2002/data/5.in new file mode 100644 index 00000000..f7d34b4d --- /dev/null +++ b/BZOJ/2002/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7b3ad1e2a7def96007651d44c0f16ed7ade319b2c0f48de633f46e5b81d2751b +size 812752 diff --git a/BZOJ/2002/data/5.out b/BZOJ/2002/data/5.out new file mode 100644 index 00000000..4c7561b9 --- /dev/null +++ b/BZOJ/2002/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:739f5fe245c4446cea10863cf9b38e8ba2e7cfb26726ea497565fed1d41e3048 +size 240156 diff --git a/BZOJ/2002/data/6.in b/BZOJ/2002/data/6.in new file mode 100644 index 00000000..47cc36b8 --- /dev/null +++ b/BZOJ/2002/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e1adbd16ae0a3b69db988e4c7974ca8e5af3eb3e9bbdfc1ec435c5158e990515 +size 138438 diff --git a/BZOJ/2002/data/6.out b/BZOJ/2002/data/6.out new file mode 100644 index 00000000..73abeefa --- /dev/null +++ b/BZOJ/2002/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6670630816b3821b80db820d870d60d8f932a8438db3b6154dc463a5d450a986 +size 10120 diff --git a/BZOJ/2002/data/7.in b/BZOJ/2002/data/7.in new file mode 100644 index 00000000..bdee9b00 --- /dev/null +++ b/BZOJ/2002/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e41a8e1c01477c9a886ed800869fd2bb5d40c2b485117e46e8fb14596d19971f +size 980036 diff --git a/BZOJ/2002/data/7.out b/BZOJ/2002/data/7.out new file mode 100644 index 00000000..6763b88b --- /dev/null +++ b/BZOJ/2002/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5f89a3ac7401b823e10352a0632455c2701426323686f5c6de182cd27e8142a1 +size 250420 diff --git a/BZOJ/2002/data/8.in b/BZOJ/2002/data/8.in new file mode 100644 index 00000000..00bbc447 --- /dev/null +++ b/BZOJ/2002/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:00a71d7aa71bd8107b138d70a85d900c7053503ae2c8b5059718300072927d8f +size 138739 diff --git a/BZOJ/2002/data/8.out b/BZOJ/2002/data/8.out new file mode 100644 index 00000000..55149312 --- /dev/null +++ b/BZOJ/2002/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:500303fcbeb7b3cf4c86d2a16b24fdc85fd350788e52a87a69527ab50b81161f +size 9971 diff --git a/BZOJ/2002/data/9.in b/BZOJ/2002/data/9.in new file mode 100644 index 00000000..ccec185c --- /dev/null +++ b/BZOJ/2002/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ba5feb22a0dada77848218d4d7846f81ea8a47729e9edd892b43527f016ac8d6 +size 1265975 diff --git a/BZOJ/2002/data/9.out b/BZOJ/2002/data/9.out new file mode 100644 index 00000000..aec81ca6 --- /dev/null +++ b/BZOJ/2002/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:89097a36d9200aaf0790a42500361860dc587cffec443e3a372faf71bba9ec82 +size 350049