U have N integers, A1, A2, ... , AN
"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.
#include
#include
#include
using namespace std;
typedef long long ll;
const ll maxn=100005;
ll delt[maxn],sum[maxn],n,m;
ll lowbit(ll x) {return x&(-x);}
void update(ll *a,ll pos,ll val)
{
while(pos<=n+1)
{
a[pos]+=val;
pos+=lowbit(pos);
}
}
ll query(ll *a,ll pos)
{
ll ans=0;
while(pos>0)
{
ans+=a[pos];
pos-=lowbit(pos);
}
return ans;
}
int main()
{
#define int ll
scanf("%lld%lld",&n,&m);
int x=0,y=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&x);
update(delt,i,x-y);
update(sum,i,(i-1)*(x-y));
y=x;
}
//scanf("%d",&m);
for(int i=1;i<=m;i++)
{
int x,y,z;
char k[5];
scanf("%s%lld%lld",k,&x,&y);
if(k[0]=='C')
{
scanf("%lld",&z);
update(delt,x,z);
update(delt,y+1,-z);
update(sum,x,z*(x-1));
update(sum,y+1,-z*y);
}
else
{
int sum1=(x-1)*query(delt,x-1)-query(sum,x-1);
int sum2=y*query(delt,y)-query(sum,y);
printf("%lld\n",sum2-sum1);
}
}
return 0;
}
与线段树对比
#include
#include
#include
#include
#include
#include
#include
#include