0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00

P1198 [JSOI2008] 最大数

https://www.luogu.com.cn/record/168534477
This commit is contained in:
Baoshuo Ren 2024-07-25 19:30:19 +08:00
parent cb44458fea
commit e4682a81a7
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -1,38 +1,97 @@
#include <bits/stdc++.h>
#include <iostream>
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;
}