fzu_1608 Huge Mission线段树_区间修改求和

来源:互联网 发布:知乎接口 编辑:程序博客网 时间:2024/06/06 20:06
Problem 1608 Huge Mission

Accept: 405    Submit: 1025
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 <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define mem0(a) memset(a,0,sizeof(a))const int maxn = 500000+10;int a[maxn<<2];//只建立一个线段树,每个结点保存子叶的最大值void build(int l,int r,int cur){    a[cur] = 0 ;    if(l == r ){//由题知,初始(默认)值为0                //因此a[cur]=0;可省略        return ;    }    int mid = ( l + r  )>> 1 ;    build(l,mid,cur<<1);    build(mid+1,r,cur<<1|1);}void pushDown(int cur ){//向下更新    if(a[cur]){//如果该结点被标记        a[cur<<1]=max(a[cur<<1],a[cur]);        a[cur<<1|1]=max(a[cur<<1|1],a[cur]);        a[cur] = 0 ;    }}void update(int x,int y,int z,int l,int r,int cur){    if( x <= l && y >= r){        a[cur] = max(z,a[cur]);        return ;    }    pushDown(cur);    int mid = ( l + r)>>1;    if(x <= mid)        update(x,y,z,l,mid,cur<<1);    if(y > mid)        update(x,y,z,mid+1,r,cur<<1|1);}int query(int l,int r,int cur){    if(l == r )        return a[cur];    pushDown(cur);    int mid = (l + r )>>1 ;    return query(l,mid,cur<<1)+query(mid+1,r,cur<<1|1);}int main(){    int n , m ;    while(scanf("%d %d",&n,&m)!=EOF){        build(1,n,1);        while(m--){            int x,y,z;            scanf("%d%d%d",&x,&y,&z);            if(z) update(x+1,y,z,1,n,1);        }        printf("%d\n",query(1,n,1));    }    return 0;}

方法二,用两个线段树表示,样例对,但是提交有错,先贴上,欢迎指正
#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define mem0(a) memset(a,0,sizeof(a))const int maxn = 500000+10;int a[maxn<<2],sum[maxn<<2];//只建立一个线段树,每个结点保存子叶的最大值void Getsum(int cur){    sum[cur] = sum[cur<<1]+sum[cur<<1|1];}void build(int l,int r,int cur){    a[cur] = 0 ;    if( l == r ){        sum[cur]=0;        return ;    }    int mid = ( l +r )>>1;    build(l,mid,cur<<1);    build(mid+1,r,cur<<1|1);    Getsum(cur);}void pushDown(int cur,int len){    if(a[cur]){        a[cur<<1]=max(a[cur],a[cur<<1]);        a[cur<<1|1]=max(a[cur],a[cur<<1|1]);        sum[cur<<1]=a[cur<<1]*(len-(len>>1));        sum[cur<<1|1]=a[cur<<1|1]*(len>>1);        a[cur]=0;    }}void update(int x,int y,int z,int l,int r,int cur){    if( x <= l && y >= r){        a[cur]=max(a[cur],z);        sum[cur]=max(a[cur]*(r-l+1),sum[cur]);        return ;    }    pushDown(cur,r-l+1);    int mid = ( l + r  )>>1;    if( x <= mid )        update(x,y,z,l,mid,cur<<1);    if( y > mid )        update(x,y,z,mid+1,r,cur<<1|1);    Getsum(cur);}int main(){    int n , m ;    while(scanf("%d %d",&n,&m)!=EOF){        build(1,n,1);        while(m--){            int x,y,z;            scanf("%d%d%d",&x,&y,&z);            if(z) update(x+1,y,z,1,n,1);        }        printf("%d\n",sum[1]);    }    return 0;}




0 0