标签:
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 71540 | Accepted: 22049 | |
| 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
Source
// Fast Sequence Operations I
// Rujia Liu
// 输入格式:
// n m 数组范围是a[1]~a[n],初始化为0。操作有m个
// 1 L R v 表示设a[L]+=v, a[L+1]+v, ..., a[R]+=v
// 2 L R 查询a[L]~a[R]的sum, min和max
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int maxnode = 111111 << 2;
LL _sum;
LL qL,qR, v; //<span style="color:#ff0000;">_sum为全局变量</span>
struct IntervalTree {
LL sumv[maxnode], addv[maxnode];
// 维护信息
void maintain(LL o, LL L, LL R) {
int lc = o*2, rc = o*2+1;
sumv[o]=0;
if(R > L) {
sumv[o] = sumv[lc] + sumv[rc];
}
if(addv[o]) {sumv[o] += addv[o] * (R-L+1); }
}
void update(LL o, LL L, LL R) {
int lc = o*2, rc = o*2+1;
if(qL <= L && qR >= R) { // 递归边界
addv[o] += v; // 累加边界的add值
} else {
int M = L + (R-L)/2;
if(qL <= M) update(lc, L, M);
if(qR > M) update(rc, M+1, R);
}
maintain(o, L, R); // 递归结束前重新计算本结点的附加信息
}
void query(LL o, LL L, LL R, LL add) {
if(qL <= L && qR >= R) { // 递归边界:用边界区间的附加信息更新答案
_sum += sumv[o] + add * (R-L+1);
} else { // 递归统计,累加参数add
int M = L + (R-L)/2;
if(qL <= M) query(o*2, L, M, add + addv[o]);
if(qR > M) query(o*2+1, M+1, R, add + addv[o]);
}
}
};
IntervalTree tree;
int main() {
int m,n;
scanf("%d%d",&n,&m);
memset(&tree,0,sizeof(tree));
for(int i=1;i<=n;i++)
{
qL=i;qR=i;
scanf("%I64d",&v);
tree.update(1,1,n);
}
while(m--)
{
char s[5];
int a,b,c;
scanf("%s",s);
if(s[0]==‘Q‘)
{
scanf("%I64d%I64d",&qL,&qR);
_sum=0;
tree.query(1,1,n,0);
printf("%I64d\n",_sum);
}
else
{
scanf("%I64d%I64d%I64d",&qL,&qR,&v);
tree.update(1,1,n);
}
}
// system("pause");
return 0;
}
标签:
原文地址:http://www.cnblogs.com/xianbin7/p/4489646.html