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
一般如果遇到这种题而且没有说初始值都一样,是很难做的,可能做不出来。。。。但是你得找到解这道题的关键点,如果就直接用线段树模拟操作,那延迟标记就写不出来了。。。这道题的关键点就是数组的初始值都一样,可以维护一个相同值区间,如果这个区间的值都相同,那么答案就是tree[root] * (r -l + 1) % mod;
这道题的突破点就在于判断区间的所有数是否相同。。。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N = 1e5 + 5;
const int mod = 10007;
int tree[N << 2];
int vis[N << 2];
int n,m;
void pushdown(int root)
{
if(vis[root]){
vis[root * 2 + 1] = vis[root * 2 + 2] = true;
vis[root] = false;
tree[root * 2 + 1] = tree[root * 2 + 2] = tree[root];
}
}
void update(int root,int l,int r,int ql,int qr,int op,int val)
{
if(r < ql || l > qr){
return ;
}
if(l >= ql && r <= qr && vis[root]){
if(op == 1){
tree[root] = (tree[root] + val) % mod;
}else if(op == 2){
tree[root] = (tree[root] * val) % mod;
}else{
tree[root] = val;
}
return ;
}
pushdown(root);
int mid = (l + r) / 2;
if(ql <= mid){
update(root * 2 + 1,l,mid,ql,qr,op,val);
}
if(qr > mid){
update(root * 2 + 2,mid + 1,r,ql,qr,op,val);
}
if(!vis[root * 2 + 1] || !vis[root * 2 + 2])
vis[root] = false;
else{
if(tree[root * 2 + 1] != tree[root * 2 + 2]){
vis[root] = false;
}else{
vis[root] = true;
tree[root] = tree[root * 2 + 1];
}
}
}
int query(int root,int l,int r,int ql,int qr,int p)
{
if(l >= ql && r <= qr && vis[root]){
int ans = 1;
for(int i = 1;i <= p;++i){
ans = ans * tree[root] % mod;
}
ans = ans * (r - l + 1) % mod;
return ans;
}
pushdown(root);
int mid = (l + r) / 2;
int sum = 0;
if(ql <= mid){
sum += query(root * 2 + 1,l,mid,ql,qr,p);
}
if(qr > mid){
sum += query(root * 2 + 2,mid + 1,r,ql,qr,p);
}
return sum % mod;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
if(n == 0 && m == 0){
break;
}
memset(vis,true,sizeof(vis));
memset(tree,0,sizeof(tree));
for(int i = 0;i < m;++i){
int op,x,y,z;
scanf("%d %d %d %d",&op,&x,&y,&z);
if(op <= 3){
update(1,1,n,x,y,op,z);
}else{
printf("%d\n",query(1,1,n,x,y,z));
}
}
}
return 0;
}