Transformation Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others) Total Submission(s): 3869 Accepted Submission(s): 956
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.
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
|
不停取余,不停取余。。。眼都花了 o(╯□╰)o
题意:给你N个数和Q次操作。
1 x y c 表示区间[x, y]所有数全加c
2 x y c 表示区间[x, y]所有数全乘c
3 x y c 表示区间[x, y]所有数修改为c
4 x y c 求区间[x, y]所有数c次方之和 结果对10007取余。
思路:lazy1 lazy2 lazy3 对应三个操作的标记。sum1 sum2 sum3为区间所有数1、2、3次方之和。len为区间长度。
这里面lazy1 和 lazy2的标记不太好弄,可以简化下。
对一个数a,先加lazy1再乘lazy2,结果为(a + lazy1) * lazy2 = a * lazy2 + lazy1 * lazy2
反过来结果为a * lazy2 + lazy1,我们只需把末尾等效就好了。对lazy1,每当出现lazy2时,乘上就可以了。
单点加c的时候,把三次方公式化简一下就得到
sum3 += 3*c*sum2 + 3*c*c*sum1 + c*c*c*len。sum2类似。
单点乘c的时候,注意lazy1的等效,单点修改c时,lazy1 lazy2取消。后两个公式很简单。
标记下传时,考虑顺序。假设lazy3是最后一个标记,那么我们直接操作它就好了。但是如果它不是最后一个,我们还是要修改它,再对lazy1或者lazy2操作。这样每次先操作lazy3。
lazy3标记下传最简单,直接去掉lazy1和lazy2,修改lazy3就可以了。至于lazy1和lazy2有:
[son].lazy1 = [father].lazy1 + [father].lazy2 * [son].lazy1。[son].lazy2 *= [father].lazy1
记lazy1 lazy2是father向son传的标记,s1、s2、s3为儿子1次、2次、3次方之和。
这样对结果的更新 (a+lazy1)*lazy2 —— a*l2 + l1,已经操作了lazy1 * lazy2
1次方sum1—— a*l2 + l1 = s1 * l2 + len * l1;
2次方sum2——(a*l2 + l1) ^ 2 = s2*l2*l2 + 2*s1*l2*l1 + len*l1*l1;
3次方sum3——(a*l2 + l1) ^ 3 = s3*l2*l2*l2 + 3*s2*l2*l2*l1 + 3*s1*l2*l1*l1 + len*l1*l1*l1;
AC代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include