您的位置:首页 > 其它

POJ3468(线段树区间增加,区间求和)

2015-10-31 23:51 288 查看
A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 81519   Accepted: 25185
Case Time Limit: 2000MS

Description

You have N integers, A1, A2, ... , 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 A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.

Output

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

Sample Input

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

Sample Output

4
55
9
15

Hint

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

Source

POJ Monthly--2007.11.25, Yang Yi   注意lazy标记的更新
/*
* @Author: LinK
* @Date:   2015-10-31 18:34:32
* @Last Modified by:   LinK
* @Last Modified time: 2015-10-31 23:11:18
*/

#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll;

const int maxn = 100010;

struct Node {
ll sum, add;
} tree[maxn << 2];

int n, q;
ll num[maxn];

void build(int rt, int l, int r) {
if (l == r) {
tree[rt].sum = num[l];
tree[rt].add = 0;
return;
}
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
tree[rt].add = 0;
}

void update(int rt, int l, int r, int L, int R, ll val) {
if (L <= l && R >= r) {
tree[rt].add += val;
return;
}
tree[rt].sum += val * (min(R, r) - max(L, l) + 1);
int mid = (l + r) >> 1;
if (R <= mid) update(rt << 1, l, mid, L, R, val);
else if (L > mid) update(rt << 1 | 1, mid + 1, r, L, R, val);
else {
update(rt << 1, l, mid, L, R, val);
update(rt << 1 | 1, mid + 1, r, L, R, val);
}
}

ll query(int rt, int l, int r, int L, int R) {
if (L <= l && R >= r) {
return tree[rt].sum + tree[rt].add * (r - l + 1);
}
int mid = (l + r) >> 1;
if (tree[rt].add) {
tree[rt].sum += (r - l + 1) * tree[rt].add;
tree[rt << 1].add += tree[rt].add;
tree[rt << 1 | 1].add += tree[rt].add;
tree[rt].add = 0;
}
if (R <= mid) return query(rt << 1, l, mid, L, R);
if (L > mid) return query(rt << 1 | 1, mid + 1, r, L, R);
return query(rt << 1, l, mid, L, R) + query(rt << 1 | 1, mid + 1, r, L, R);
}

int main() {
char op;
int a, b;
ll c;
while (~scanf("%d %d", &n, &q)) {
for (int i = 1; i <= n; i ++) scanf("%lld", &num[i]);
build(1, 1, n);
while (q --) {
scanf(" %c %d %d", &op, &a, &b);
if (op == 'Q') printf("%lld\n", query(1, 1, n, a, b));
else {
scanf("%lld", &c);
update(1, 1, n, a, b, c);
}
}
}

return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: