A Simple Problem with Integers

Time Limit: 5000MS Memory Limit: 131072K
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.

思路:

一道线段树的题目(数据结构的题代码量还真是大),首先这是一道和区间有关的线段树的一道模板题,首先把线段树给建造出来,然后就是把每个点的增加值和总和值赋值上去,之后去更新这个线段树,每次更新的时候都要将到达这个区间的值重新更新一遍,最后进行查询。

#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 1e+5 + 10;
struct NODE {
	int l;
	int r;
	ll add;
	ll sum;
	int mid() {
		return (l + r) >> 1;
	}
};
NODE node[maxn << 2];
void PushUp(int rt) {
	node[rt].sum = node[rt << 1].sum + node[rt << 1 | 1].sum;
}
void PushDown(int rt, int x) {
	if (node[rt].add) {
		node[rt << 1].add += node[rt].add;
		node[rt << 1 | 1].add += node[rt].add;
		node[rt << 1].sum += node[rt].add * (x - (x >> 1));
		node[rt << 1 | 1].sum += node[rt].add * (x >> 1);
		node[rt].add = 0;
	}
}
void BuildTree(int l, int r, int rt) {
	node[rt].l = l;
	node[rt].r = r;
	node[rt].add = 0;
	if (l == r) {
		scanf("%lld", &node[rt].sum);
		return ;
	}
	int m = node[rt].mid();
	BuildTree(l, m, rt << 1);
	BuildTree(m + 1, r, rt << 1 | 1);
	PushUp(rt);
}
void UpdataTree(int c, int l, int r, int rt) {
	if (node[rt].l == l && node[rt].r == r) {
		node[rt].add += c;
		node[rt].sum += (ll)c * (r - l + 1);
		return ; 
	}
	if (node[rt].l == node[rt].r) return ;
	PushDown(rt, node[rt].r - node[rt].l + 1);
	int m = node[rt].mid();
	if (r <= m) UpdataTree(c, l, r, rt << 1);
	else if (l > m) UpdataTree(c, l, r, rt << 1 | 1);
	else {
		UpdataTree(c, l, m, rt << 1);
		UpdataTree(c, m + 1, r, rt << 1 | 1);
	}
	PushUp(rt);
}
ll Query(int l, int r, int rt) {
	if (node[rt].l == l && node[rt].r == r) return node[rt].sum;
	PushDown(rt, node[rt].r - node[rt].l + 1);
	int m = node[rt].mid();
	ll res = 0;
	if (r <= m) res += Query(l, r, rt << 1);
	else if (l > m) res += Query(l, r, rt << 1 | 1);
	else {
		res += Query(l, m, rt << 1);
		res += Query(m + 1, r, rt << 1 | 1);
	}
	return res;
}
int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	BuildTree(1, n, 1);
	while (m--) {
		char s[2];
		int a, b, c;
		scanf("%s", s);
		if (s[0] == 'Q') {
			scanf("%d %d", &a, &b);
			printf("%lld\n", Query(a, b, 1));
		} else {
			scanf("%d %d %d", &a, &b, &c);
			UpdataTree(c, a, b, 1);
		}
	}
	return 0;
} 

树状数组的做法

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn = 100010;
ll bit0[maxn] = {0}, bit1[maxn] = {0}, a[maxn] = {0};
int n;
int lowbit(int x) {
    return x & (-x);
}
void updata(ll* bit, int x, int val) {
    for (int i = x; i <= n; i += lowbit(i)) {
        bit[i] += val;
    }
}
ll getsum(ll* bit, int x) {
    ll sum = 0;
    for (int i = x; i >= 1; i -= lowbit(i)) {
        sum += bit[i];
    }
    return sum;
}
ll f(int x) {
    return a[x] + ((x + 1) * getsum(bit0, x) - getsum(bit1, x));
}
int main() {
    int m;
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &a[i]);
        a[i] += a[i - 1];
    }
    while (m--) {
        char s[2];
        int l, r, val;
        scanf("%s %d %d", s, &l, &r);
        if (s[0] == 'Q') {
            printf("%lld\n", f(r) - f(l - 1));
        } else {
            scanf("%d", &val);
            updata(bit0, l, val);
            updata(bit0, r + 1, -val);
            updata(bit1, l, val * l);
            updata(bit1, r + 1, -(val * (r + 1)));
        }
    }
    return 0;
}