#include <stdio.h>
#include <string.h>
#include <algorithm>
#include<iostream>
using namespace std;
#define MAXN 102000
struct node
{
int l;
int r;
__int64 sum;
__int64 add;
}t[4*MAXN];
__int64 rr[MAXN];
void build(int id,int l,int r)
{
int mid = (l+r)/2;
t[id].l = l;
t[id].r = r;
if(r==l)
{
t[id].sum = rr[l];
t[id].add = 0;
}
else
t[id].add = 0;
if(r>l)
{
build(id*2,l,mid);
build(id*2+1,mid+1,r);
t[id].sum = t[id*2].sum + t[id*2+1].sum;
}
}
void change(int id,int start,int end,__int64 num)
{
if(start==t[id].l && end==t[id].r)
{
t[id].add += num;
return ;
}
t[id].sum+= (end-start+1)*num;
int mid = (t[id].l + t[id].r)/2;
if(t[id].add!=0)
{
t[id].sum += (t[id].r-t[id].l+1)*t[id].add;
change(id*2,t[id].l,mid,t[id].add);
change(id*2+1,mid+1,t[id].r,t[id].add);
t[id].add = 0;
}
if(mid>=end)
{
change(id*2,start,end,num);
}
else if(mid+1<=start)
{
change(id*2+1,start,end,num);
}
else
{
change(id*2,start,mid,num);
change(id*2+1,mid+1,end,num);
}
}
__int64 ans;
void find(int id,int start,int end)
{
if (t[id].l==start && t[id].r == end)
{
ans+=t[id].sum+(t[id].r-t[id].l+1)*t[id].add;
return;
}
if (t[id].add!=0)
{
t[id].sum+= (t[id].r-t[id].l+1)*t[id].add;
t[id*2].add+=t[id].add;
t[id*2+1].add+=t[id].add;
t[id].add = 0;
}
int mid = (t[id].l+t[id].r)/2;
if(mid>=end)
find(id*2,start,end);
else if(start>=mid+1)
find(id*2+1,start,end);
else
{
find(id*2,start,mid);
find(id*2+1,mid+1,end);
}
}
int main()
{
int n,m;
int i,j;
char str[10];
int start,end;
__int64 num;
while (scanf("%d%d",&n,&m)!=EOF)
{
for (i=1;i<=n;i++)
{
scanf("%I64d",&rr[i]);
}
build(1,1,n);
for (i=0;i<m;i++)
{
scanf("%s",str);
if (strcmp(str,"C")==0)
{
scanf("%d%d%I64d",&start,&end,&num);
change(1,start,end,num);
}
else
{
scanf("%d%d",&start,&end);
ans = 0;
find(1,start,end);
printf("%I64d\n",ans);
}
}
}
return 0;
}
A Simple Problem with Integers
最新推荐文章于 2022-12-09 12:45:57 发布