Problem 1608 Huge Mission
Accept: 489 Submit: 1268
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
Source
FOJ月赛-2008年5月
题目大意:
长度n,m次操作;每次操作都有三个数:a,b,c;意味着(a,b]区间单位长度的价值为c,若某段长度不曾被操作,
则意味着该长度价值为0,若有一段长度有多个价值,则选取最大的。(多组输入)请输出(0,n]内最大价值和。
区间更新板子题。
# include <iostream>
# include <cstdio>
# define ls k<<1,l,mid
# define rs k<<1|1,mid+1,r
using namespace std;
const int MAXN = 50005;
int n,m,ans;
struct node
{
int l,r,add;
}tree[MAXN<<2];
void pushdown(int k)
{
if(tree[k].add)//若该节点维护的区间需加值,则它的儿子需要继承这个值
{
int mid=k<<1;
tree[mid].add=max(tree[k].add,tree[mid].add);
tree[mid|1].add=max(tree[k].add,tree[mid|1].add);
tree[k].add=0;
}
return ;
}
void build(int k,int l,int r)//建树
{
tree[k].l=l;
tree[k].r=r;
tree[k].add=0;
if(l==r)
return ;
int mid=(l+r)>>1;
build(ls);
build(rs);
return ;
}
void updata(int k,int l,int r,int add)
{
if(tree[k].add>=add)
return ;
if(l<=tree[k].l&&tree[k].r<=r)//节点维护的区间在所加区间内
{
tree[k].add=add;//此时add必定大于原add
return ;
}
int mid = (tree[k].l+tree[k].r)>>1;
if(r<=mid) //所加区间在该节点左儿子所维护的区间
updata(k<<1,l,r,add);
else if(l>mid)//所加区间在该节点右儿子所维护的区间
updata(k<<1|1,l,r,add);
else//所加区间由左右儿子共同维护
{
updata(k<<1,l,mid,add);
updata(k<<1|1,mid+1,r,add);
}
return ;
}
void query(int k,int l,int r)
{
if(l==r)//若为叶子节点
{
ans+=tree[k].add;
return ;
}
//否则,由该节点的儿子继承这个值,直到为叶子节点
pushdown(k);
int mid=(tree[k].l+tree[k].r)>>1;
if(r<=mid)// 询问区间在该节点左儿子所维护的区间
query(k<<1,l,r);
else if(l>mid)//询问区间在该节点左儿子所维护的区间
query(k<<1|1,l,r);
else //询问区间由该节点左右儿子共同维护
{
query(k<<1,l,mid);
query(k<<1|1,mid+1,r);
}
return ;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=0;
build(1,1,n);
int a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
updata(1,a+1,b,c);
}
query(1,1,n);
printf("%d\n",ans);
}
return 0;
}