版权声明:转载请注明出处: https://blog.csdn.net/weixin_43871781/article/details/87714781
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
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<cctype>
#include<algorithm>
#define read read()
#define Read Read()
#define dread dread()
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int MAX=1e6+5;
inline int read
{
int X=0,w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
inline ll Read
{
ll X=0,w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
inline double dread
{
double X=0,Y=1.0; int w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=X*10+(ch^48),ch=getchar();
ch=getchar();
while(isdigit(ch)) X+=(Y/=10)*(ch^48),ch=getchar();
return w?-X:X;
}
/*inline void write(int x)
{
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}*/
char ch[4];
int num[100005],ad[320];
int m;
ll sum[320];
void add(int a,int b,int c)
{
int l=a/m,r=b/m;
if(l==r)
{
for(int i=a;i<=b;i++)
num[i]+=c;
sum[l]+=(b-a+1)*c;
return;
}
for(int i=a;i<(l+1)*m;i++)
num[i]+=c,sum[l]+=c;
for(int i=r*m;i<=b;i++)
num[i]+=c,sum[r]+=c;
for(int i=l+1;i<r;i++)
ad[i]+=c,sum[i]+=m*c;
}
ll Find(int a,int b)
{
int l=a/m,r=b/m;
ll ans=0;
if(l==r)
{
for(int i=a;i<=b;i++)
ans+=(num[i]+ad[l]);
return ans;
}
for(int i=a;i<(l+1)*m;i++)
ans+=(num[i]+ad[l]);
for(int i=r*m;i<=b;i++)
ans+=(num[i]+ad[r]);
for(int i=l+1;i<r;i++)
ans+=sum[i];
return ans;
}
int main()
{
int n,q;
n=read;q=read;
m=sqrt(n);
for(int i=1;i<=n;i++)
{
num[i]=Read;
sum[i/m]+=num[i];
}
int a,b,c;
while(q--)
{
scanf("%s",ch);
if(ch[0]=='Q')
{
a=read;
b=read;
printf("%lld\n",Find(a,b));
}
else
{
a=read;
b=read;
c=read;
add(a,b,c);
}
}
}