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));
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章