您的位置:首页 > 其它

Another Array of Orz Pandas

2016-12-05 15:33 260 查看
[b]Another Array of Orz Pandas[/b]

题目链接:http://acm.xidian.edu.cn/problem.php?id=1187

[b]线段树[/b]

线段树维护区间和以及区间内各个数平方和,对于每一个询问ans=(sum2-pow_sum)/2

代码如下:

#include<cstdio>
#include<cstring>
#define lson (x<<1)
#define rson (x<<1|1)
#define mid ((l+r)>>1)
#define N 100007
typedef long long LL;
struct nod{
LL sum,lazy,or2zds;
}a[N<<2];
const LL mod=1e9+7;
const LL Max=1e9;
LL n,m;
void push_up(LL x){
a[x].sum=(a[lson].sum+a[rson].sum)%mod;
a[x].or2zds=(a[lson].or2zds+a[rson].or2zds)%mod;
}
void push_down(LL x,LL l,LL r){
a[lson].or2zds=(a[lson].or2zds+(a[x].lazy*a[x].lazy)%mod*(mid+1-l)+2*a[x].lazy*a[lson].sum+mod)%mod;
a[lson].sum=(a[lson].sum+a[x].lazy*(mid+1-l))%mod;
a[lson].lazy=(a[lson].lazy+a[x].lazy)%mod;
a[rson].or2zds=(a[rson].or2zds+(a[x].lazy*a[x].lazy)%mod*(r-mid)+2*a[x].lazy*a[rson].sum+mod)%mod;
a[rson].sum=(a[rson].sum+a[x].lazy*(r-mid))%mod;
a[rson].lazy=(a[rson].lazy+a[x].lazy)%mod;
a[x].lazy=0;
}
void add(LL x,LL l,LL r,LL cl,LL cr,LL v){
if(cl<=l&&r<=cr){
a[x].or2zds=(a[x].or2zds+(v*v)%mod*(r-l+1)%mod+2*v*a[x].sum)%mod;
a[x].sum=(a[x].sum+v*(r-l+1))%mod;
a[x].lazy=(a[x].lazy+v)%mod;
return;
}
if(a[x].lazy!=0)push_down(x,l,r);
if(cl<=mid)add(lson,l,mid,cl,cr,v);
if(mid<cr)add(rson,mid+1,r,cl,cr,v);
push_up(x);
}
void query(LL x,LL l,LL r,LL ql,LL qr,LL &sum,LL &sum2){
if(ql<=l&&r<=qr){
sum=(sum+a[x].sum)%mod;
sum2=(sum2+a[x].or2zds)%mod;
return;
}
if(a[x].lazy!=0)push_down(x,l,r);
if(ql<=mid)query(lson,l,mid,ql,qr,sum,sum2);
if(mid<qr)query(rson,mid+1,r,ql,qr,sum,sum2);
}
int main(void){
while(~scanf("%lld%lld",&n,&m)){
memset(a,0,sizeof(a));
LL l,r,op,k;
for(LL i=0;i<m;i++){
scanf("%lld",&op);
if(op==1){
scanf("%lld%lld%lld",&l,&r,&k);
add(1,1,n,l,r,k);
}else {
scanf("%lld%lld",&l,&r);
LL sum=0,sum2=0;
query(1,1,n,l,r,sum,sum2);
printf("%lld\n",(sum*sum%mod-sum2+mod)%mod*500000004%mod);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: