题目地址http://poj.org/problem?id=3468
题面:
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.
题意:
给定Q (1 ≤ Q ≤ 100,000)个数A1,A2 … AQ,以及可能多次进行的两个操作:
- 对某个区间Ai … Aj的每个数都加n(n可变)
- 求某个区间Ai … Aj的数的和
只存和,会导致每次加数的时候都要更新到叶子节点,速度太慢(O(nlogn)),这是必须要避免的。会TLE
AC代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#define ll long long
#define dd double
#define f(i, x, y) for(ll i = x; i < y; i++)
using namespace std;
ll ans = 0; //最后的结果
struct node {
//树的结构
ll l, r;
ll sum, inc;
ll mid() {
return (l + r) / 2;
}
}tree[800010];
void BuildTree(ll root, ll l, ll r) {
//建一个空的树
tree[root].sum = 0;
tree[root].inc = 0;
tree[root].l = l;
tree[root].r = r;
if (tree[root].l == tree[root].r) {
return;
}
else {
BuildTree(root * 2 + 1, l, (l + r) / 2);
BuildTree(root * 2 + 2, (l + r) / 2 + 1, r);
}
}
void Insert(ll root, ll i, ll v) {
//插入数字
if (tree[root].l == tree[root].r) {
tree[root].sum = v;
return;
}
tree[root].sum += v;
if (i <= tree[root].mid()) {
Insert(root * 2 + 1, i, v);
}
else {
Insert(root * 2 + 2, i, v);
}
}
void Add(ll root, ll s, ll e, ll v) {
//增加数字的值
if (tree[root].l == s && tree[root].r == e) {
tree[root].inc += v;
return;
}
tree[root].sum += ((e - s + 1) * v);
if (e <= tree[root].mid()) {
Add(root * 2 + 1, s, e, v);
}
else if (s > tree[root].mid()) {
Add(root * 2 + 2, s, e, v);
}
else {
Add(root * 2 + 1, s, tree[root].mid(), v);
Add(root * 2 + 2, tree[root].mid() + 1, e, v);
}
}
ll QuerySum(ll root, ll s, ll e) {
//查询区间,如果该节点区间正好是查询区间则直接返回答案
//否则向下更新sum和inc,
//把inc加到sum里面之后给左儿子和右儿子,然后将inc归零
if (tree[root].l == s && tree[root].r == e) {
return ans + tree[root].sum + (e - s + 1) * tree[root].inc;
}
else {
tree[root].sum = tree[root].sum + (tree[root].r - tree[root].l + 1) * tree[root].inc;
tree[root * 2 + 1].inc += tree[root].inc;
tree[root * 2 + 2].inc += tree[root].inc;
tree[root].inc = 0;
}
if (e <= tree[root].mid()) {
return QuerySum(root * 2 + 1, s, e);
}
else if (s > tree[root].mid()) {
return QuerySum(root * 2 + 2, s, e);
}
else {
return QuerySum(root * 2 + 1, s, tree[root].mid()) + QuerySum(root * 2 + 2, tree[root].mid() + 1, e);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n, m; cin >> n >> m;
BuildTree(0, 1, n);
f(i, 0, n) {
ll x; cin >> x;
Insert(0, i + 1, x);
}
f(i, 0, m) {
string s; cin >> s;
if (s[0] == 'Q') {
ans = 0;
ll st, en;
cin >> st >> en;
ans = QuerySum(0, st, en);
cout << ans << endl;
}
else {
ll st, en, v;
cin >> st >> en >> v;
Add(0, st, en, v);
ans = 0;
}
}
}