题意:有N个数,M个操作。(1)"= l r k",表示把区间[l,r]的数全部变成k。(2)"? l r k",查询区间[l,r]范围里。
k比较小,将式子拆开成多项式,一项一项加。数学的东西感觉多些。。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define MID(a,b) (a+((b-a)>>1))
const LL mod=1000000007;
const int N=100005;
const int K=10;
LL C[K][K],S[N][K],a[N];
LL pow(int x,int y)
{
LL res=1;
for(int i=0;i<y;i++) res=(res*(LL)x)%mod;
return res;
}
struct node
{
int lft,rht;
LL valu[K],flag;
int mid(){return MID(lft,rht);}
void fun(int tmp)
{
flag=tmp;
for(int i=0;i<K;i++)
{
valu[i]=((((S[rht][i]-S[lft-1][i])%mod+mod)%mod)*flag)%mod;
}
}
void init()
{
flag=-1;
memset(valu,0,sizeof(valu));
}
};
struct Seg
{
node tree[N*4];
void PushUp(int ind)
{
for(int i=0;i<K;i++)
tree[ind].valu[i]=(tree[LL(ind)].valu[i]+tree[RR(ind)].valu[i])%mod;
}
void PushDown(int ind)
{
if(tree[ind].flag==-1) return;
tree[LL(ind)].fun(tree[ind].flag);
tree[RR(ind)].fun(tree[ind].flag);
tree[ind].flag=-1;
}
void build(int lft,int rht,int ind)
{
tree[ind].lft=lft; tree[ind].rht=rht;
tree[ind].init();
if(lft==rht)
{
for(int i=0;i<K;i++)
tree[ind].valu[i]=(a[lft]*pow(lft,i))%mod;
}
else
{
int mid=tree[ind].mid();
build(lft,mid,LL(ind));
build(mid+1,rht,RR(ind));
PushUp(ind);
}
}
void updata(int st,int ed,int ind,int flag)
{
int lft=tree[ind].lft,rht=tree[ind].rht;
if(st<=lft&&rht<=ed) tree[ind].fun(flag);
else
{
PushDown(ind);
int mid=tree[ind].mid();
if(st<=mid) updata(st,ed,LL(ind),flag);
if(ed> mid) updata(st,ed,RR(ind),flag);
PushUp(ind);
}
}
LL query(int st,int ed,int ind,int k)
{
int lft=tree[ind].lft,rht=tree[ind].rht;
if(st<=lft&&rht<=ed) return tree[ind].valu[k];
else
{
PushDown(ind);
LL sum=0;
int mid=tree[ind].mid();
if(st<=mid) sum=(sum+query(st,ed,LL(ind),k))%mod;
if(ed> mid) sum=(sum+query(st,ed,RR(ind),k))%mod;
PushUp(ind);
return sum%mod;
}
}
}seg;
void pre_calu(int n)
{
for(int i=0;i<=K;i++) C[i][i]=C[i][0]=1;
for(int i=2;i<K;i++)
for(int j=1;j<i;j++)
C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
for(int i=1;i<=n;i++)
for(int j=0;j<K;j++)
S[i][j]=(S[i-1][j]+pow(i,j))%mod;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
pre_calu(n);
for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
seg.build(1,n,1);
while(m--)
{
char str[5];
int l,r,k;
scanf("%s%d%d%d",str,&l,&r,&k);
if(str[0]=='=') seg.updata(l,r,1,k);
else
{
LL res=0,t=1;
for(int i=0;i<=k;i++)
{
res=(res+((seg.query(l,r,1,k-i)*C[k][i])%mod*t)%mod)%mod;
res=(res+mod)%mod;
t=(t*(1-l))%mod;
}
printf("%I64d\n",res);
}
}
}
return 0;
}