1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include <iostream> #include <cstdio> #define MAX_N 200000 #define INF 2000000000 #define ll long long using namespace std; ll tree[MAX_N*4+50]; int n, len = 0; void updata(int v) { tree[v] = max(tree[v<<1], tree[v<<1|1]); } void build(int v, int s, int t) { if (s == t) { tree[v] = -INF; return; } int mid = s+t>>1; build(v<<1, s, mid); build(v<<1|1, mid+1, t); updata(v); } void modify(int v, int s, int t, int pos, ll x) { if (s == t) { tree[v] = x; return; } int mid = s+t>>1; if (pos <= mid) { modify(v<<1, s, mid, pos, x); } else { modify(v<<1|1, mid+1, t, pos, x); } updata(v); } ll query(int v, int s, int t, int l, int r) { if (s >= l && t <= r) { return tree[v]; } int mid = s+t>>1; ll ret = -INF; if (l <= mid) { ret = max(ret, query(v<<1, s, mid, l, r)); } if (r >= mid+1) { ret = max(ret, query(v<<1|1, mid+1, t, l, r)); } return ret; } int main() { ll x, prev = 0, mod; char opt; int l; scanf("%d%lld", &n, &mod); build(1, 1, n); for (int i = 0; i < n; i++) { cin >> opt; if (opt == 'A') { scanf("%lld", &x); modify(1, 1, n, ++len, (x%mod+prev)%mod); } else { scanf("%d", &l); prev = query(1, 1, n, len-l+1, len); printf("%lld\n", prev); prev %= mod; } } return 0; }
|