A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 93004 Accepted: 28934
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
线段树区间更新
#include "cstring"
#include "cstdio"
#include "iostream"
#include "string.h"
#define MAXSIZE 100005
using namespace std;
long long num[100005];
typedef struct
{
long long num,mark;
int left, right;
}NODE;
NODE tree[MAXSIZE * 4];
long long build(int root,int lf,int rt)
{
int mid;
tree[root].left = lf;
tree[root].right = rt;
tree[root].mark=0;
if (lf == rt)
{
return tree[root].num = num[lf];
}
mid = (lf + rt) / 2;
long long a, b;
a = build(2 * root, lf, mid);
b = build(2 * root + 1, mid + 1, rt);
return tree[root].num=a+b;
}
void update(int root,int left,int right,long long number)
{
//如果当前区间完全包含在需要更新的区间中那么对该节点进行标记而不继续向下查找
if(tree[root].left==left&&tree[root].right==right)
{
tree[root].mark+=number;
return;
}
tree[root].num+=((long long)right-(long long)left+(long long)1)*number;
int mid=(tree[root].left+tree[root].right)/2;
if(left>mid)
update(root*2+1, left, right, number);
else
if(right<=mid)
update(root*2,left,right,number);
else
{
update(root*2,left,mid,number);
update(root*2+1,mid+1,right,number);
}
}
long long sum=0;
/*long long cnt(int root,int left,int right)
{
if(tree[root].left==left&&tree[root].right==right)
{
return tree[root].num+((long long)right-(long long)left+(long long)1)*tree[root].mark;
}
if(tree[root].mark!=0)
{
tree[root*2].mark+=tree[root].mark;
tree[root*2+1].mark+=tree[root].mark;
tree[root].num+=(long long)(tree[root].right-tree[root].left+1)*tree[root].mark;
tree[root].mark=0;
}
int mid=(tree[root].left+tree[root].right)/2;
if(left>mid)
return cnt(root*2+1, left, right);
else
if(right<=mid)
return cnt(root*2,left,right);
else
{
long long a=cnt(root*2,left,mid);
long long b=cnt(root*2+1,mid+1,right);
return a+b;
}
}*/
void cnt(int root,int left,int right)
{
if(tree[root].left==left&&tree[root].right==right)
{
sum+=tree[root].num+((long long)right-(long long)left+(long long)1)*tree[root].mark;
return;
}
if(tree[root].mark!=0)
{
tree[root*2].mark+=tree[root].mark;
tree[root*2+1].mark+=tree[root].mark;
tree[root].num+=(long long)(tree[root].right-tree[root].left+1)*tree[root].mark;
tree[root].mark=0;
}
int mid=(tree[root].left+tree[root].right)/2;
if(left>mid)
{
cnt(root*2+1, left, right);
return;
}
else
if(right<=mid)
{
cnt(root*2,left,right);
return;
}
else
{
cnt(root*2,left,mid);
cnt(root*2+1,mid+1,right);
return ;
}
}
int main()
{
int n,q;
while(~scanf("%d%d",&n,&q))
{
for(int i=1;i<=n;i++)
scanf("%lld",&num[i]);
build(1,1,n);
for(int i=1;i<=q;i++)
{
sum=0;
char move[20];
scanf("%s",move);
if(move[0]=='Q')
{
int left,right;
scanf("%d%d",&left,&right);
cnt(1,left,right);
printf("%lld\n",sum);
}
else
{
int left,right;
long long number;
scanf("%d%d%lld",&left,&right,&number);
update(1,left,right,number);
//printf("%lld\n",sum);
}
}
}
return 0;
}