A Simple Problem with Integers
一直在数据类型上出错。
终于知道long long 是 “%lld” 来输出输入了。
time 1732ms
#include <iostream>
#include <stdio.h>
#define LL long long
using namespace std;
const int N=100005;
LL add[N<<2];
LL sum[N<<2];
void push_up(int rt)
{ sum[rt]=sum[rt<<1]+sum[rt<<1|1]; }
void push_down(int rt,int m)
{ if(add[rt])
{ add[rt<<1]+=add[rt];
add[rt<<1|1]+=add[rt];
sum[rt<<1]+=add[rt]*(m-(m>>1));
sum[rt<<1|1]+=add[rt]*(m>>1) ;
add[rt]=0;
}
}
void build(int l,int r,int rt)
{ add[rt]=0;
if(l==r)
{ scanf("%lld",&sum[rt]) ;
return ;
}
int m=(l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
push_up(rt);
}
void updata(int l,int r,int rt,int L,int R,int v)
{ if(L<=l&&R>=r)
{ add[rt]+=v;
sum[rt]+=(LL)v*(r-l+1);
return ;
}
push_down(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m)
updata(l,m,rt<<1,L,R,v);
if(R>m)
updata(m+1,r,rt<<1|1,L,R,v);
push_up(rt);
}
LL query(int l,int r,int rt,int L,int R)
{ if(L<=l&&R>=r)
return sum[rt];
push_down(rt,r-l+1);
int m=(l+r)>>1;
LL ans=0;
if(L<=m)
ans+=query(l,m,rt<<1,L,R);
if(R>m)
ans+=query(m+1,r,rt<<1|1,L,R);
return ans;
}
int main()
{
int n,m,a,b,c;
char op[2];
while(scanf("%d%d",&n,&m)!=EOF)
{
build(1,n,1);
while(m--)
{
scanf("%s",op);
if(op[0]=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",query(1,n,1,a,b));
}
else
{
scanf("%d%d%d",&a,&b,&c);
updata(1,n,1,a,b,c);
}
}
}
}