# POJ 3468 A Simple Problem with Integers【线段树区间更新入门题】

A Simple Problem with Integers
 Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 102132 Accepted: 31872 Case Time Limit: 2000MS

Description

You have N integers, A1A2, … , 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 A1A2, … , AN. ⑴000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, … , Ab. ⑴0000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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-⑵007.11.25, Yang Yi

http://blog.csdn.net/acdreamers/article/details/8578161

http://blog.csdn.net/acceptedxukai/article/details/6933446

## AC代码：

```#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=100000+5;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long LL;
int a[maxn];
struct node
{
int l,r,m;
LL sum,mark;
}tree[maxn<<2];

void BuildTree(int l,int r,int k)
{
tree[k].l=l;
tree[k].r=r;
tree[k].m=(l+r)>>1;
tree[k].mark=0;
if(l==r)
{
tree[k].sum=a[l];
return;
}
int mid=(l+r)>>1;
BuildTree(l,mid,k<<1);
BuildTree(mid+1,r,k<<1|1);

tree[k].sum=(tree[k<<1].sum+tree[k<<1|1].sum);
}
void UpdateTree(int l,int r,int k,int x)
{
if(tree[k].l==l&&tree[k].r==r)
{
tree[k].mark+=x;
return;
}
tree[k].sum+=(LL)x*(r-l+1);
//int mid=(tree[k].l+tree[k].r)>>1;
int mid=tree[k].m;
if(r<=mid)
UpdateTree(l,r,k<<1,x);
else if(mid<l)
UpdateTree(l,r,k<<1|1,x);
else
{
UpdateTree(l,mid,k<<1,x);
UpdateTree(mid+1,r,k<<1|1,x);
}
}
LL QueryTree(int l,int r,int k)
{
//cout<<l<<","<<r<<","<<k<<endl;
if(tree[k].l==l&&tree[k].r==r)
return tree[k].sum+tree[k].mark*(LL)(r-l+1);

if(tree[k].mark!=0)
{
tree[k<<1].mark+=tree[k].mark;
tree[k<<1|1].mark+=tree[k].mark;

tree[k].sum+=(LL)(tree[k].r-tree[k].l+1)*tree[k].mark;//记得+1
tree[k].mark=0;
}

//int mid=(tree[k].l+tree[k].r)>>1;
int mid=tree[k].m;
if(mid>=r)
return QueryTree(l,r,k<<1);
else if(l>mid)
return QueryTree(l,r,k<<1|1);
else
return QueryTree(l,mid,k<<1)+QueryTree(mid+1,r,k<<1|1);
}
int main()
{
int n,m;
while(cin>>n>>m)
{
for(int i=1;i<=n;i++)
cin>>a[i];
BuildTree(1,n,1);
char ch[2];
int x,y,z;
while(m--)
{
scanf("%s",ch);
if(ch[0]=='Q')
{
scanf("%d%d",&x,&y);
printf("%lld\n",QueryTree(x,y,1));
}
else if(ch[0]=='C')
{
scanf("%d%d%d",&x,&y,&z);
UpdateTree(x,y,1,z);
}
}
}
return 0;
}
```

1. 本站所有资源来源于用户上传和网络，如有侵权请邮件联系站长！
2. 分享目的仅供大家学习和交流，您必须在下载后24小时内删除！
3. 不得使用于非法商业用途，不得违反国家法律。否则后果自负！
4. 本站提供的源码、模板、插件等等其他资源，都不包含技术服务请大家谅解！
5. 如有链接无法下载、失效或广告，请联系管理员处理！
6. 本站资源售价只是赞助，收取费用仅维持本站的日常运营所需！
7. 如遇到加密压缩包，请使用WINRAR解压,如遇到无法解压的请联系管理员！