# poj------(3468)A Simple Problem with Integers(区间更新)

A Simple Problem with Integers

Time Limit: 5000MS

Memory Limit: 131072K

Total Submissions: 60745

Accepted: 18522

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 AaAa+1, ... , Ab. -10000 ≤ 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--2007.11.25, Yang Yi

```#include<cstdio>
#include<cstring>
const int maxn=100005;
struct node
{
int lef,rig;
__int64 sum,cnt;
int mid(){
return lef+(rig-lef>>1);
}
};
node reg[maxn<<2];

void Build(int left ,int right,int pos)
{
reg[pos]=(node){left,right,0,0};
if((left==right))
{
scanf("%I64d",&reg[pos].sum);
return ;
}
int mid=reg[pos].mid();
Build(left,mid,pos<<1);
Build(mid+1,right,pos<<1|1);
reg[pos].sum=reg[pos<<1].sum+reg[pos<<1|1].sum;
}
void Update(int left,int right,int pos,int val)
{
if(reg[pos].lef>=left&&reg[pos].rig<=right)
{
reg[pos].cnt+=val;
reg[pos].sum+=val*(reg[pos].rig-reg[pos].lef+1);
return ;
}
if(reg[pos].cnt)
{
reg[pos<<1].cnt+=reg[pos].cnt;
reg[pos<<1|1].cnt+=reg[pos].cnt;
reg[pos<<1].sum+=reg[pos].cnt*(reg[pos<<1].rig-reg[pos<<1].lef+1);
reg[pos<<1|1].sum+=reg[pos].cnt*(reg[pos<<1|1].rig-reg[pos<<1|1].lef+1);
reg[pos].cnt=0;
}
int mid=reg[pos].mid();
if(left<=mid)
Update(left,right,pos<<1,val);
if(right>mid)
Update(left,right,pos<<1|1,val);
reg[pos].sum=reg[pos<<1].sum+reg[pos<<1|1].sum;
}
__int64 Query(int left,int right,int pos)
{
if(left<=reg[pos].lef&&reg[pos].rig<=right)
{
return reg[pos].sum;
}
if(reg[pos].cnt)  //再向下更新一次
{
reg[pos<<1].cnt+=reg[pos].cnt;
reg[pos<<1|1].cnt+=reg[pos].cnt;
reg[pos<<1].sum+=reg[pos].cnt*(reg[pos<<1].rig-reg[pos<<1].lef+1);
reg[pos<<1|1].sum+=reg[pos].cnt*(reg[pos<<1|1].rig-reg[pos<<1|1].lef+1);
reg[pos].cnt=0;
}
int mid=reg[pos].mid();
__int64 res=0;
if(left<=mid)
res+=Query(left,right,pos<<1);
if(mid<right)
res+=Query(left,right,pos<<1|1);
return res;
}
int main()
{
int n,m,a,b,c;
char ss;
while(scanf("%d%d",&n,&m)!=EOF)
{
Build(1,n,1);
while(m--)
{
getchar();
scanf("%c %d%d",&ss,&a,&b);
if(ss=='Q')
printf("%I64d\n",Query(a,b,1));
else{
scanf("%d",&c);
Update(a,b,1,c);
}
}
}
return 0;
}```

0 条评论

## 相关文章

### HDOJ 1008

Elevator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

19060

### hdu 1805Expressions（二叉树构造的后缀表达式）

Expressions Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

35660

### HDOJ 1003

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J...

225100

### POJ-1959 Darts

Darts Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1286 ...

382100

### HDUOJ----John

John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java...

28590

### POJ--3468 A Simple Problem with Integers(线段树)

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K ...

35240

### HDUOJ----More is better（并查集）

More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/10...

412120

### POJ-2039 To and Fro

To and Fro Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8...

22140

### HDU 5676 ztr loves lucky numbers

ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others)    Me...

37750

### hdu----（5045）Contest（数位dp）

Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (J...

36450