diff --git a/BZOJ/2821/2821.cpp b/BZOJ/2821/2821.cpp new file mode 100644 index 00000000..62add4f3 --- /dev/null +++ b/BZOJ/2821/2821.cpp @@ -0,0 +1,111 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +constexpr int N = 1e5 + 5, + M = 1005; + +int n, c, m, a[N], cnt[M][N], ans[M][N], res; +int t, b, st[N], ed[N], pos[N], h[N]; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> c >> m; + + t = std::sqrt(n); + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + + for (b = 1;; b++) { + st[b] = (b - 1) * t + 1; + ed[b] = std::min(n, b * t); + + if (b * t >= n) break; + } + + for (int i = 1; i <= b; i++) { + for (int j = st[i]; j <= ed[i]; j++) { + pos[j] = i; + cnt[i][a[j]]++; + } + } + + for (int i = 1; i <= b; i++) { + for (int j = 0; j <= c; j++) { + cnt[i][j] += cnt[i - 1][j]; + } + } + + for (int i = 1; i <= b; i++) { + for (int j = i; j <= b; j++) { + ans[i][j] = ans[i][j - 1]; + + for (int k = st[j]; k <= ed[j]; k++) { + h[a[k]]++; + + if (h[a[k]] % 2 == 0) ans[i][j]++; + else if (h[a[k]] >= 3) ans[i][j]--; + } + } + + memset(h, 0x00, sizeof(h)); + } + + while (m--) { + int l, r; + + cin >> l >> r; + + l = (l + res) % n + 1; + r = (r + res) % n + 1; + if (l > r) std::swap(l, r); + + if (pos[l] == pos[r]) { + res = 0; + + for (int i = l; i <= r; i++) { + h[a[i]]++; + if (h[a[i]] % 2 == 0) res++; + else if (h[a[i]] >= 3) res--; + } + + for (int i = l; i <= r; i++) h[a[i]]--; + + } else { + res = ans[pos[l] + 1][pos[r] - 1]; + + for (int i = l; i <= ed[pos[l]]; i++) { + h[a[i]]++; + + int x = cnt[pos[r] - 1][a[i]] - cnt[pos[l]][a[i]]; + + if ((h[a[i]] + x) % 2 == 0) res++; + else if (h[a[i]] + x >= 3) res--; + } + + for (int i = st[pos[r]]; i <= r; i++) { + h[a[i]]++; + + int x = cnt[pos[r] - 1][a[i]] - cnt[pos[l]][a[i]]; + + if ((h[a[i]] + x) % 2 == 0) res++; + else if (h[a[i]] + x >= 3) res--; + } + + for (int i = l; i <= ed[pos[l]]; i++) h[a[i]]--; + for (int i = st[pos[r]]; i <= r; i++) h[a[i]]--; + } + + cout << res << endl; + } + + return 0; +} diff --git a/BZOJ/2821/data/1.in b/BZOJ/2821/data/1.in new file mode 100644 index 00000000..b950a1cc --- /dev/null +++ b/BZOJ/2821/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:02a9be93b2ba69155f8d97ab85d3e47b4ce46577f957de997f33786ad6699fdd +size 1722366 diff --git a/BZOJ/2821/data/1.out b/BZOJ/2821/data/1.out new file mode 100644 index 00000000..93fe9944 --- /dev/null +++ b/BZOJ/2821/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:52978d4601d5082a3bbfe9d7fa54e4d4d87284ae1f8e05d9858d88efbf173f31 +size 481988 diff --git a/BZOJ/2821/data/10.in b/BZOJ/2821/data/10.in new file mode 100644 index 00000000..c7b3abc8 --- /dev/null +++ b/BZOJ/2821/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ab95d666264d88fac793b62bc813dfe898bb4fbac5651e881ca061300f18a9fe +size 1666599 diff --git a/BZOJ/2821/data/10.out b/BZOJ/2821/data/10.out new file mode 100644 index 00000000..16d718f9 --- /dev/null +++ b/BZOJ/2821/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b5b34667ee103025e2e4f718c1021e23afb7330b555004eee62cc9f292742e18 +size 484916 diff --git a/BZOJ/2821/data/2.in b/BZOJ/2821/data/2.in new file mode 100644 index 00000000..de9d424b --- /dev/null +++ b/BZOJ/2821/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0dc27472b32617b5e7c436dfdda5b4661f0318432d97ae56f1f6c233d3b9a481 +size 562239 diff --git a/BZOJ/2821/data/2.out b/BZOJ/2821/data/2.out new file mode 100644 index 00000000..b5c6fd19 --- /dev/null +++ b/BZOJ/2821/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6d3a0579291d3711ffc60ca44763d8e8e2a9267e730d0c1d13d1c6f0b286c6ec +size 58614 diff --git a/BZOJ/2821/data/3.in b/BZOJ/2821/data/3.in new file mode 100644 index 00000000..01882d86 --- /dev/null +++ b/BZOJ/2821/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e8625004c3f44c9cb0ba887115416fe329eb1500f981ef20f4ac708166abfe48 +size 1766040 diff --git a/BZOJ/2821/data/3.out b/BZOJ/2821/data/3.out new file mode 100644 index 00000000..69baeafb --- /dev/null +++ b/BZOJ/2821/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:86fd3af7b4bceddbe2277a24368b40ad56dda8ea730634d0ac9b6477b13ac37d +size 482614 diff --git a/BZOJ/2821/data/4.in b/BZOJ/2821/data/4.in new file mode 100644 index 00000000..59087fc7 --- /dev/null +++ b/BZOJ/2821/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:14cbd47358d47c5baedaae1efeb85138fe61c735bc0dc21f9a01d3c76d87b0c9 +size 1565973 diff --git a/BZOJ/2821/data/4.out b/BZOJ/2821/data/4.out new file mode 100644 index 00000000..02b430af --- /dev/null +++ b/BZOJ/2821/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f8cb467ebb721a8920d1bc595cf6218481a6a7f4dde98a40b6221e7e132380b1 +size 398525 diff --git a/BZOJ/2821/data/5.in b/BZOJ/2821/data/5.in new file mode 100644 index 00000000..6cf682e5 --- /dev/null +++ b/BZOJ/2821/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dba582f77ea0f0eeccc2bf59209e2129092a5677b339c2b23b6424d1fb2281e1 +size 1655113 diff --git a/BZOJ/2821/data/5.out b/BZOJ/2821/data/5.out new file mode 100644 index 00000000..3f3e7cd4 --- /dev/null +++ b/BZOJ/2821/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:94f202150cc8bfd37c23656300cd6e2044d07bb08a03d23d98c4c837978683b2 +size 486111 diff --git a/BZOJ/2821/data/6.in b/BZOJ/2821/data/6.in new file mode 100644 index 00000000..bc407178 --- /dev/null +++ b/BZOJ/2821/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7b48472a9ee6adb31f74cec59e684ec1fac36a2ab2cf140b1d727c59c0966493 +size 1680885 diff --git a/BZOJ/2821/data/6.out b/BZOJ/2821/data/6.out new file mode 100644 index 00000000..6cac8b90 --- /dev/null +++ b/BZOJ/2821/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b0da73f06bdb76bed3e9a6f5b0570cf5f485a0e8c6d5451ca0a0121e32238a23 +size 484305 diff --git a/BZOJ/2821/data/7.in b/BZOJ/2821/data/7.in new file mode 100644 index 00000000..1b7e085d --- /dev/null +++ b/BZOJ/2821/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:05f937b670fc63cfb1c5a218b337139b1a3d62580e7b00148bade07b3d68bbab +size 1666448 diff --git a/BZOJ/2821/data/7.out b/BZOJ/2821/data/7.out new file mode 100644 index 00000000..598147c8 --- /dev/null +++ b/BZOJ/2821/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:58f6f8315a6c4ea9f3148c2a3d170a2732ce5ed33ebe8b723ea19a75add5524c +size 484884 diff --git a/BZOJ/2821/data/8.in b/BZOJ/2821/data/8.in new file mode 100644 index 00000000..9110bf10 --- /dev/null +++ b/BZOJ/2821/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9b937a52416001f714da072bbb278cd82a8db6fc0d19579a8130031e6700c5c3 +size 1666671 diff --git a/BZOJ/2821/data/8.out b/BZOJ/2821/data/8.out new file mode 100644 index 00000000..5b96b61c --- /dev/null +++ b/BZOJ/2821/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:aff397d5385985976c51aa104fb3b0a893710aac934d45e7b94a50176eb1b456 +size 485061 diff --git a/BZOJ/2821/data/9.in b/BZOJ/2821/data/9.in new file mode 100644 index 00000000..63c08037 --- /dev/null +++ b/BZOJ/2821/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b0001843e3719b096b8703ffdd0d93b970ff4b62485943ac7abe2aaba462aeec +size 1755294 diff --git a/BZOJ/2821/data/9.out b/BZOJ/2821/data/9.out new file mode 100644 index 00000000..9ac61b76 --- /dev/null +++ b/BZOJ/2821/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a5b91ea5b3f94d3c8bb016768a659b70fb517cf7aa4e338f510af40941f94ccc +size 491422