poj 3468 A Simple Problem with Integers 解题报告
A Simple Problem with Integers
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 43907 | Accepted: 12862 | |
| 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.
这和上一题基本上是一致的,只是在细节上注意,在QUERY()函数里面,也是要把延迟的标记,更新的,一定要注意!些外这题是要用INT64的!
#include <iostream>
#include <stdio.h>
using namespace std;
#define N 111111
__int64 l[N<<2],flag[N<<2];
void build(__int64 num,__int64 s,__int64 e)
{
flag[num]=0;
if(s==e)
{
scanf("%I64d",&l[num]);
return ;
}
__int64 mid=(s+e)>>1;
build(num<<1,s,mid);
build(num<<1|1,mid+1,e);
l[num]=l[num<<1]+l[num<<1|1];
}
void update(__int64 num ,__int64 s,__int64 e,__int64 a, __int64 b,__int64 c)
{
if(a<=s&&b>=e)
{
flag[num]+=c;
l[num]+=(e-s+1)*c;
return ;
}
if(flag[num])
{
flag[num<<1]+=flag[num];
flag[num<<1|1]+=flag[num];
l[num<<1]+=(e-s+1-((e-s+1)>>1))*flag[num];
l[num<<1|1]+=((e-s+1)>>1)*flag[num];
flag[num]=0;
}
__int64 mid=(s+e)>>1;
if(mid>=a)
{
update(num<<1,s,mid,a,b,c);
}
if(b>mid)
{
update(num<<1|1,mid+1,e,a,b,c);
}
l[num]=l[num<<1]+l[num<<1|1];
}
__int64 query(__int64 num,__int64 s,__int64 e ,__int64 a,__int64 b)
{
if(a<=s&&b>=e)
{
return l[num];
}
if(flag[num])
{
flag[num<<1]+=flag[num];
flag[num<<1|1]+=flag[num];
l[num<<1]+=(e-s+1-((e-s+1)>>1))*flag[num];
l[num<<1|1]+=((e-s+1)>>1)*flag[num];
flag[num]=0;
}
__int64 mid=(s+e)>>1;
__int64 re=0;
if(mid>=a)re+=query(num<<1,s,mid,a,b);
if(mid<b) re+=query(num<<1|1,mid+1,e,a,b);
return re;
}
int main()
{
__int64 n,q,a,b,d;
char c;
while(scanf("%I64d%I64d",&n,&q)!=EOF)
{
build(1,1,n);
while(q--)
{
getchar();
if((c=getchar())=='Q')
{
scanf("%I64d%I64d",&a,&b);
printf("%I64d\n",query(1,1,n,a,b));
}
else if(c=='C')
{
scanf("%I64d%I64d%I64d",&a,&b,&d);
update(1,1,n,a,b,d);
}
}
}
return 0;
}


浙公网安备 33010602011771号