A Simple Problem with Integers

2023-05-02,,

A Simple Problem with Integers

Time Limit: 5000MS Memory Limit: 131072K

Total Submissions: 77964 Accepted: 24012

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

线段树的区间查询与区间更新,lazy优化,不然会超时

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#define LL long long
using namespace std;
const int MAX = 110000;
struct node
{
LL lazy;
LL sum;
} Tree[MAX*12];
LL a[MAX];
void Build(int L,int R,int site)
{
if(L==R)
{
Tree[site].lazy=0;
Tree[site].sum=a[L];
return ;
}
Tree[site].lazy=0;
int mid=(L+R)>>1;
Build(L,mid,site<<1);
Build(mid+1,R,site<<1|1);
Tree[site].sum=Tree[site<<1].sum+Tree[site<<1|1].sum;
}
void update(int L,int R,int l,int r,int site,LL w)
{
if(L==l&&r==R)
{
if(L==R)
Tree[site].lazy=0;
else
Tree[site].lazy+=w;
Tree[site].sum+=((R-L+1)*w);
return ;
}
int mid=(L+R)>>1;
if(Tree[site].lazy!=0)
{
update(L,mid,L,mid,site<<1,Tree[site].lazy);
update(mid+1,R,mid+1,R,site<<1|1,Tree[site].lazy);
Tree[site].lazy=0;
}
Tree[site].sum+=((r-l+1)*w);
if(mid>=r)
{
update(L,mid,l,r,site<<1,w);
}
else if(l>mid)
{
update(mid+1,R,l,r,site<<1|1,w);
}
else
{
update(L,mid,l,mid,site<<1,w);
update(mid+1,R,mid+1,r,site<<1|1,w);
}
Tree[site].sum=Tree[site<<1].sum+Tree[site<<1|1].sum;
}
LL Query(int L,int R,int l,int r,int site)
{
if(L==l&&r==R)
{
return Tree[site].sum;
}
int mid=(L+R)>>1;
if(Tree[site].lazy!=0)
{
update(L,mid,L,mid,site<<1,Tree[site].lazy);
update(mid+1,R,mid+1,R,site<<1|1,Tree[site].lazy);
Tree[site].lazy=0;
} if(mid>=r)
{
return Query(L,mid,l,r,site<<1);
}
else if(mid<l)
{
return Query(mid+1,R,l,r,site<<1|1);
}
else
{
return Query(L,mid,l,mid,site<<1)+Query(mid+1,R,mid+1,r,site<<1|1);
}
}
int main()
{
int n;
int Q;
char s[5];
int u,v;
LL w;
while(~scanf("%d %d",&n,&Q))
{
for(int i=1; i<=n; i++)
{
scanf("%I64d",&a[i]);
}
Build(1,n,1);
while(Q--)
{
scanf("%s",s);
if(s[0]=='Q')
{
scanf("%d %d",&u,&v);
printf("%I64d\n",Query(1,n,u,v,1));
}
else
{
scanf("%d %d %I64d",&u,&v,&w);
update(1,n,u,v,1,w);
}
}
}
return 0;
}

A Simple Problem with Integers的相关教程结束。

《A Simple Problem with Integers.doc》

下载本文的Word格式文档,以方便收藏与打印。