poj3468 A Simple Problem with Integers
阿新 • • 發佈:2018-02-16
strong sin getch har post query tree col turn
思路:
線段樹區間更新。
實現:
1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 typedef long long ll; 5 const int N = 100005; 6 ll tree[N * 4], lazy[N * 4], a[N]; 7 int n, m; 8 9 void build(int num, int l, int r) 10 { 11 if (l == r) { tree[num] = a[l]; return; } 12 int m = l + r >> 1; 13 build(num * 2, l, m); 14 build(num * 2 + 1, m + 1, r); 15 tree[num] = tree[num * 2] + tree[num * 2 + 1]; 16 } 17 18 void pushdown(int num, int cl, int cr) 19 { 20 if (!lazy[num]) return; 21 tree[num * 2] += lazy[num] * cl; 22 tree[num * 2 + 1] += lazy[num] * cr; 23 lazy[num * 2] += lazy[num]; 24 lazy[num * 2 + 1] += lazy[num]; 25 lazy[num] = 0; 26 } 27 28 void update(int num, int l, int r, int x, int y, ll dx) 29 { 30 if (x <= l && y >= r) { tree[num] += (r - l + 1) * dx; lazy[num] += dx; return; } 31 int m = l + r >> 1; 32 pushdown(num, m - l + 1, r - m); 33 if (x <= m) update(num * 2, l, m, x, y, dx); 34 if (y >= m + 1) update(num * 2 + 1, m + 1, r, x, y, dx); 35 tree[num] = tree[num * 2] + tree[num * 2 + 1]; 36 } 37 38 ll query(int num, int l, int r, int x, int y) 39 { 40 if (x <= l && y >= r) return tree[num]; 41 int m = l + r >> 1; 42 pushdown(num, m - l + 1, r - m); 43 ll ans = 0; 44 if (x <= m) ans += query(num * 2, l, m, x, y); 45 if (y >= m + 1) ans += query(num * 2 + 1, m + 1, r, x, y); 46 return ans; 47 } 48 49 int main() 50 { 51 scanf("%d %d", &n, &m); 52 for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); 53 getchar(); 54 build(1, 1, n); 55 char tmp; int x, y; ll d; 56 for (int i = 0; i < m; i++) 57 { 58 scanf("%c", &tmp); 59 if (tmp == ‘Q‘) { scanf("%d %d", &x, &y); getchar(); printf("%lld\n", query(1, 1, n, x, y)); } 60 else { scanf("%d %d %lld", &x, &y, &d); getchar(); update(1, 1, n, x, y, d); } 61 } 62 return 0; 63 }
poj3468 A Simple Problem with Integers