Huge Mission
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
题意:给定M个区间 (1 <= M <= 500000)和这些区间上的权值,求最终并区间的最大权值总和(某个区间不能被计算两次)。
思路:用一个maxx数组维护区间最大值(最后查询的时候push_down到叶子),用一个maxx数组也表示这个区间是否都是相同的数, != -1表示相同。那么当 maxx[i] != -1时,直接更新,获得最大值。否则继续递归,可以加入一个剪枝条件,如果此区间 maxx[i] >= 要比较的val,直接return
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define INT(t) int t; scanf("%d",&t)
using namespace std;
const int maxn = 5e4 + 10;
int maxx[maxn << 2];
int n,m;
void push_down(int l,int r,int i){
if(maxx[i] > 0){
maxx[i << 1] = max(maxx[i << 1],maxx[i]);
maxx[i << 1 | 1] = max(maxx[i << 1 | 1],maxx[i]);
maxx[i] = 0;
}
}
void update(int l,int r,int ql,int qr,int i,int val){
if(ql <= l && r <= qr){
if(maxx[i] != -1){
maxx[i] = max(maxx[i],val);
return ;
}
}
if(maxx[i] >= val) return ;
if(l == r) return ;
push_down(l,r,i);
int mid = (l + r) >> 1;
if(ql <= mid) update(l,mid,ql,qr,i << 1,val);
if(qr > mid) update(mid + 1,r,ql,qr,i << 1 | 1,val);
maxx[i] = (maxx[i << 1] == maxx[i << 1 | 1] ? maxx[i << 1] : -1);
}
int ans = 0;
void query(int l,int r,int i){
if(l == r){
ans += maxx[i];
}
else {
push_down(l,r,i);
int mid = (l + r) >> 1;
query(l,mid,i << 1);
query(mid + 1,r,i << 1 | 1);
}
}
int main() {
while(~scanf("%d%d",&n,&m)){
clr(maxx,0);
while(m --){
int l,r,val; scanf("%d%d%d",&l,&r,&val);
++ l;
update(1,n,l,r,1,val);
}
ans = 0;
query(1,n,1);
printf("%d\n",ans);
}
return 0;
}