pku3468 A Simple Problem with Integers
2013-02-27 16:16
337 查看
http://poj.org/problem?id=3468
线段树,成段更新
注意long long
线段树,成段更新
注意long long
#include <stdio.h> #define lson l, m, root<<1 #define rson m+1, r, root<<1|1 #define N 100100 long long sum[N<<2], add[N<<2]; void push_up(int root) { sum[root] = sum[root<<1] + sum[root<<1|1]; } void push_down(int root, int len) { if(add[root]) { add[root<<1] += add[root]; add[root<<1|1] += add[root]; sum[root<<1] += add[root] * (len - (len>>1)); sum[root<<1|1] += add[root] * (len>>1); add[root] = 0; } } void build(int l, int r, int root) { int m; add[root] = 0; if(l == r) { scanf("%lld", sum+root); return; } m = (l + r) >> 1; build(lson); build(rson); push_up(root); } void update(int L, int R, int x, int l, int r, int root) { int m; if(L <= l && r <= R) { add[root] += x; sum[root] += (r-l+1)*(long long)x; return; } push_down(root, r-l+1); m = (l + r) >> 1; if(L <= m) { update(L, R, x, lson); } if(m+1 <= R) { update(L, R, x, rson); } push_up(root); } long long query(int L, int R, int l, int r, int root) { int m; long long result; if(L <= l && r <= R) { return sum[root]; } push_down(root, r-l+1); m = (l + r) >> 1; result = 0; if(L <= m) { result += query(L, R, lson); } if(m+1 <= R) { result += query(L, R, rson); } return result; } int main() { int n, q, a, b, c; char c1; scanf("%d%d", &n, &q); build(1, n, 1); getchar(); while(q-- && scanf("%c%d%d%*c", &c1, &a, &b)) { if(c1 == 'C') { scanf("%d%*c", &c); update(a, b, c, 1, n, 1); continue; } printf("%lld\n", query(a, b, 1, n, 1)); } return 0; }
相关文章推荐
- BZOJ3212: Pku3468 A Simple Problem with Integers
- bzoj3212 pku3468 A Simple Problem with Integers
- pku 3468 A Simple Problem with Integers
- 线段树 区域覆盖模版题 pku 3468 A Simple Problem with Integers 线段树——成段操作
- [PKU] 3468 A Simple Problem with Integers -- 线段树
- 3212: Pku3468 A Simple Problem with Integers
- bzoj3212 Pku3468 A Simple Problem with Integers
- [BZOJ3212]Pku3468 A Simple Problem with Integers
- 【分块】【线段树】bzoj3212 Pku3468 A Simple Problem with Integers
- pku 3468 A Simple Problem with Integers
- 3212: Pku3468 A Simple Problem with Integers
- pku -- 3468 A Simple Problem with Integers(线段树)
- BZOJ P3212 Pku3468 A Simple Problem with Integers
- [BZOJ3212] Pku3468 A Simple Problem with Integers
- BZOJ3212: Pku3468 A Simple Problem with Integers
- PKU A Simple Problem with Integers 3468
- bzoj 3212: Pku3468 A Simple Problem with Integers (线段树)
- PKU 3468 A Simple Problem with Integers
- BZOJ 3212 Pku3468 A Simple Problem with Integers
- PKU 3468 A Simple Problem with Integers