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.
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.
You should output an integer A, which is oaiei’s greatest working efficiency in the period of time from 0 to N.
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
132 13 108
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 const int maxn = 50010; 5 struct node{ 6 int lt,rt,sum,val,maxV; 7 }tree[maxn<<2]; 8 void build(int lt,int rt,int v){ 9 tree[v].lt = lt; 10 tree[v].rt = rt; 11 tree[v].sum = tree[v].val = tree[v].maxV = 0; 12 if(lt + 1 == rt) return; 13 int mid = (lt + rt)>>1; 14 build(lt,mid,v<<1); 15 build(mid,rt,v<<1|1); 16 } 17 void pushdown(int v){ 18 tree[v<<1].val = tree[v<<1|1].val = tree[v].val; 19 tree[v<<1].maxV = tree[v<<1|1].maxV = tree[v].val; 20 tree[v<<1].sum = tree[v].val*(tree[v<<1].rt - tree[v<<1].lt); 21 tree[v<<1|1].sum = tree[v].val*(tree[v<<1|1].rt - tree[v<<1|1].lt); 22 } 23 void pushup(int v){ 24 if(tree[v<<1].val == tree[v<<1|1].val) tree[v].val = tree[v<<1].val; 25 else tree[v].val = -1; 26 tree[v].sum = tree[v<<1].sum + tree[v<<1|1].sum; 27 tree[v].maxV = max(tree[v<<1].maxV,tree[v<<1|1].maxV); 28 } 29 void update(int lt,int rt,int val,int v){ 30 if(val <= tree[v].val) return;//纯色 31 if(lt <= tree[v].lt && tree[v].rt <= rt &&(tree[v].val >= 0 || tree[v].maxV < val)){ 32 tree[v].val = val; 33 tree[v].maxV = val; 34 tree[v].sum = val*(tree[v].rt - tree[v].lt); 35 return; 36 } 37 if(tree[v].val > 0) pushdown(v); 38 if(lt < tree[v<<1].rt) update(lt,rt,val,v<<1); 39 if(rt > tree[v<<1|1].lt) update(lt,rt,val,v<<1|1); 40 pushup(v); 41 } 42 int main(){ 43 int n,m,s,t,p; 44 while(~scanf("%d %d",&n,&m)){ 45 build(0,n,1); 46 while(m--){ 47 scanf("%d %d %d",&s,&t,&p); 48 update(s,t,p,1); 49 } 50 printf("%d\n",tree[1].sum); 51 } 52 return 0; 53 }
原文:http://www.cnblogs.com/crackpotisback/p/4444697.html