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
| #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll;
const int N = 2e5 + 10; int n, q; ll sum1[N*4], a[N], sum2[N*4];
void change1(int k1) { sum1[k1] = sum1[k1*2] + sum1[k1*2+1]; } void change2(int k1) { sum2[k1] = sum2[k1*2] + sum2[k1*2+1]; }
void buildtree1(int k, int l, int r) { if(l == r) { sum1[k] = a[l]; return; } int mid = l+r >> 1; buildtree1(k*2, l, mid); buildtree1(k*2+1, mid+1, r); change1(k); } void buildtree2(int k, int l, int r) { if(l == r) { sum2[k] = a[l]*l; return; } int mid = l+r >> 1; buildtree2(k*2, l, mid); buildtree2(k*2+1, mid+1, r); change2(k); }
void update1(int k1, int l, int r, int p, int q) { if(l == r) { sum1[k1] = q; return; } int mid = l+r >> 1; if(p <= mid) { update1(k1*2, l, mid, p, q); } else update1(k1*2+1, mid+1, r, p, q); change1(k1); }
void update2(int k1, int l, int r, int p, int q) { if(l == r) { sum2[k1] = 1LL * p * q; return; } int mid = l+r >> 1; if(p <= mid) { update2(k1*2, l, mid, p, q); } else { update2(k1*2+1, mid+1, r, p, q); } change2(k1); }
ll Find1(int k1, int l, int r, int L, int R) { if(l > R || r < L) return 0; if(L <= l && r <= R) return sum1[k1]; int mid = l+r >> 1; return Find1(k1*2, l, mid, L, R) + Find1(k1*2+1, mid+1, r, L, R); }
ll Find2(int k1, int l, int r, int L, int R) { if(l > R || r < L) return 0; if(L <= l && r <= R) return sum2[k1]; int mid = l+r >> 1; return Find2(k1*2, l, mid, L, R) + Find2(k1*2+1, mid+1, r, L, R); }
int main() { cin >> n >> q; for(int i = 1; i <= n; i++) { scanf("%lld", &a[i]); } buildtree1(1, 1, n); buildtree2(1, 1, n); for(int i = 1; i <= q; i++) { ll op, l, r; scanf("%lld%lld%lld", &op, &l, &r); if(op == 1) { ll tmp = (1ll*r+1)*Find1(1, 1, n, l, r) - Find2(1, 1, n, l, r); printf("%lld\n", tmp); } else if(op == 2) { update1(1, 1, n, l, r); update2(1, 1, n, l, r); } }
return 0; }
|