From e4682a81a794bc36a0856b4baf12b3f4c12dffb0 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 25 Jul 2024 19:30:19 +0800 Subject: [PATCH] =?UTF-8?q?P1198=20[JSOI2008]=20=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/168534477 --- Luogu/P1198/P1198.cpp | 107 ++++++++++++++++++++++++++++++++---------- 1 file changed, 83 insertions(+), 24 deletions(-) diff --git a/Luogu/P1198/P1198.cpp b/Luogu/P1198/P1198.cpp index 76f3ac02..018f7530 100644 --- a/Luogu/P1198/P1198.cpp +++ b/Luogu/P1198/P1198.cpp @@ -1,38 +1,97 @@ -#include +#include -using namespace std; +using std::cin; +using std::cout; +const char endl = '\n'; -long long t, d, a[200005], f[200005][25]; -int n, m, x; -char op; -bool flag; +const int N = 2e5 + 5; -void change(int u) { - f[u][0] = a[u]; - for (int i = 1; u - (1 << i) >= 0; i++) { - f[u][i] = max(f[u][i - 1], f[u - (1 << (i - 1))][i - 1]); - } +int m, d, cnt; +long long t; + +struct node { + int l, r; + long long max; +} tr[N << 2]; + +void pushup(int u) { + tr[u].max = std::max(tr[u << 1].max, tr[u << 1 | 1].max); } -long long find(int x, int y) { - int d = (int)(log(y - x + 1) / log(2)); - return max(f[y][d], f[x + (1 << d) - 1][d]); +void build(int u, int l, int r) { + tr[u].l = l; + tr[u].r = r; + + if (l == r) { + tr[u].max = 0; + + return; + } + + int mid = (l + r) >> 1; + + build(u << 1, l, mid); // [l, mid] + build(u << 1 | 1, mid + 1, r); // [mid + 1, r] + + pushup(u); +} + +void modify(int u, int pos, long long val) { + if (tr[u].l == pos && tr[u].r == pos) { + tr[u].max = val; + + return; + } + + int mid = (tr[u].l + tr[u].r) >> 1; + + if (pos <= mid) modify(u << 1, pos, val); + else modify(u << 1 | 1, pos, val); + + pushup(u); +} + +long long query(int u, int l, int r) { + if (l <= tr[u].l && tr[u].r <= r) return tr[u].max; + + int mid = (tr[u].l + tr[u].r) >> 1; + long long res = 0; + + if (l <= mid) res = std::max(res, query(u << 1, l, r)); + if (r > mid) res = std::max(res, query(u << 1 | 1, l, r)); + + return res; } int main() { + std::ios::sync_with_stdio(false); + cin.tie(NULL); + cin >> m >> d; + + build(1, 1, m); + while (m--) { - cin >> op >> x; - if (op == 'A') { - a[++n] = (x + t) % d; - change(n); - } else if (op == 'Q') { - if (x == 1) { - cout << (t = a[n]) << endl; - } else { - cout << (t = find(n - x + 1, n)) << endl; - } + char op; + + cin >> op; + + if (op == 'Q') { + int l; + + cin >> l; + + t = query(1, cnt - l + 1, cnt); + + cout << t << endl; + } else { // op == 2 + long long x; + + cin >> x; + + modify(1, ++cnt, ((t + x) % d + d) % d); } } + return 0; }