Huge Mission(FOJ-1608)

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

题意:

给定24小时内的各段的工作效率,求总共的工作效率值。

思路:

线段树。这道题乍一看是区间更新,但实际上思考了一下应该是区间内的点修改。因为最后计算总最大效率的时候由于同一段时间可能有不同的效率值,所以应该贪心尽量取大的。
这样子思路就很清晰了,建树时将所有节点的value置为0,之后每次更新叶节点的值,与原先的值进行对比取大的那一个。
查询的时候就是从根节点开始向叶节点递归,统计所有叶节点的value值。
坑点的话就是线段树的数据范围尽量往大开,一开始比较保守只开了50100 * 4结果re了……开成5500 * 4就过了。还有的话就是按照题目里说的
计算方式,[x,x]的结果应该是0,所以建树时的叶节点应该是[x,x+1],这样子返回的才是value。

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdio>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <list>
#include <algorithm>
#define MST(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int MAXN = 50000 + 5000;
struct tree {
    int left, right, value;
} T[MAXN << 2];
void build(int x, int l, int r) {
    T[x].left = l;
    T[x].right = r;
    T[x].value = 0;
    if (l + 1 == r) return ;
    int m = (l + r) >> 1;
    build(x << 1, l, m);
    build(x << 1 | 1, m, r);
}
void update(int x, int l, int r, int v) {
    if (T[x].left > r || T[x].right < l) return ;
    if (T[x].left >= l && T[x].right <= r) {
        T[x].value = max(T[x].value, v);
        return ;
    }
    update(x << 1, l, r, v);
    update(x << 1 | 1, l, r, v);
}
int query(int x, int l, int r) {
    if (l + 1 == r) return T[x].value;
    T[x << 1].value = max(T[x << 1].value, T[x].value);
    T[x << 1 | 1].value = max(T[x << 1 | 1].value, T[x].value);
    int m = (l + r) >> 1;
    return query(x << 1, l, m) + query(x << 1 | 1, m, r);
}
int n, m;
int main() {
    while (scanf("%d %d", &n, &m) != EOF) {
        MST(T, 0);
        build(1, 0, n);
        while (m--) {
            int s, t, p;
            scanf("%d %d %d", &s, &t, &p);
            update(1, s, t, p);
        }
        for (int i = 1; i <= n * 4; i++)
            printf("%d: %d %d %d\n", i, T[i].left, T[i].right, T[i].value);
        printf("%d\n", query(1, 0, n));
    }
}

猜你喜欢

转载自blog.csdn.net/white_yasha/article/details/80450359