我还是第一次见一会对 一会不对的评测系统。。。一样的代码 一次对一次不对??
#include<iostream>
#include<algorithm>
#include<cstring>
#define lc rt<<1
#define rc rt<<1|1
using namespace std;
const int maxn = 1e5+10;
int n,m,a[maxn];
struct Node{
int l,r;
long long sum,inc;
}t[maxn*40];
void build(int L = 1,int R = n,int rt = 1)
{
t[rt].l = L, t[rt].r = R;
if(L==R)
{
t[rt].sum = a[L]; //wdnmd
return;
}
int mid = (L+R)>>1;
build(L,mid,rt<<1); build(mid+1,R,rt<<1|1); //wdnmd
t[rt].sum = t[rt<<1].sum + t[rt<<1|1].sum;
}
long long query(int l,int r,int L = 1, int R = n, int rt = 1)
{
if(l==L&&r==R) return t[rt].sum;
if(t[rt].inc != 0)
{
t[rt<<1].sum += t[rt].inc * (t[rt<<1].r-t[rt<<1].l+1);
t[rt<<1].inc += t[rt].inc;
t[rt<<1|1].sum += t[rt].inc * (t[rt<<1|1].r-t[rt<<1|1].l+1);
t[rt<<1|1].inc += t[rt].inc;
t[rt].inc = 0;
}
int mid = (L+R)>>1;
long long s = 0;
if(r<=mid) s+=query(l,r,L,mid,rt<<1);
else if(l>mid) s+=query(l,r,mid+1,R,rt<<1|1);
else s+=(query(l,mid,L,mid,rt<<1)+query(mid+1,r,mid+1,R,rt<<1|1));
return s;
}
void update(int l,int r,int v,int L = 1,int R = n,int rt = 1)
{
if(t[rt].l == l && t[rt].r == r){
t[rt].sum += (t[rt].r-t[rt].l+1)*v;
t[rt].inc += v;
return;
}
if(t[rt].inc != 0){
t[rt*2].sum += t[rt].inc*(t[rt*2].r-t[rt*2].l+1);
t[rt*2].inc += t[rt].inc;
t[rt*2+1].sum += t[rt].inc*(t[rt*2+1].r-t[rt*2+1].l+1);
t[rt*2+1].inc += t[rt].inc;
t[rt].inc = 0;
}
int mid = (L+R)>>1;
if(r <= mid) update(l,r,v,L,mid,rt<<1);
else if(l > mid) update(l,r,v,mid+1,R,rt<<1|1);
else{
update(l,mid,v,L,mid,rt<<1);
update(mid+1,r,v,mid+1,R,rt<<1|1);
}
t[rt].sum = t[rt*2].sum+t[rt*2+1].sum;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; ++i) scanf("%d",&a[i]);
build();
int l,r,c;
char op[2];
while(m--)
{
scanf("%s",op);
if(op[0]=='Q')
{
scanf("%d%d",&l,&r);
printf("%lld\n",query(l,r));//wdnmd
}
else
{
scanf("%d%d%d",&l,&r,&c);
update(l,r,c);
}
}
}