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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| #include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int num[maxn]; int n; struct node{ int l, r, sum, lazy, mx; node(){ l = r = sum = lazy = mx = 0; } }a[5 * maxn]; inline void update(int k){ a[k].sum = a[k * 2].sum + a[k * 2 + 1].sum; a[k].mx = max(a[k * 2].mx, a[k * 2 + 1].mx); }
void build(int k, int l, int r){ a[k].l = l, a[k].r = r; if(l == r){ a[k].sum = num[l]; a[k].mx = num[l]; return; } int mid = l + (r - l) / 2; build(k * 2, l, mid); build(k * 2 + 1, mid + 1, r); update(k); }
void change(int k, int x, int y) { if(a[k].l == a[k].r){ a[k].sum = y; a[k].mx = y; return; } int mid = (a[k].l + a[k].r) / 2; if(x <= mid) change(k * 2, x, y); else change(k * 2 + 1, x, y); update(k); }
void segChange(int k, int l, int r, int x){ if(a[k].l == l && a[k].r == r){ a[k].sum += (r - l + 1) * x; a[k].lazy += x; return; } int mid = (a[k].l + a[k].r) / 2; if(r <= mid) segChange(2 * k, l, r, x); else if(l > mid) segChange(2 * k + 1, l, r, x); else{ segChange(2 * k, l, mid, x); segChange(2 * k + 1, mid + 1, r, x); } update(k); }
void pushdown(int k) { if(a[k].l == a[k].r){ a[k].lazy = 0; return; } a[2 * k].sum += (a[k * 2].r - a[k * 2].l + 1) * a[k].lazy; a[k * 2 + 1].sum += (a[k * 2 + 1].r - a[k * 2 + 1].l + 1) * a[k].lazy; a[k * 2].lazy += a[k].lazy; a[k * 2 + 1].lazy += a[k].lazy; a[k].lazy = 0; }
int query(int k, int l, int r){ if(a[k].lazy) pushdown(k); if(a[k].l == l && a[k].r == r) return a[k].sum; int mid = (a[k].l + a[k].r) / 2; if(r <= mid) return query(k * 2, l, r); if(l > mid) return query(k * 2 + 1, l, r); return query(k * 2, l, mid) + query(k * 2 + 1, mid + 1, r); }
int queryMax(int k, int l, int r){ if(a[k].l == l && a[k].r == r) return a[k].mx; int mid = (a[k].l + a[k].r) / 2; if(r <= mid) return queryMax(k * 2, l, r); if(l > mid) return queryMax(k * 2 + 1, l, r); return max(queryMax(k * 2, l, mid), queryMax(k * 2 + 1, mid + 1, r)); } int main() { int m; while(~scanf("%d%d", &n, &m)){ memset(num, 0, sizeof(num)); for(int i = 0; i < 5 * n + 1; ++i){ a[i].l = a[i].r = a[i].sum = a[i].mx = a[i].lazy = 0; } for(int i = 1; i <= n; ++i){ scanf("%d", &num[i]); } build(1, 1, n); char s; int l, r; for(int i = 0; i < m; ++i){ cin >> s >> l >> r; if(s == 'Q'){ printf("%d\n", queryMax(1, l, r)); } else if(s == 'U'){ change(1, l, r); } } } return 0; }
|