From 713585bf36b7e6698d90112715265380095b565f Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 18 May 2022 07:15:08 +0800 Subject: [PATCH] =?UTF-8?q?P2801=20=E6=95=99=E4=B8=BB=E7=9A=84=E9=AD=94?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/75948111 --- Luogu/P2801/P2801.cpp | 101 +++++++++++++++++++++++++++++++++++ Luogu/P2801/data/P2801_1.in | 3 ++ Luogu/P2801/data/P2801_1.out | 3 ++ 3 files changed, 107 insertions(+) create mode 100644 Luogu/P2801/P2801.cpp create mode 100644 Luogu/P2801/data/P2801_1.in create mode 100644 Luogu/P2801/data/P2801_1.out diff --git a/Luogu/P2801/P2801.cpp b/Luogu/P2801/P2801.cpp new file mode 100644 index 00000000..f7945d0a --- /dev/null +++ b/Luogu/P2801/P2801.cpp @@ -0,0 +1,101 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e6 + 5; + +int n, q, t, c, a[N], b[N], st[N], ed[N], pos[N], add[N]; + +inline void process(int x) { + for (int i = st[x]; i <= ed[x]; i++) b[i] = a[i]; + std::sort(b + st[x], b + ed[x] + 1); +} + +void change(int l, int r, int c) { + int p = pos[l], + q = pos[r]; + + if (p == q) { + for (int i = l; i <= r; i++) a[i] += c; + process(p); + + return; + } + + for (int i = l; i <= ed[p]; i++) a[i] += c; + process(p); + for (int i = st[q]; i <= r; i++) a[i] += c; + process(q); + for (int i = p + 1; i <= q - 1; i++) add[i] += c; +} + +int query(int l, int r, int c) { + int p = pos[l], + q = pos[r]; + int cnt = 0; + + if (p == q) { + for (int i = l; i <= r; i++) { + if (a[i] + add[p] < c) cnt++; + } + + return cnt; + } + + for (int i = l; i <= ed[p]; i++) { + if (a[i] + add[p] < c) cnt++; + } + for (int i = st[q]; i <= r; i++) { + if (a[i] + add[q] < c) cnt++; + } + for (int i = p + 1; i <= q - 1; i++) { + cnt += std::lower_bound(b + st[i], b + ed[i] + 1, c - add[i]) - b - st[i]; + } + + return cnt; +} + +int main() { + std::ios::sync_with_stdio(false); + + cin >> n >> q; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + b[i] = a[i]; + } + + t = std::sqrt(n); + + for (c = 1;; c++) { + st[c] = (c - 1) * t + 1; + ed[c] = std::min(n, c * t); + + if (c * t >= n) break; + } + + for (int i = 1; i <= c; i++) { + for (int j = st[i]; j <= ed[i]; j++) { + pos[j] = i; + } + std::sort(b + st[i], b + ed[i] + 1); + } + + while (q--) { + char op; + int l, r, w; + cin >> op >> l >> r >> w; + + if (op == 'M') { + change(l, r, w); + } else { // op == 'A' + cout << r - l + 1 - query(l, r, w) << endl; + } + } + + return 0; +} diff --git a/Luogu/P2801/data/P2801_1.in b/Luogu/P2801/data/P2801_1.in new file mode 100644 index 00000000..2f673288 --- /dev/null +++ b/Luogu/P2801/data/P2801_1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4e84f6f2be68265a7b39f9d9d9b59078636eb979dbbc0a7c65222b40d31c614e +size 151 diff --git a/Luogu/P2801/data/P2801_1.out b/Luogu/P2801/data/P2801_1.out new file mode 100644 index 00000000..f3f205a4 --- /dev/null +++ b/Luogu/P2801/data/P2801_1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9d1d5ca3a7aac275074dc875647edc9d0c3de810f04fa7799ae7885c35a3f7dd +size 21