我是靠谱客的博主 美满帽子,这篇文章主要介绍【HDU 4578 --- Transformation】线段树,现在分享给大家,希望可以做个参考。

【HDU 4578 --- Transformation】


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

307
7489

解题思路

通过flag数组记录线段树中区间[l,r]中的数值是否一样,后续查询时只需要算出当前节点的值然后乘以(r-l+1)就可以直接得到这个区间的值。

AC代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream> #include <algorithm> #include <cstring> 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; }

最后

以上就是美满帽子最近收集整理的关于【HDU 4578 --- Transformation】线段树的全部内容,更多相关【HDU内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(132)

评论列表共有 0 条评论

立即
投稿
返回
顶部