原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1012
线段树,单点更新。。
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstdio> 5 #define lc root<<1 6 #define rc root<<1|1 7 using std::max; 8 const int Max_N = 200010; 9 const int INF = 0x3f3f3f3f; 10 int MOD; 11 struct Node { int val; }; 12 struct SegTree { 13 Node seg[Max_N << 2]; 14 inline void push_up(int root) { 15 seg[root].val = max(seg[lc].val, seg[rc].val); 16 } 17 inline void update(int root, int l, int r, int pos, int t) { 18 if (pos > r || pos < l) return; 19 if (pos <= l && pos >= r) { 20 seg[root].val += t; 21 return; 22 } 23 int mid = (l + r) >> 1; 24 update(lc, l, mid, pos, t); 25 update(rc, mid + 1, r, pos, t); 26 push_up(root); 27 } 28 inline int query(int root, int l, int r, int x, int y) { 29 if (x > r || y < l) return -INF; 30 if (x <= l && y >= r) return seg[root].val; 31 int mid = (l + r) >> 1; 32 int v1 = query(lc, l, mid, x, y); 33 int v2 = query(rc, mid + 1, r, x, y); 34 return max(v1, v2); 35 } 36 }seg; 37 int main() { 38 #ifdef LOCAL 39 freopen("in.txt", "r", stdin); 40 freopen("out.txt", "w+", stdout); 41 #endif 42 char ch; 43 int n, v, res, len = 0; 44 while (~scanf("%d %d", &n, &MOD)) { 45 res = 0; 46 for (int i = 0; i < n; i++) { 47 getchar(); 48 scanf("%c %d", &ch, &v); 49 if (ch == 'A') ++len, seg.update(1, 1, n, len, (res + v) % MOD); 50 else printf("%d\n", res = seg.query(1, 1, n, len - v + 1, n)); 51 } 52 } 53 return 0; 54 }