POJ3468 A Simple Problem with Integers 【线段树】+【成段更新】

来源:互联网 发布:青果软件学院 编辑:程序博客网 时间:2024/06/07 01:50

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072KTotal Submissions: 57666 Accepted: 17546Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915

Hint

The sums may exceed the range of 32-bit integers.

2014-9-4 16:25:07更新

#include <stdio.h>#include <string.h>#define maxn 100002#define lson l, mid, rt << 1#define rson mid + 1, r, rt << 1 | 1typedef long long ll;struct Node{ll lazy, sum;} tree[maxn << 2];void pushUp(int rt){tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;}void pushDown(int l, int r, int rt){int mid = (l + r) >> 1;tree[rt << 1].sum += (mid - l + 1) * tree[rt].lazy;tree[rt << 1 | 1].sum += (r - mid) * tree[rt].lazy;tree[rt << 1].lazy += tree[rt].lazy;tree[rt << 1 | 1].lazy += tree[rt].lazy;tree[rt].lazy = 0;}void build(int l, int r, int rt){tree[rt].lazy = 0;if(l == r){scanf("%lld", &tree[rt].sum);return;}int mid = (l + r) >> 1;build(lson); build(rson);pushUp(rt);}void update(int left, int right, ll val, int l, int r, int rt){if(left == l && right == r){tree[rt].sum += (r - l + 1) * val;tree[rt].lazy += val; return;}int mid = (l + r) >> 1;if(tree[rt].lazy) pushDown(l, r, rt);if(right <= mid) update(left, right, val, lson);else if(left > mid) update(left, right, val, rson);else {update(left, mid, val, lson);update(mid + 1, right, val, rson);}pushUp(rt);}ll query(int left, int right, int l, int r, int rt){if(left == l && right == r) return tree[rt].sum;int mid = (l + r) >> 1;if(tree[rt].lazy) pushDown(l, r, rt);if(right <= mid) return query(left, right, lson);else if(left > mid) return query(left, right, rson);return query(left, mid, lson) + query(mid + 1, right, rson);}int main(){int n, m, i, a, b;char com[2];ll c;while(scanf("%d%d", &n, &m) == 2){build(1, n, 1);while(m--){scanf("%s%d%d", com, &a, &b);if(com[0] == 'Q') printf("%lld\n", query(a, b, 1, n, 1));else{scanf("%lld", &c);update(a, b, c, 1, n, 1);}}}return 0;}


RE了三次,发现是query函数内的pushdown函数位置放错了。。改了下,终于A掉了。

//#define DEBUG#include <stdio.h>#define maxn 100002#define lson l, mid, rt << 1#define rson mid + 1, r, rt << 1 | 1long long tree[maxn << 2], arr[maxn], lazy[maxn << 2];void pushUp(int rt){tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];}void pushDown(int l, int r, int rt){int mid = (l + r) >> 1;tree[rt << 1] += (mid - l + 1) * lazy[rt];tree[rt << 1 | 1] += (r - mid) * lazy[rt];lazy[rt << 1] += lazy[rt];lazy[rt << 1 | 1] += lazy[rt];lazy[rt] = 0;}void build(int l, int r, int rt){lazy[rt] = 0;if(l == r){tree[rt] = arr[r]; return;}int mid = (l + r) >> 1;build(lson);build(rson);pushUp(rt);}void update(int left, int right, long long val, int l, int r, int rt){if(left == l && right == r){lazy[rt] += val; tree[rt] += val * (r - l + 1); return;} //include l == rif(lazy[rt]) pushDown(l, r, rt);int mid = (l + r) >> 1;if(right <= mid) update(left, right, val, lson);else if(left > mid) update(left, right, val, rson);else{update(left, mid, val, lson);update(mid + 1, right, val, rson);}pushUp(rt);}long long query(int left, int right, int l, int r, int rt){if(left == l && right == r)return tree[rt];if(lazy[rt]) pushDown(l, r, rt);int mid = (l + r) >> 1;if(right <= mid){return query(left, right, lson);}else if(left > mid){return query(left, right, rson);}else{return query(left, mid, lson) + query(mid + 1, right, rson);}}int main(){#ifdef DEBUGfreopen("../stdin.txt", "r", stdin);freopen("../stdout.txt", "w", stdout);#endifint n, q, i, a, b;long long c;char com[2];while(scanf("%d%d", &n, &q) == 2){for(i = 1; i <= n; ++i)scanf("%lld", arr + i);build(1, n, 1);while(q--){scanf("%s%d%d", com, &a, &b);if(com[0] == 'C'){scanf("%lld", &c);update(a, b, c, 1, n, 1);}else printf("%lld\n", query(a, b, 1, n, 1));}}return 0;}


0 0