我是靠谱客的博主 笑点低舞蹈,这篇文章主要介绍HDOJ 1698 Just a Hook,现在分享给大家,希望可以做个参考。

题意:给出n组数据,每组第一行表示数的范围,第二行表示有k组数据,接下来k行,每行有三个数,l,r,t,将l->r范围内的所有数变成t,求最终的这个数列的和

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698

思路:线段树成段更新,查询树根。

注意点:裸线段树,第一次cin没关闭流同步TLE了一次。


以下为AC代码:

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
126168062014-12-30 19:13:23Accepted16981045MS3784K2260 BG++luminous11
复制代码
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
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <vector> #include <deque> #include <list> #include <cctype> #include <algorithm> #include <climits> #include <queue> #include <stack> #include <cmath> #include <map> #include <set> #include <iomanip> #include <cstdlib> #include <ctime> #define ll long long #define ull unsigned long long #define all(x) (x).begin(), (x).end() #define clr(a, v) memset( a , v , sizeof(a) ) #define pb push_back #define mp make_pair #define read(f) freopen(f, "r", stdin) #define write(f) freopen(f, "w", stdout) using namespace std; int num[100005<<2]; int cnt[100005<<2]; inline void pushdown ( int rt, int len ) { if ( cnt[rt] > 0 ) { cnt[rt<<1] = cnt[rt<<1|1] = cnt[rt]; num[rt<<1] = ( len - ( len >> 1 ) ) * cnt[rt]; num[rt<<1|1] = ( len >> 1 ) * cnt[rt]; cnt[rt] = 0; } } void update ( int L, int R, int l, int r, int rt, int n ) { if ( L <= l && r <= R ) { cnt[rt] = n; num[rt] = ( r - l + 1 ) * n; return; } else { pushdown ( rt, r - l + 1 ); int mid = ( l + r ) >> 1; if ( L <= mid ) { update ( L, R, l, mid, rt << 1, n ); } if ( mid < R ) { update ( L, R, mid + 1, r, rt << 1 | 1, n ); } num[rt] = num[rt<<1] + num[rt<<1|1]; } } void build ( int l, int r, int rt ) { cnt[rt] = 0; if ( l == r )num[rt] = 1; else { int mid = ( l + r ) >> 1; build ( l, mid, rt << 1 ); build ( mid + 1, r, rt << 1 | 1 ); num[rt] = num[rt<<1] + num[rt<<1|1]; } } int main() { ios::sync_with_stdio( false ); int ncase; cin >> ncase; for ( int nc = 1; nc <= ncase; nc ++ ) { int m, k, l, r, n; memset ( num, 0, sizeof ( num ) ); cin >> m >> k; build ( 1, m, 1 ); for ( int i = 0; i < k; i ++ ) { cin >> l >> r >> n; update ( l, r, 1, m, 1, n ); } cout << "Case " << nc << ": The total value of the hook is " << num[1] << "." << endl; } return 0; }


最后

以上就是笑点低舞蹈最近收集整理的关于HDOJ 1698 Just a Hook的全部内容,更多相关HDOJ内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部