Yuanfang is puzzled with the question below:
There are n integers, a 1, a 2, …, a n. The initial values of them are 0. There are four kinds of operations.
Operation 1: Add c to each number between a x and a y inclusive. In other words, do transformation a k<—a k+c, k = x,x+1,…,y.
Operation 2: Multiply c to each number between a x and a y inclusive. In other words, do transformation a k<—a k×c, k = x,x+1,…,y.
Operation 3: Change the numbers between a x and a y to c, inclusive. In other words, do transformation a k<—c, k = x,x+1,…,y.
Operation 4: Get the sum of p power among the numbers between a x and a y inclusive. In other words, get the result of a x p+a x+1 p+…+a y p.
Yuanfang has no idea of how to do it. So he wants to ask you to help him.
Input
There are no more than 10 test cases.
For each case, the first line contains two numbers n and m, meaning that there are n integers and m operations. 1 <= n, m <= 100,000.
Each the following m lines contains an operation. Operation 1 to 3 is in this format: “1 x y c” or “2 x y c” or “3 x y c”. Operation 4 is in this format: “4 x y p”. (1 <= x <= y <= n, 1 <= c <= 10,000, 1 <= p <= 3)
The input ends with 0 0.
Output
For each operation 4, output a single integer in one line representing the result. The answer may be quite large. You just need to calculate the remainder of the answer when divided by 10007.
Sample Input
5 5
3 3 5 7
1 2 4 4
4 1 5 2
2 2 5 8
4 3 5 3
0 0
Sample Output
307
7489
有四种操作,1:区间加法;2:区间乘法;3:区间修改;4:区间询问q次幂的和
用二维数组分别记录三个幂(q=1,q=2,q=3)的和
进行区间修改时可以清除加法和乘法的标记
进行区间乘法时如果有加法标记则与加法标记相乘,最后计算时最先置数再乘再加
#include<stdio.h>
#define mod 10007
#define maxn 100005
int sum1,sum2,sum3,len,temp1,temp2,temp3;
struct node
{
int l,r,add,mul,same,sum[4];
} tree[maxn<<2];
void pushup(int k)
{
tree[k].sum[1]=(tree[k<<1].sum[1]+tree[k<<1|1].sum[1])%mod;
tree[k].sum[2]=(tree[k<<1].sum[2]+tree[k<<1|1].sum[2])%mod;
tree[k].sum[3]=(tree[k<<1].sum[3]+tree[k<<1|1].sum[3])%mod;
}
void build(int l,int r,int k)
{
tree[k].l=l;
tree[k].r=r;
tree[k].sum[1]=tree[k].sum[2]=tree[k].sum[3]=0;
tree[k].add=tree[k].mul=tree[k].same=0;
if(l==r)
return;
int mid=(l+r)/2;
build(l,mid,k<<1);
build(mid+1,r,k<<1|1);
}
void opadd(int num,int k)
{
tree[k].add=(tree[k].add+num)%mod;
sum1=tree[k].sum[1],sum2=tree[k].sum[2],sum3=tree[k].sum[3];
len=tree[k].r-tree[k].l+1;
temp1=(num*len)%mod;
tree[k].sum[1]=(sum1+temp1)%mod;
temp2=(num*num)%mod;
tree[k].sum[2]=(sum2+(2*sum1*num)%mod+(temp2*len)%mod)%mod;
temp3=(temp2*num)%mod;
tree[k].sum[3]=(sum3+(3*sum2*num)%mod+(3*sum1*temp2)%mod+(temp3*len)%mod)%mod;
}
void opmul(int num,int k)
{
if(tree[k].mul)
tree[k].mul=(tree[k].mul*num)%mod;
else
tree[k].mul=num;
tree[k].add=(tree[k].add*num)%mod;
sum1=tree[k].sum[1],sum2=tree[k].sum[2],sum3=tree[k].sum[3];
temp1=num;
tree[k].sum[1]=(sum1*temp1)%mod;
temp2=(temp1*num)%mod;
tree[k].sum[2]=(sum2*temp2)%mod;
temp3=(temp2*num)%mod;
tree[k].sum[3]=(sum3*temp3)%mod;
}
void opsame(int num,int k)
{
tree[k].same=num;
tree[k].add=tree[k].mul=0;
len=tree[k].r-tree[k].l+1;
temp1=num;
tree[k].sum[1]=(temp1*len)%mod;
temp2=(temp1*num)%mod;
tree[k].sum[2]=(temp2*len)%mod;
temp3=(temp2*num)%mod;
tree[k].sum[3]=(temp3*len)%mod;
}
void pushdown(int k)
{
if(tree[k].same)
{
opsame(tree[k].same,k<<1);
opsame(tree[k].same,k<<1|1);
tree[k].same=0;
}
if(tree[k].mul)
{
opmul(tree[k].mul,k<<1);
opmul(tree[k].mul,k<<1|1);
tree[k].mul=0;
}
if(tree[k].add)
{
opadd(tree[k].add,k<<1);
opadd(tree[k].add,k<<1|1);
tree[k].add=0;
}
}
void oporate(int op,int num,int k,int L,int R)
{
if(L<=tree[k].l&&R>=tree[k].r)
{
if(op==1)
opadd(num,k);
else if(op==2)
opmul(num,k);
else
opsame(num,k);
return;
}
pushdown(k);
int mid=(tree[k].r+tree[k].l)/2;
if(mid>=L)
oporate(op,num,k<<1,L,R);
if(mid<R)
oporate(op,num,k<<1|1,L,R);
pushup(k);
}
int query(int k,int L,int R,int p)
{
if(L<=tree[k].l&&tree[k].r<=R)
return tree[k].sum[p];
pushdown(k);
int mid=(tree[k].l+tree[k].r)/2;
int ans=0;
if(L<=mid)
ans=(ans+query(k<<1,L,R,p))%mod;
if(R>mid)
ans=(ans+query(k<<1|1,L,R,p))%mod;
pushup(k);
return ans;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
if(n==0||m==0)
break;
build(1,n,1);
while(m--)
{
int op,x,y,c;
scanf("%d%d%d%d",&op,&x,&y,&c);
if(op<4)
oporate(op,c,1,x,y);
else
printf("%d\n",query(1,x,y,c));
}
}
}