FZU 1608 Huge Mission ——————线段树,维护区间最大值

Problem 1608 Huge Mission

Accept: 491    Submit: 1274
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

Sample Output


132
13
108


抄的板子,,以后得好好理解


#include
#include
#include
#include
using namespace std;
const int MAXN=5e5+10;
const int INF=0x3f3f3f3f;
struct Node{
    int l,r,date;
    int lazy;
}node[MAXN<<2];

void upf(int now)//update_father
{
    node[now].date = node[now<<1].date + node[now<<1|1].date;//夫亲节点的值更新为儿子值的和
}

void bt(int now,int l,int r)//build_tree 建树
{
    node[now].l=l;
    node[now].r=r;
    node[now].date=0;
    node[now].lazy=0;
    if(l==r)
        return ;
    int mid=(l+r)>>1;
    bt(now<<1, l, mid);
    bt(now<<1|1, mid+1, r);
    upf(now);
}
void upt(int now, int l, int r, int val)// update_tree 更新树
{
    if(l <= node[now].l && node[now].r <= r)
    {
        node[now].lazy = max(node[now].lazy, val);
        return ;
    }
    if(node[now].r < l || node[now].l > r)     return ;
    upt(now<<1, l, r, val);
    upt(now<<1|1, l, r, val);
}
void query(int now, int l, int r)// 查询
{
    if(l == r)
    {
        node[now].date = max(node[now].date,node[now].lazy);
        return ;
    }
    if(node[now].lazy != 0)//
    {
        node[now<<1].lazy = max(node[now].lazy, node[now<<1].lazy);
        node[now<<1|1].lazy = max(node[now].lazy, node[now<<1|1].lazy);
    }
    int mid = (l+r)>>1;
    query(now<<1, l, mid);
    query(now<<1|1, mid+1,r);
    upf(now);
}

int main()
{
    int n, m, x, y, v;
    while(~scanf("%d %d",&n, &m))
    {
        bt(1,1,n);
        while(m--)
        {
            scanf("%d %d %d",&x, &y, &v);
            upt(1,x+1,y,v);
        }
        query(1, 1, n);
        printf("%d\n",node[1].date);
    }
    return 0;
}






















你可能感兴趣的:(线段树)