题目链接:http://poj.org/problem?id=3468
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.
Problem solving report:
Description: 给一串数,C操作对区间每个数加值,Q操作查询区间和。
Problem solving: 裸的线段树区间更新问题,树状数组也可以做。
Accepted Code:
//线段树
/*
* @Author: lzyws739307453
* @Language: C++
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 100005;
struct Tree {
long long val, mark;
}segTree[MAXN << 2];
void Create(int l, int r, int rt) {
if (!(r != l)) {
scanf("%lld", &segTree[rt].val);
segTree[rt].mark = 0;
return ;
}
int mid = l + ((r - l) >> 1);
Create(l, mid, rt << 1);
Create(mid + 1, r, rt << 1 | 1);
segTree[rt].val = segTree[rt << 1].val + segTree[rt << 1 | 1].val;
}
void PushDown_(int l, int r, long long v, int rt) {
segTree[rt].val += 1ll * (r - l + 1) * v;
segTree[rt].mark += v;
}
void PushDown(int l, int r, int rt) {
if (segTree[rt].mark) {
int mid = l + ((r - l) >> 1);
PushDown_(l, mid, segTree[rt].mark, rt << 1);
PushDown_(mid + 1, r, segTree[rt].mark, rt << 1 | 1);
segTree[rt].mark = 0;
}
}
void Update(int Ql, int Qr, int v, int l, int r, int rt) {
if (Ql <= l && Qr >= r) {
segTree[rt].val += 1ll * (r - l + 1) * v;
segTree[rt].mark += v;
return ;
}
PushDown(l, r, rt);
int mid = l + ((r - l) >> 1);
if (Ql <= mid)
Update(Ql, Qr, v, l, mid, rt << 1);
if (Qr > mid)
Update(Ql, Qr, v, mid + 1, r, rt << 1 | 1);
segTree[rt].val = segTree[rt << 1].val + segTree[rt << 1 | 1].val;
}
long long Query(int Ql, int Qr, int l, int r, int rt) {
if (Ql <= l && Qr >= r)
return segTree[rt].val;
long long cnt = 0;
PushDown(l, r, rt);
int mid = l + ((r - l) >> 1);
if (Ql <= mid)
cnt += Query(Ql, Qr, l, mid, rt << 1);
if (Qr > mid)
cnt += Query(Ql, Qr, mid + 1, r, rt << 1 | 1);
return cnt;
}
int main() {
char op;
int l, r, n, m, delta;
while (~scanf("%d%d", &n, &m)) {
Create(1, n, 1);
while (m--) {
scanf(" %c%d%d", &op, &l, &r);
if (op != 'Q') {
scanf("%d", &delta);
Update(l, r, delta, 1, n, 1);
}
else printf("%lld\n", Query(l, r, 1, n, 1));
}
}
return 0;
}
//树状数组
/*
* @Author: lzyws739307453
* @Language: C++
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 100005;
int n;
long long bits_0[MAXN], bits_1[MAXN];
int lowbit(int x) {
return x & -x;
}
void Update(long long bits[], int i, long long k) {
while (i <= n) {
bits[i] += k;
i += lowbit(i);
}
}
long long Query(long long bits[], int i) {
long long cnt = 0;
while (i > 0) {
cnt += bits[i];
i -= lowbit(i);
}
return cnt;
}
int main() {
char op;
int l, r, m;
long long delta;
while (~scanf("%d%d", &n, &m)) {
for (int i = 1; i <= n; i++) {
scanf("%lld", &delta);
Update(bits_1, i, delta);
}
while (m--) {
scanf(" %c%d%d", &op, &l, &r);
if (op != 'Q') {
scanf("%lld", &delta);
Update(bits_0, l, delta);
Update(bits_0, r + 1, -delta);
Update(bits_1, l, -delta * (l - 1));
Update(bits_1, r + 1, delta * r);
}
else {
long long cnt = Query(bits_0, r) * r + Query(bits_1, r);
cnt -= Query(bits_0, l - 1) * (l - 1) + Query(bits_1, l - 1);
printf("%lld\n", cnt);
}
}
}
return 0;
}