hdu 1698 Just a Hook

线段树求整个区间的和
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <vector>
#include <cstring>
#include <algorithm>

#define INF 0x3fffffff
#define N 100010
#define M (N << 2)
#define LL long long
#define mod 95041567

using namespace std;

struct Node{
    int set;
    int sum;
    int l, r;
};

Node p[M];

void build(int rt, int l, int r){
    p[rt].l = l;
    p[rt].r = r;
    p[rt].set = 0;
    if(l == r){
        p[rt].sum = 1;
        return ;
    }
    int mid = (r - l) / 2 + l;
    int lc = rt << 1;
    int rc = lc + 1;
    build(lc, l, mid);
    build(rc, mid + 1, r);
    p[rt].sum = p[lc].sum + p[rc].sum;
}

void pushdown(int rt){
    if(! p[rt].set) return ;
    int lc = rt << 1;
    int rc = lc + 1;
    p[lc].set = p[rc].set = p[rt].set;
    p[lc].sum = p[lc].set * (p[lc].r - p[lc].l + 1);
    p[rc].sum = p[rc].set * (p[rc].r - p[rc].l + 1);
    p[rt].set = 0;
}

void update(int rt, int l, int r, int L, int R, int val){
    if(l == L && r == R) {
        p[rt].set = val;
        p[rt].sum = (R - L + 1) * val;
        return ;
    }
    pushdown(rt);
    int mid = (r - l) / 2 + l;
    int lc = rt << 1;
    int rc = lc + 1;
    if(R <= mid) update(lc, l, mid, L, R, val);
    else if(L > mid) update(rc, mid + 1, r, L, R, val);
    else {
        update(lc, l, mid, L, mid, val);
        update(rc, mid + 1, r, mid + 1, R, val);
    }
    p[rt].sum = p[lc].sum + p[rc].sum;
}

int main() {
   // freopen("in.txt", "r", stdin);
    int n, m, t, cnt = 0;
    scanf("%d", &t);
    while(t --){
        scanf("%d %d", &n, &m);
        build(1, 1, n);
        while(m --){
            int i, j, val;
            scanf("%d %d %d", &i, &j, &val);
            update(1, 1, n, i, j, val);
        }
        printf("Case %d: The total value of the hook is %d.\n", ++ cnt, p[1].sum);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值