线段树(成段更新) HDU 1698 Just a Hook

 

题目传送门

 1 /*  2  线段树-成段更新:第一题!只要更新区间,输出总长度就行了  3  虽然是超级裸题,但是用自己的风格写出来,还是很开心的:)  4 */  5 #include <cstdio>  6 #include <algorithm>  7 #include <cmath>  8 #include <cstring>  9 #include <string>  10 #include <iostream>  11 using namespace std;  12  13 #define lson l, mid, rt << 1  14 #define rson mid + 1, r, rt << 1 | 1  15  16 const int MAXN = 1e5 + 10;  17 const int INF = 0x3f3f3f3f;  18 struct Node  19 {  20 int v, sum, add, mx, mn, len;  21 }node[MAXN<<2];  22  23 void push_up(int rt)  24 {  25 node[rt].sum = node[rt<<1].sum + node[rt<<1|1].sum;  26 }  27  28 void push_down(int rt, int c)  29 {  30 if (node[rt].add)  31  {  32 node[rt<<1].add = node[rt<<1|1].add = node[rt].add;  33 node[rt<<1].sum = node[rt<<1].add * (c - (c >> 1));  34 node[rt<<1|1].sum = node[rt<<1|1].add * (c >> 1);  35 node[rt].add = 0;  36  }  37 }  38  39 void build(int l, int r, int rt)  40 {  41 node[rt].add = 0;  42 if (l == r) {node[rt].sum = 1; return ;}  43 int mid = (l + r) >> 1;  44  build (lson); build (rson);  45  46  push_up (rt);  47 }  48  49 void updata(int ql, int qr, int c, int l, int r, int rt)  50 {  51 if (ql <= l && r <= qr) {node[rt].sum = c * (r - l + 1); node[rt].add = c; return ;}  52  53 push_down (rt, r - l + 1);  54  55 int mid = (l + r) >> 1;  56 if (ql <= mid) updata (ql, qr, c, lson);  57 if (qr > mid) updata (ql, qr, c, rson);  58  59  push_up (rt);  60 }  61  62 int query(int ql, int qr, int l, int r, int rt)  63 {  64 if (ql <= l && r <= qr) {return node[rt].sum;}  65  66 push_down (rt, r - l + 1);  67  68 int mid = (l + r) >> 1; int ans = 0;  69 if (ql <= mid) ans = query (ql, qr, lson);  70 if (qr > mid) ans = query (ql, qr, rson);  71  72 return ans;  73 }  74  75 int main(void) //HDU 1698 Just a Hook  76 {  77 //freopen ("HDOJ_1698.in", "r", stdin);  78  79 int t, cas = 0;  80 scanf ("%d", &t);  81 while (t--)  82  {  83 int n, q;  84 scanf ("%d", &n);  85 build (1, n, 1);  86  87 scanf ("%d", &q);  88 while (q--)  89  {  90 int ql, qr, c;  91 scanf ("%d%d%d", &ql, &qr, &c);  92 updata (ql, qr, c, 1, n, 1);  93  }  94  95 printf ("Case %d: The total value of the hook is %d.\n", ++cas, node[1].sum);  96  }  97  98 return 0;  99 } 100 101 /* 102 Case 1: The total value of the hook is 24. 103 */

 

你可能感兴趣的:(HDU)