HDU 1698 Just a Hook
Posted zaq19970105
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1698 Just a Hook相关的知识,希望对你有一定的参考价值。
题目链接:https://vjudge.net/problem/HDU-1698
题目大意:
给定一个 N 个数的序列,初始全为1,现在进行Q次操作,每次操作把 [L, R] 区间内的所有数变为 x,求操作完成后序列的总和。
分析:
线段树成段更新模板题。
代码如下:
1 #pragma GCC optimize("Ofast") 2 #include <bits/stdc++.h> 3 using namespace std; 4 5 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0); 6 #define Rep(i,n) for (int i = 0; i < (n); ++i) 7 #define For(i,s,t) for (int i = (s); i <= (t); ++i) 8 #define rFor(i,t,s) for (int i = (t); i >= (s); --i) 9 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i) 10 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i) 11 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i) 12 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) 13 14 #define pr(x) cout << #x << " = " << x << " " 15 #define prln(x) cout << #x << " = " << x << endl 16 17 #define LOWBIT(x) ((x)&(-x)) 18 19 #define ALL(x) x.begin(),x.end() 20 #define INS(x) inserter(x,x.begin()) 21 22 #define ms0(a) memset(a,0,sizeof(a)) 23 #define msI(a) memset(a,inf,sizeof(a)) 24 #define msM(a) memset(a,-1,sizeof(a)) 25 26 #define MP make_pair 27 #define PB push_back 28 #define ft first 29 #define sd second 30 31 template<typename T1, typename T2> 32 istream &operator>>(istream &in, pair<T1, T2> &p) { 33 in >> p.first >> p.second; 34 return in; 35 } 36 37 template<typename T> 38 istream &operator>>(istream &in, vector<T> &v) { 39 for (auto &x: v) 40 in >> x; 41 return in; 42 } 43 44 template<typename T1, typename T2> 45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) { 46 out << "[" << p.first << ", " << p.second << "]" << "\n"; 47 return out; 48 } 49 50 typedef long long LL; 51 typedef unsigned long long uLL; 52 typedef pair< double, double > PDD; 53 typedef pair< int, int > PII; 54 typedef pair< LL, LL > PLL; 55 typedef set< int > SI; 56 typedef vector< int > VI; 57 typedef map< int, int > MII; 58 typedef vector< LL > VL; 59 typedef vector< VL > VVL; 60 const double EPS = 1e-10; 61 const int inf = 1e9 + 9; 62 const LL mod = 1e9 + 7; 63 const int maxN = 1e5 + 7; 64 const LL ONE = 1; 65 const LL evenBits = 0xaaaaaaaaaaaaaaaa; 66 const LL oddBits = 0x5555555555555555; 67 68 int T, N, Q; 69 70 #define lson l , mid , rt << 1 71 #define rson mid + 1 , r , rt << 1 | 1 72 struct SegmentTree{ 73 int st[maxN << 2]; 74 int lazy[maxN << 2]; // 懒惰标记,lazy[i] = la 表示节点 i 尚未向下更新 la 75 76 inline void pushUp(int rt) { 77 st[rt] = st[rt << 1] + st[rt << 1 | 1]; 78 } 79 80 inline void pushDown(int rt, int l, int mid, int r) { 81 if(lazy[rt]) { 82 st[rt << 1] = lazy[rt] * (mid - l + 1); 83 st[rt << 1 | 1] = lazy[rt] * (r - mid); 84 lazy[rt << 1] = lazy[rt << 1 | 1] = lazy[rt]; 85 lazy[rt] = 0; 86 } 87 } 88 89 inline void build(int l, int r, int rt) { 90 if(l >= r) { 91 st[rt] = 1; 92 return; 93 } 94 int mid = (l + r) >> 1; 95 build(lson); 96 build(rson); 97 pushUp(rt); 98 } 99 100 // 成段更新 101 inline void update(int L, int R, int x, int l, int r, int rt) { 102 if(L <= l && r <= R) { 103 // 不更新到底,标记一下表明下面的还没更新 104 lazy[rt] = x; 105 st[rt] = x * (r - l + 1); 106 return; 107 } 108 int mid = (l + r) >> 1; 109 pushDown(rt, l, mid, r); // 如果孩子节点有上一次还没更新的,就更新它,同时转移懒惰标记 110 111 if(L <= mid) update(L, R, x, lson); 112 if(R > mid) update(L, R, x, rson); 113 pushUp(rt); 114 } 115 }; 116 SegmentTree segTr; 117 118 int main(){ 119 INIT(); 120 cin >> T; 121 For(i, 1, T) { 122 cin >> N >> Q; 123 segTr.build(1, N, 1); 124 ms0(segTr.lazy); 125 int L, R, x; 126 Rep(j, Q) { 127 cin >> L >> R >> x; 128 segTr.update(L, R, x, 1, N, 1); 129 } 130 cout << "Case " << i <<": The total value of the hook is " << segTr.st[1] << "." << endl; 131 } 132 return 0; 133 }
以上是关于HDU 1698 Just a Hook的主要内容,如果未能解决你的问题,请参考以下文章