FZU1608(线段树)

 

传送门:Huge Mission

题意:给定区间范围[0,N] (2 <= N <= 50000)M个区间 (1 <= M <= 500000)和这些区间上的权值,求最终并区间的最大权值总和(某个区间不能被计算两次)。

分析:线段树区间维护最值,最后pushdown下去取每点的最大值。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define maxn 50010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int mx[maxn<<2];
void PushDown(int rt)
{
    if(mx[rt])
    {
        mx[rt<<1]=max(mx[rt<<1],mx[rt]);
        mx[rt<<1|1]=max(mx[rt<<1|1],mx[rt]);
        mx[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    mx[rt]=0;
    if(l==r)return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        mx[rt]=max(mx[rt],c);
            return;
    }//printf("%d\n",R);
    PushDown(rt);
    int m=(l+r)>>1;
    if(L<=m)update(L,R,c,lson);
    if(m<R)update(L,R,c,rson);
}
int Query(int l,int r,int rt)
{
    if(l==r)return mx[rt];
    PushDown(rt);
    int m=(l+r)>>1;
    return Query(lson)+Query(rson);
}
int main()
{
    int n,m;
    int u,v,e;
    while(scanf("%d%d",&n,&m)>0)
    {
        build(1,n,1);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&e);
            update(u+1,v,e,1,n,1);
        }
        printf("%d\n",Query(1,n,1));
    }
}
View Code

 

posted on 2015-02-12 13:04  lienus  阅读(103)  评论(0编辑  收藏  举报

导航