题目链接:HDU 4578 Transformation
Yuanfang is puzzled with the question below:
There are n integers, a1, a2, …, an. The initial values of them are 0. There are four kinds of operations.
Operation 1: Add c to each number between ax and ay inclusive. In other words, do transformation ak<—ak+c, k = x,x+1,…,y.
Operation 2: Multiply c to each number between ax and ay inclusive. In other words, do transformation ak<—ak×c, k = x,x+1,…,y.
Operation 3: Change the numbers between ax and ay to c, inclusive. In other words, do transformation ak<—c, k = x,x+1,…,y.
Operation 4: Get the sum of p power among the numbers between ax and ay inclusive. In other words, get the result of axp+ax+1p+…+ay p.
Yuanfang has no idea of how to do it. So he wants to ask you to help him.
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 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.
5 5
3 3 5 7
1 2 4 4
4 1 5 2
2 2 5 8
4 3 5 3
0 0
307
7489
其实懒标记的思路不难想,关键这题巨烦,需要一定的耐心与经验,不然很容易敲错。
贴上自己拙劣的代码以表纪念:
#include
#include
using namespace std;
#define ll long long
const int maxn = 1e5+10;
const int mod = 10007;
int n, m;
struct node{
int k, b, c;
int p[4];
} tree[maxn*8];
void change(int dex, int k, int b, int l, int r)
{
tree[dex].p[3] = (k*k%mod*k%mod*tree[dex].p[3] + 3*k*k%mod*b%mod*tree[dex].p[2] + 3*k*b%mod*b%mod*tree[dex].p[1] + b*b%mod*b%mod*(r-l+1)%mod) %mod;
tree[dex].p[2] = (k*k%mod*tree[dex].p[2]+2*k*b%mod*tree[dex].p[1]+b*b%mod*(r-l+1))%mod;
tree[dex].p[1] = (k*tree[dex].p[1]+b*(r-l+1))%mod;
}
void pushdown(int l, int r, int dex)
{
int mid = (l+r) >> 1;
if(tree[dex].c){
tree[dex*2].c = tree[dex*2+1].c = tree[dex].c;
tree[dex*2].k = tree[dex*2+1].k = tree[dex].k;
tree[dex*2].b = tree[dex*2+1].b = tree[dex].b;
tree[dex].b = tree[dex].c = 0, tree[dex].k = 1;
int tmp = (tree[dex*2].c*tree[dex*2].k+tree[dex*2].b)%mod, t = 1;
for(int i = 1; i < 4; i++)
t = t*tmp%mod, tree[dex*2].p[i] = t*(mid-l+1)%mod, tree[dex*2+1].p[i] = t*(r-mid)%mod;
}
else{
tree[dex*2].k = tree[dex*2].k*tree[dex].k%mod;
tree[dex*2+1].k = tree[dex*2+1].k*tree[dex].k%mod;
tree[dex*2].b = (tree[dex*2].b*tree[dex].k+tree[dex].b)%mod;
tree[dex*2+1].b = (tree[dex*2+1].b*tree[dex].k+tree[dex].b)%mod;
int k = tree[dex].k, b = tree[dex].b;
tree[dex].k = 1, tree[dex].b = 0;
change(dex*2, k, b, l, mid), change(dex*2+1, k, b, mid+1, r);
}
}
void build(int l, int r, int dex)
{
tree[dex].b = tree[dex].c = 0, tree[dex].k = 1;
memset(tree[dex].p, 0, sizeof(tree[dex].p));
if(l != r){
int mid = (l+r) >> 1;
build(l, mid, dex*2), build(mid+1, r, dex*2+1);
}
}
int query(int l, int r, int dex, int x, int y, int c)
{
if(l >= x && r <= y) return tree[dex].p[c];
else if(l > y || r < x) return 0;
else{
int mid = (l+r) >> 1;
pushdown(l, r, dex);
int res = query(l, mid, dex*2, x, y, c)+ query(mid+1, r, dex*2+1, x, y, c);
for(int i = 1; i <= 3; i++)
tree[dex].p[i] = (tree[dex*2].p[i] + tree[dex*2+1].p[i])%mod;
return res%mod;
}
}
void update(int l, int r, int dex, int x, int y, int c)
{
if(l >= x && r <= y){
tree[dex].c = c; tree[dex].k = 1, tree[dex].b = 0;
int tmp = c;
for(int i = 1; i < 4; i++)
tree[dex].p[i] = (r-l+1)*tmp%mod, tmp = tmp * c %mod;
}
else if(l > y || r < x) return;
else{
int mid = (l+r) >> 1;
pushdown(l, r, dex);
update(l, mid, dex*2, x, y, c), update(mid+1, r, dex*2+1, x, y, c);
for(int i = 1; i < 4; i++)
tree[dex].p[i] = (tree[dex*2].p[i] + tree[dex*2+1].p[i])%mod;
}
}
void mutiply(int l, int r, int dex, int x, int y, int c)
{
if(l >= x && r <= y){
tree[dex].k *= c, tree[dex].b *= c;
tree[dex].k%=mod, tree[dex].b%=mod;
int tmp = c;
for(int i = 1; i < 4; i++)
tree[dex].p[i] = (tree[dex].p[i]*tmp)%mod, tmp = tmp * c %mod;
}
else if(l > y || r < x) return;
else{
int mid = (l+r) >> 1;
pushdown(l, r, dex);
mutiply(l, mid, dex*2, x, y, c), mutiply(mid+1, r, dex*2+1, x, y, c);
for(int i = 1; i < 4; i++)
tree[dex].p[i] = (tree[dex*2].p[i] + tree[dex*2+1].p[i])%mod;
}
}
void add(int l, int r, int dex, int x, int y, int c)
{
if(l >= x && r <= y){
tree[dex].b = (c+tree[dex].b)%mod;
change(dex, 1, c, l, r);
}
else if(l > y || r < x) return;
else{
int mid = (l+r) >> 1;
pushdown(l, r, dex);
add(l, mid, dex*2, x, y, c), add(mid+1, r, dex*2+1, x, y, c);
for(int i = 1; i < 4; i++)
tree[dex].p[i] = (tree[dex*2].p[i] + tree[dex*2+1].p[i])%mod;
}
}
int main()
{
int x, y, c, cmd;
while(scanf("%d%d", &n, &m), n||m){
build(1, n, 1);
while(m--){
scanf("%d%d%d%d", &cmd, &x, &y, &c);
if(cmd == 4) printf("%d\n", query(1, n, 1, x, y, c));
else if(cmd == 3) update(1, n, 1, x, y, c);
else if(cmd == 2) mutiply(1, n, 1, x, y, c);
else add(1, n, 1, x, y, c);
}
}
}