题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1012
看到单点更新和查询先想到线段树,初始化直接将所有的位置看成0,就只有单点修改和单点查询了。
1 #include<bits/stdc++.h> 2 #define lson l,mid,i<<1 3 #define rson mid+1,r,i<<1|1 4 #define mid (l+r)/2 5 using namespace std; 6 typedef long long ll; 7 const int maxn = 2e5 + 10; 8 ll Max[maxn << 2]; 9 inline ll read() { 10 ll n = 0, f = 1; char ch = getchar(); 11 while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); } 12 while (ch >= '0'&&ch <= '9') { n = n * 10 + ch - '0'; ch = getchar(); } 13 return n * f; 14 } 15 void up(int i) { 16 Max[i] = max(Max[i << 1], Max[i << 1 | 1]); 17 } 18 void update(int p, ll x, int l, int r, int i) { 19 if (l == r) { 20 Max[i] = x; 21 return; 22 } 23 if (p <= mid) 24 update(p, x, lson); 25 else 26 update(p, x, rson); 27 up(i); 28 } 29 ll query(int L, int R, int l, int r, int i) { 30 if (L <= l && r <= R) 31 return Max[i]; 32 ll MMax = 0; 33 if (L <= mid) 34 MMax = max(MMax, query(L, R, lson)); 35 if (R > mid) 36 MMax = max(MMax, query(L, R, rson)); 37 return MMax; 38 } 39 char q[3]; 40 int main() { 41 ll n, d, x, t = 0, len = 0; 42 n = read(), d = read(); 43 for (int i = 1; i <= n; i++) { 44 scanf("%s", q); 45 x = read(); 46 if (q[0] == 'A') { 47 x = (x + t) % d; 48 len++; 49 update(len, x, 1, n, 1); 50 } 51 else { 52 t = query(len - x + 1, len, 1, n, 1); 53 printf("%lld ", t); 54 } 55 } 56 }