Huge Mission (区间更新板子题 *)

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

 

线段树的区间更新:

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

const int MAXN = 5e5+10;
int sum[MAXN<<2];
void Build(int i,int l,int r){
    sum[i]=0;
    if(l==r){
        return ;
    }
    int m=(l+r)>>1;
    Build(i<<1,l,m);
    Build(i<<1|1,m+1,r);
}

void PushDown(int i){
    if(sum[i]){
        sum[i<<1]=max(sum[i<<1],sum[i]);
        sum[i<<1|1]=max(sum[i<<1|1],sum[i]);
        sum[i]=0;
    }
}

void Update(int a,int b,int c,int i,int l,int r){
    if(a<=l&&b>=r){
        sum[i]=max(sum[i],c);
        return ;
    }
    PushDown(i);
    int m=(l+r)>>1;
    if(a<=m) Update(a,b,c,i<<1,l,m);
    if(b>m) Update(a,b,c,i<<1|1,m+1,r);
}

int Query(int i,int l,int r){
    if(l==r) return sum[i];
    PushDown(i);
    int m=(l+r)>>1;
    return Query(i<<1,l,m)+Query(i<<1|1,m+1,r);
}

int main(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        Build(1,1,n);
        while(m--){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            if(c) Update(a+1,b,c,1,1,n);
        }
        printf("%d\n",Query(1,1,n));
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值