hdu1698Just a Hook(线段树区间修改)

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

非常简单的线段树的区间修改问题,稍微不同的就是有三种标记,不同的标记按题意说的更新就行。剩下的套模板就行了。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<cmath>
  5 #include<cstring>
  6 #define ll long long
  7 using namespace std;
  8 const int MAXN = 100010;
  9 
 10 struct {
 11     int sum;
 12     //int cu, ag, au;
 13     int lazy;
 14 }Tree[MAXN << 2];
 15 
 16 int A[MAXN];
 17 //ll Add[MAXN << 2], Sum[MAXN << 2];
 18 int n, m;
 19 
 20 void PushUp(int rt) {
 21     Tree[rt].sum = Tree[rt << 1].sum + Tree[rt << 1 | 1].sum;
 22 }
 23 void Build(int l, int r, int rt) {
 24     if (l == r) {
 25         Tree[rt].sum = A[l];
 26         return;
 27     }
 28     int m = (l + r) >> 1;
 29     Build(l, m, rt << 1);
 30     Build(m + 1, r, rt << 1 | 1);
 31     PushUp(rt);
 32 }
 33 void PointUpdate(int L, int C, int l, int r, int rt) {
 34     if (l = r) {
 35         Tree[rt].sum += C;
 36         return;
 37     }
 38     int m = (l + r) >> 1;
 39     if (L <= m) PointUpdate(L, C, l, m, rt << 1);
 40     else PointUpdate(L, C, m + 1, r, rt << 1 | 1);
 41     PushUp(rt);
 42 }
 43 void PushDown(int rt, int ln, int rn) {
 44     if (Tree[rt].lazy == 0) return;
 45     if (Tree[rt].lazy == 1) {
 46         Tree[rt << 1].sum = ln;
 47         Tree[rt << 1 | 1].sum = rn;
 48         Tree[rt << 1].lazy = Tree[rt << 1 | 1].lazy = 1;
 49     }
 50     else if (Tree[rt].lazy == 2) {
 51         Tree[rt << 1].sum = ln * 2;
 52         Tree[rt << 1 | 1].sum = rn * 2;
 53         Tree[rt << 1].lazy = Tree[rt << 1 | 1].lazy = 2;
 54     }
 55     else {
 56         Tree[rt << 1].sum = ln * 3;
 57         Tree[rt << 1 | 1].sum = rn * 3;
 58         Tree[rt << 1].lazy = Tree[rt << 1 | 1].lazy = 3;
 59     }
 60     Tree[rt].lazy = 0;
 61 }
 62 void QjUpdate(int L, int R, int C, int l, int r, int rt) {
 63     if (l >= L && r <= R) {
 64         Tree[rt].sum = (C * (r - l + 1));
 65         Tree[rt].lazy = C;
 66         return;
 67     }
 68     int m = (l + r) >> 1;
 69     PushDown(rt, m - l + 1, r - m);
 70     if (m >= L) QjUpdate(L, R, C, l, m, rt << 1);
 71     if (m < R) QjUpdate(L, R, C, m + 1, r, rt << 1 | 1);
 72     PushUp(rt);
 73 }
 74 ll Query(int L, int R, int l, int r, int rt) {
 75     if (l >= L && r <= R) {
 76         return Tree[rt].sum;
 77     }
 78     int m = (l + r) >> 1;
 79     PushDown(rt, m - l + 1, r - m);
 80     ll ANS = 0;
 81     if (m >= L) ANS += Query(L, R, l, m, rt << 1);
 82     if (m < R) ANS += Query(L, R, m + 1, r, rt << 1 | 1);
 83     return ANS;
 84 }
 85 
 86 int main() {
 87 
 88     ios::sync_with_stdio(false);
 89     cin.tie(0);
 90 
 91     int T;
 92     cin >> T;
 93     int cnt = 1;
 94     while (T--) {
 95         //cout << "Case " << cnt++ << ": The total value of the hook is ";
 96         //memset(Sum, 0, sizeof(Sum));
 97         memset(Tree, 0, sizeof(Tree));
 98         cin >> n >> m;
 99         //if (n == 0) break;
100         for (int i = 1; i <= n; i++) {
101             A[i] = 1;
102         }
103         Build(1, n, 1);
104         for (int i = 1; i <= m; i++) {
105             int a, b, val;
106             cin >> a >> b >> val;
107             //if(val == 3)
108             QjUpdate(a, b, val, 1, n, 1);
109         }
110         cout << "Case " << cnt++ << ": The total value of the hook is ";
111         cout << Query(1, n, 1, n, 1) << ".\n";
112     }
113     return 0;
114 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值