From b2241462afb6333f826914e1fd9b23a27e61a6d0 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 17 Aug 2022 19:41:41 +0800 Subject: [PATCH] =?UTF-8?q?P4135=20=E4=BD=9C=E8=AF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/84184013 --- Luogu/P4135/P4135.cpp | 111 +++++++++++++++++++++++++++++++++++ Luogu/P4135/data/P4135_2.in | 3 + Luogu/P4135/data/P4135_2.out | 3 + 3 files changed, 117 insertions(+) create mode 100644 Luogu/P4135/P4135.cpp create mode 100644 Luogu/P4135/data/P4135_2.in create mode 100644 Luogu/P4135/data/P4135_2.out diff --git a/Luogu/P4135/P4135.cpp b/Luogu/P4135/P4135.cpp new file mode 100644 index 00000000..62add4f3 --- /dev/null +++ b/Luogu/P4135/P4135.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/Luogu/P4135/data/P4135_2.in b/Luogu/P4135/data/P4135_2.in new file mode 100644 index 00000000..d5a52f7b --- /dev/null +++ b/Luogu/P4135/data/P4135_2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:55eb135873830d5f3a7f39a82fd7d30ffe3a6e5724ba10f15f8daea8a2a64f63 +size 1666672 diff --git a/Luogu/P4135/data/P4135_2.out b/Luogu/P4135/data/P4135_2.out new file mode 100644 index 00000000..99539972 --- /dev/null +++ b/Luogu/P4135/data/P4135_2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3d55e1506fbcfcd78515ffb49c19252037179616b8948696224bafc1944c96fc +size 585061