K - Transformation
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
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#define lt k<<1
#define rt k<<1|1
#define lson l, mid, lt
#define rson mid+1, r, rt
using namespace std;
const int maxn = 100005;
const int N = 10007;
struct node
{
int l,r;
int lazy1,lazy2,lazy3;//分别表示加号,乘号,等号标记
} t[maxn<<2];
int n;
void build(int l, int r, int k)
{
t[k].l = l;
t[k].r = r;
t[k].lazy1 = 0;
t[k].lazy2 = 1;
t[k].lazy3 = -1;
if(l == r)
{
t[k].lazy3 = 0;//最底层等号赋值为0
return;
};
int mid = (l+r)>>1;
build(lson);
build(rson);
}
void pushdown(int k)
{
if(t[k].l == t[k].r)
{
return;
}
if(t[k].lazy3 != -1)//处理等号
{
t[lt].lazy3 = t[rt].lazy3 = t[k].lazy3;//更新子区间等号标记
t[lt].lazy2 = t[rt].lazy2 = 1;//清空子区间加乘标记
t[lt].lazy1 = t[rt].lazy1 = 0;
t[k].lazy3 = -1;
return;
}
if(t[k].lazy2 != 1)//处理乘号
{
if(t[lt].lazy3 != -1)
{
t[lt].lazy3 = (t[lt].lazy3*t[k].lazy2) % N;//如果子区间有等号标记,直接修改等号标记
}
else//否则清空该子区间标记,进行子区间标记
{
pushdown(lt);
t[lt].lazy2 = (t[lt].lazy2*t[k].lazy2)%N;
}
if(t[rt].lazy3 != -1)
{
t[rt].lazy3=(t[rt].lazy3*t[k].lazy2) % N;//同理
}
else
{
pushdown(rt);
t[rt].lazy2=(t[rt].lazy2*t[k].lazy2)%N;
}
t[k].lazy2=1;//清空乘法标记
}
if(t[k].lazy1!=0)//处理加号标记
{
if(t[lt].lazy3 != -1)
{
t[lt].lazy3=(t[lt].lazy3+t[k].lazy1)%N;
}
else
{
pushdown(lt);
t[lt].lazy1 = (t[lt].lazy1+t[k].lazy1)%N;
}
if(t[rt].lazy3 != -1)
{
t[rt].lazy3=(t[rt].lazy3+t[k].lazy1)%N;
}
else
{
pushdown(rt);
t[rt].lazy1=(t[rt].lazy1+t[k].lazy1)%N;
}
t[k].lazy1=0;//记得清空
}
}
void update(int l, int r, int num, int d, int k)
{
if(t[k].l == l && t[k].r == r)
{
if(d == 1)
{
if(t[k].lazy3!=-1)
{
t[k].lazy3=(t[k].lazy3+num)%N;//如果有等号标记,就直接修改等号标记
}
else
{
pushdown(k);//否则清空该区间,进行标记
t[k].lazy1 = (t[k].lazy1+num)%N;
}
}
else if(d == 2)//同理
{
if(t[k].lazy3 != -1)
{
t[k].lazy3=(t[k].lazy3*num)%N;
}
else
{
pushdown(k);
t[k].lazy2 = (t[k].lazy2*num) % N;
}
}
else
{
t[k].lazy3=num%N;
t[k].lazy1=0;
t[k].lazy2=1;
}
return;
}
pushdown(k);//向下更新
int mid = (t[k].l+t[k].r)>>1;
if(r <= mid)
{
update(l,r,num,d,lt);
}
else if(l>mid)
{
update(l,r,num,d,rt);
}
else
{
update(l,mid,num,d,lt);
update(mid+1,r,num,d,rt);
}
}
int query(int l,int r,int p,int k)
{
if(t[k].l>=l&&t[k].r<=r&&t[k].lazy3!=-1)//查到是查询区间的子区间且一段全为相同的数
{
int temp = 1;
for(int i = 1; i <= p; i++)
{
temp = (temp*t[k].lazy3)%N;
}
return ((t[k].r-t[k].l+1)*temp)%N;//注意要乘上长度
}
pushdown(k);
int mid = (t[k].l + t[k].r)>>1;
if(r <= mid)
{
return query(l,r,p,lt)%N;
}
else if(l > mid)
{
return query(l,r,p,rt)%N;
}
else
{
return (query(l,mid,p,lt)+query(mid+1,r,p,rt))%N;
}
}
int main()
{
int m;
while(~scanf("%d%d",&n,&m))
{
if(!n && !m)
{
break;
}
build(1,n,1);
for(int i = 0; i < m; i++)
{
int d,x,y,c;
scanf("%d%d%d%d",&d, &x, &y, &c);
if(d >= 1 && d <= 3)
{
update(x,y,c,d,1);
}
else
{
printf("%d\n",query(x,y,c,1)%N);
}
}
}
return 0;
}