FZU 1608 Huge Mission (线段树区间更新 + 贪心)

来源:互联网 发布:java初学者注意什么 编辑:程序博客网 时间:2024/06/06 20:07
Problem 1608 Huge Mission

Accept: 443    Submit: 1156
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem 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
2 3
0 1 2
0 2 3
1 2 5

 Sample Output

132
13
108

 Source

FOJ月赛-2008年5月

题目链接:http://acm.fzu.edu.cn/problem.php?pid=1608

题目大意:x y val表示大线段x到y间的所有长度为1的小线段值可以为val, 求最后求把n段全加起来的最大值

题目分析:对区间贪心选择,如果比之前的值大则更新,要注意贪心之前需先排序,原因看第三个样例就知道了,然后线段树区间更新搞一下,还有就是因为是段操作不是点操作,要对左端点加1

#include <cstdio>#include <cstring>#include <algorithm>#define lson l, mid, rt << 1#define rson mid + 1, r, rt << 1 | 1using namespace std;int const MAX = 50005;int sum[MAX << 2], lazy[MAX << 2];int n, q;struct Seg{int l, r, c;}seg[MAX * 10];bool cmp(Seg a, Seg b){return a.c < b.c;}void PushUp(int rt){sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];}void PushDown(int ln, int rn, int rt){if(lazy[rt]){sum[rt << 1] = lazy[rt] * ln;sum[rt << 1 | 1] = lazy[rt] * rn;lazy[rt << 1] = lazy[rt];lazy[rt << 1 | 1] = lazy[rt];lazy[rt] = 0;}return;}void Build(){memset(sum, 0, sizeof(sum));memset(lazy, 0, sizeof(lazy));}void Update(int L, int R, int c, int l, int r, int rt){if(L <= l && r <= R){if(sum[rt] < c * (r - l + 1)){sum[rt] = c * (r - l + 1);lazy[rt] = c;}return;}int mid = (l + r) >> 1;PushDown(mid - l + 1, r - mid, rt);if(L <= mid)Update(L, R, c, lson);if(mid < R)Update(L, R, c, rson);PushUp(rt);return;}int main(){while(scanf("%d %d", &n, &q) != EOF){Build();for(int i = 0; i < q; i++)scanf("%d %d %d", &seg[i].l, &seg[i].r, &seg[i].c);sort(seg, seg + q, cmp);for(int i = 0; i < q; i++)Update(seg[i].l + 1, seg[i].r, seg[i].c, 1, n, 1);printf("%d\n", sum[1]);}}



0 0