FZU 1608 Huge Mission(线段树)

Problem 1608 Huge Mission


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 <cstdio>

#include <algorithm>

using namespace std;



#define lson l,m,rt<<1

#define rson m+1,r,rt<<1|1

#define root 1,N,1

const int maxn=55555;

int col[maxn<<2];



void PushDown(int rt)//向下更新

{

    if(col[rt])

    {

        int ls=rt<<1;

        int rs=ls+1;

        col[ls]=max(col[ls],col[rt]);

        col[rs]=max(col[rs],col[rt]);

        col[rt]=0;

    }

}



void build(int l,int r,int rt)

{

    col[rt]=0;

    if(l==r) return ;

    int m=(l+r)>>1;

    build(lson);

    build(rson);

}



void update(int L,int R,int c,int l,int r,int rt)

{

    if(L<=l&&R>=r)

    {

        col[rt]=max(col[rt],c);

        return ;

    }

    PushDown(rt);//down

    int m=(l+r)>>1;

    if(L<=m)

        update(L,R,c,lson);

    if(R>m)

        update(L,R,c,rson);

}



int get_ans(int l,int r,int rt)

{

    if(l==r) return col[rt];

    PushDown(rt);

    int m=(l+r)>>1;

    return get_ans(lson)+get_ans(rson);

}



int solve()

{

    int N,M;

    while(~scanf("%d%d",&N,&M))

    {

        build(root);

        while(M--)

        {

            int a,b,c;

            scanf("%d%d%d",&a,&b,&c);

            if(c) update(a+1,b,c,root);

        }

        printf("%d\n",get_ans(root));

    }

    return 0;

}



int main()

{

    return solve();

}

你可能感兴趣的:(SSI)