Transformation HDU - 4578(线段树——懒惰标记的妙用)

时间:2021-07-27 05:29:26

Transformation HDU - 4578(线段树——懒惰标记的妙用)

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. 

InputThere 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. 
OutputFor 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(懒惰标记)表示该区间的元素是否是相同的,v表示这个区间里面元素的值;
然后再通过合理的pushdown和pushup操作便可以快速的实现对于区间的统计工作。这比网上其他的思路(统计sum1,sum2, sum3.....)快很多。 代码:
 #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + ;
const int mod = ;
struct node
{
int l, r;
int v;
bool flag;
}t[maxn << ];
int n, m; void pushup(int tar)
{
if (t[tar << ].flag && t[tar << | ].flag && t[tar << ].v == t[tar << | ].v)
t[tar].flag = true, t[tar].v = t[tar << ].v;
else t[tar].flag = false;
} void pushdown(int tar)
{
if (t[tar].flag)
{
t[tar << ].flag = t[tar << | ].flag = true;
t[tar << ].v = t[tar << | ].v = t[tar].v;
t[tar].flag = false;
}
} void build(int l, int r, int tar)
{
t[tar].l = l, t[tar].r = r, t[tar].v = , t[tar].flag = true;
if (l == r) return;
int mid = (l + r) >> ;
build(l, mid, tar << );
build(mid + , r, tar << | );
} void update(int ope, int l, int r, int v, int tar)
{
if (t[tar].l == l && t[tar].r == r && t[tar].flag == true)
{
if (ope == ) t[tar].v += v;
else if (ope == ) t[tar].v *= v;
else t[tar].v = v;
t[tar].v %= mod;
return;
}
pushdown(tar);
int mid = (t[tar].l + t[tar].r) >> ;
if (r <= mid) update(ope, l, r, v, tar << );
else if (l > mid) update(ope, l, r, v, tar << | );
else update(ope, l, mid, v, tar << ), update(ope, mid + , r, v, tar << | );
pushup(tar);
} int query(int l, int r, int v, int tar)
{
if (t[tar].l == l && t[tar].r == r && t[tar].flag == true)
{
int res = ;
for (int i = ; i < v; i++)
res *= t[tar].v, res %= mod;
return res * (t[tar].r - t[tar].l + ) % mod;
}
pushdown(tar);
int mid = (t[tar].l + t[tar].r) >> ;
if (r <= mid) return query(l, r, v, tar << );
else if (l > mid) return query(l, r, v, tar << | );
else return (query(l, mid, v, tar << ) + query(mid + , r, v, tar << | )) % mod;
} int main()
{
int ope, x, y, c; while (cin >> n >> m && n && m)
{
build(, n, );
while (m--)
{
scanf("%d%d%d%d", &ope, &x, &y, &c);
if (ope != )
update(ope, x, y, c, );
else cout << query(x, y, c, ) << endl;
}
}
}