0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-03 19:16:27 +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]; const int N = 2e5 + 5;
int n, m, x;
char op;
bool flag;
void change(int u) { int m, d, cnt;
f[u][0] = a[u]; long long t;
for (int i = 1; u - (1 << i) >= 0; i++) {
f[u][i] = max(f[u][i - 1], f[u - (1 << (i - 1))][i - 1]); 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) { void build(int u, int l, int r) {
int d = (int)(log(y - x + 1) / log(2)); tr[u].l = l;
return max(f[y][d], f[x + (1 << d) - 1][d]); 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() { int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> m >> d; cin >> m >> d;
build(1, 1, m);
while (m--) { while (m--) {
cin >> op >> x; char op;
if (op == 'A') {
a[++n] = (x + t) % d; cin >> op;
change(n);
} else if (op == 'Q') { if (op == 'Q') {
if (x == 1) { int l;
cout << (t = a[n]) << endl;
} else { cin >> l;
cout << (t = find(n - x + 1, n)) << endl;
} 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; return 0;
} }