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
解题思路
通过flag数组记录线段树中区间[l,r]中的数值是否一样,后续查询时只需要算出当前节点的值然后乘以(r-l+1)就可以直接得到这个区间的值。
AC代码:
#include
#include
#include
using namespace std;
#define lson root<<1
#define rson root<<1|1
#define MOD 10007
typedef long long ll;
const int MAXN = 100005;
int arr[MAXN<<2];
bool flag[MAXN<<2];
void push_up(int root)
{
if(!flag[lson] || !flag[rson]) flag[root]=false;
else if(arr[lson]!=arr[rson]) flag[root]=false;
else flag[root]=true,arr[root]=arr[lson];
}
void update(int root,int L,int R,int l,int r,int num,int type)
{
if(L<=l && R>=r && flag[root])
{
if(type==1) arr[root]=(arr[root]+num)%MOD;
else if(type==2) arr[root]=(arr[root]*num)%MOD;
else arr[root]=num;
return;
}
if(flag[root])
{
flag[lson]=flag[rson]=true;
arr[lson]=arr[rson]=arr[root];
flag[root]=false;
}
int mid=(l+r)>>1;
if(L<=mid) update(lson,L,R,l,mid,num,type);
if(R>mid) update(rson,L,R,mid+1,r,num,type);
push_up(root);
}
ll query(int root,int L,int R,int l,int r,int p)
{
if(L<=l && R>=r && flag[root])
{
ll ans=1;
for(int i=0;i<p;i++) ans=(ans*arr[root])%MOD;
ans=(ans*(r-l+1))%MOD;
return ans;
}
if(flag[root])
{
flag[lson]=flag[rson]=true;
arr[lson]=arr[rson]=arr[root];
flag[root]=false;
}
int mid=(l+r)>>1;
ll ans=0;
if(L<=mid) ans+=query(lson,L,R,l,mid,p);
if(R>mid) ans+=query(rson,L,R,mid+1,r,p);
return ans%MOD;
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m;
while(cin >> n >> m && (n||m))
{
memset(arr,0,sizeof(arr));
memset(flag,true,sizeof(flag));
while(m--)
{
int f,x,y,c;
cin >> f >> x >> y >> c;
if(f!=4) update(1,x,y,1,n,c,f);
else cout << query(1,x,y,1,n,c) << endl;
}
}
return 0;
}