Transformation
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others)Total Submission(s): 3620 Accepted Submission(s): 873
Problem Description
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.
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.
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
Source
Recommend
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define mp push_back
#define lson l,m,rt<<1
#define rson m,r,rt<<1|1
int n,nums;
const int MAXN = 200010*4;
int add[MAXN];
int mul[MAXN];
int sum[MAXN][4];
const int MOD = 10007;
void update(int L,int R,int inmul,int inadd,int l,int r,int rt);
int sqr2(int x)
{
return ((x % MOD) * (x % MOD)) % MOD;
}
int sqr3(int x)
{
return sqr2(x) * x % MOD;
}
void build(int l,int r,int rt)
{
}
void updatesum(int k,int b,int l,int r,int rt)
{
if(k != 1)
{
(sum[rt][3] *= sqr3(k)) %= MOD;
(sum[rt][2] *= sqr2(k)) %= MOD;
(sum[rt][1] *= k) %= MOD;
(add[rt] *= k) %= MOD;
(mul[rt] *= k) %= MOD;
}
if(b != 0)
{
(sum[rt][3] += sqr3(b) * (r -l)) %= MOD;
(sum[rt][3] += 3 * b * sum[rt][2]) %= MOD;
(sum[rt][3] += 3 * sqr2(b) * sum[rt][1]) %= MOD;
(sum[rt][2] += sqr2(b) * (r - l)) %= MOD;
(sum[rt][2] += 2 * b * sum[rt][1]) %= MOD;
(sum[rt][1] += b * (r - l)) %= MOD;
(add[rt] += b) %= MOD;
}
}
void PushUp(int rt)
{
for(int i=1;i<=3;i++)
{
(sum[rt][i] = sum[rt<<1][i] + sum[rt<<1|1][i]) %= MOD;
}
}
void PushDown(int l,int r,int rt)
{
int m = (l + r) >> 1;
updatesum(mul[rt],add[rt],lson);
updatesum(mul[rt],add[rt],rson);
add[rt] = 0;
mul[rt] = 1;
}
void update(int L,int R,int inmul,int inadd,int l,int r,int rt)
{
int m = (l + r) >> 1;
if(L <= l && r <= R)
{
updatesum(inmul,inadd,l,r,rt);
return ;
}
PushDown(l,r,rt);
if(L < m) update(L,min(R,m),inmul,inadd,lson);
if(m < R) update(max(L,m),R,inmul,inadd,rson);
PushUp(rt);
}
int query(int L,int R,int p,int l,int r,int rt)
{
if(L <= l && r <= R)
{
return sum[rt][p];
}
int m = (l + r) >> 1;
PushDown(l,r,rt);
int ret = 0;
if(L < m) (ret += query(L,min(m,R),p,lson)) %= MOD;
if(m < R) (ret += query(max(L,m),R,p,rson)) %= MOD;
return ret;
}
int main()
{
while(scanf("%d%d",&n,&nums))
{
if(n == 0 && nums == 0) break;
memset(add,0,sizeof(add));
for(int i=1;i<n*6;i++)
{
mul[i] = 1;
}
memset(sum,0,sizeof(sum));
for(int i=0;i<nums;i++)
{
int op,x,y,c;
scanf("%d%d%d%d",&op,&x,&y,&c);
if(op == 1)
{
update(x,y+1,1,c,1,n+1,1);
}
else if(op == 2)
{
update(x,y+1,c,0,1,n+1,1);
}
else if(op == 3)
{
update(x,y+1,0,c,1,n+1,1);
}
else
{
printf("%d\n",query(x,y+1,c,1,n+1,1));
}
}
}
}