HDU 1698 Just a Hook

线段树模板题。

代码:

#include <cstdio>  
#include <cstring>  
#include <iostream>  
#include <algorithm>  
  
using namespace std;  
  
const int maxn = 1e5 + 10;  
  
int T, n, q, date[maxn], sum[maxn<<2], lazy[maxn<<2];  
  
void init() {  
    for (int i = 1; i <= n; i++) {  
        date[i] = 1;  
    }  
    for (int i = 1; i <= n<<2; i++) {  
        sum[i] = 0;  
        lazy[i] = 0;  
    }  
}  
  
void pushup(int root) {  
    sum[root] = sum[root<<1] + sum[root<<1|1];  
}  
  
void pushdown(int root, int cntL, int cntR) {  
    if (lazy[root]) {  
        lazy[root<<1] = lazy[root];  
        lazy[root<<1|1] = lazy[root];  
        sum[root<<1] = lazy[root] * cntL;  
        sum[root<<1|1] = lazy[root] * cntR;  
        lazy[root] = 0;  
    }  
}  
  
void build(int L, int R, int root) {  
    if (L == R) {  
        sum[root] = date[L];  
        return;  
    }  
    int mid = (L + R)>>1;  
    build(L, mid, root<<1);  
    build(mid + 1, R, root<<1|1);  
    pushup(root);  
}  
  
void update_interval(int L, int R, int root, int uL, int uR, int val) {  
    if (uL <= L && R <= uR) {  
        sum[root] = val * (R - L + 1);  
        lazy[root] = val;  
        return;  
    }  
    int mid = (L + R)>>1;  
    pushdown(root, mid - L + 1, R - mid);  
    if (uL <= mid) {  
        update_interval(L, mid, root<<1, uL, uR, val);  
    }  
    if (uR > mid) {  
        update_interval(mid + 1, R, root<<1|1, uL, uR, val);  
    }  
    pushup(root);  
}  
  
int query(int L, int R, int root, int qL, int qR) {  
    if (qL <= L && R <= qR) {  
        return sum[root];  
    }  
    int mid = (L + R)>>1;  
    pushdown(root, mid - L + 1, R - mid);  
    int ans = 0;  
    if (qL <= mid) {  
        ans += query(L, mid, root<<1, qL, qR);  
    }  
    if (qR > mid) {  
        ans += query(mid + 1, R, root<<1|1, qL, qR);  
    }  
    return ans;  
}  
  
int main()  
{  
    scanf("%d", &T);  
    for (int kase = 1; kase <= T; kase++) {  
        scanf("%d%d", &n, &q);  
        init();  
        build(1, n, 1);  
        while (q--) {  
            int a, b, c; scanf("%d %d %d", &a, &b, &c);  
            update_interval(1, n, 1, a, b, c);  
        }  
        printf("Case %d: The total value of the hook is %d.\n", kase, query(1, n, 1, 1, n));  
    }  
    return 0;  
} 


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值