FZU 1608 Huge Mission (线段数区间更新求和)

Description

Oaiei is busy working with his graduation design recently. If he can not complete it before the end of the month, and he can not graduate! He will be very sad with that, and he needs your help. There are 24 hours a day, oaiei has different efficiency in different time periods, such as from 0 o’clock to 8 o'clock his working efficiency is one unit per hour, 8 o'clock to 12 o'clock his working efficiency is ten units per hour, from 12 o'clock to 20 o'clock his working efficiency is eight units per hour, from 20 o'clock to 24 o'clock his working efficiency is 5 units per hour. Given you oaiei’s working efficiency in M periods of time and the total time N he has, can you help him calculate his greatest working efficiency in time N.

Input

There are multiple tests. In each test the first line has two integer N (2 <= N <= 50000) and M (1 <= M <= 500000), N is the length of oaiei’s working hours; M is the number of periods of time. The following M lines, each line has three integer S, T, P (S < T, 0 < P <= 200), represent in the period of time from S to T oaiei’s working efficiency is P units per hour. If we do not give oaiei’s working efficiency in some periods of time, his working efficiency is zero. Oaiei can choose part of the most effective periods of time to replace the less effective periods of time. For example, from 5 o’clock to 10 o’clock his working efficiency is three units per hour and from 1 o’clock to 7 o’clock his working efficiency is five units per hour, he can choose working with five units per hour from 1 o’clocks to 7 o’clock and working with three units per hour from 7 o’clock to 10 o’clock.

Output

You should output an integer A, which is oaiei’s greatest working efficiency in the period of time from 0 to N.

Sample Input

24 4
0 8 1
8 12 10
12 20 8
20 24 5
4 3
0 3 1
1 2 2
2 4 5
10 10
8 9 15
1 7 5
5 10 3
0 7 6
5 8 2
3 7 3
2 9 12
7 8 14
6 7 2
5 6 16

Sample Output

132
13
108

线段数区间更新求和,需优化
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 50000 + 5;
struct tree {
    int max_val;
    int lh, rh;
    void exp_max(int val) {
        max_val = max(max_val, val);
    }
} T[maxn<<2];

void Build(int root, int l, int r)
{
    T[root].max_val = 0;
    T[root].lh = l;
    T[root].rh = r;
    if(l + 1 >= r) return;
    int mid = (l + r) >> 1;
    Build(root<<1, l, mid);
    Build(root<<1|1, mid, r);
}

void Insert(int root, int l, int r, int val)
{
    if(T[root].rh <= l || T[root].lh >= r) return;
    if(T[root].lh >= l && T[root].rh <= r) {
        T[root].exp_max(val);
        return;
    }
    Insert(root<<1, l, r, val);
    Insert(root<<1|1, l, r, val);
}

int Query(int root, int l, int r)
{
    if(l + 1 >= r) return T[root].max_val;
    T[root<<1].exp_max(T[root].max_val);
    T[root<<1|1].exp_max(T[root].max_val);
    int mid = (l + r) >> 1;
    return Query(root<<1, l, mid) + Query(root<<1|1, mid, r);
}

int main()
{
//    freopen("in", "r", stdin);
    int n, m;
    while(~scanf("%d%d", &n, &m)) {
        memset(T, 0, sizeof(T));
        Build(1, 0, n);
        int s, t, p;
        for(int i = 0; i < m; ++i) {
            scanf("%d%d%d", &s, &t, &p);
            Insert(1, s, t, p);
        }
        printf("%d\n", Query(1, 0, n));
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值