ccpc网络赛hdu6447(线段树区间最值)

ccpc网络赛hdu6447(线段树区间最值)

题目:

YJJ's Salesman

 

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 11    Accepted Submission(s): 3

 

 

Problem Description

YJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination.

One day, he is going to travel from city A to southeastern city B. Let us assume that A is (0,0) on the rectangle map and B (109,109). YJJ is so busy so he never turn back or go twice the same way, he will only move to east, south or southeast, which means, if YJJ is at (x,y) now (0≤x≤109,0≤y≤109), he will only forward to (x+1,y), (x,y+1) or (x+1,y+1).

On the rectangle map from (0,0) to (109,109), there are several villages scattering on the map. Villagers will do business deals with salesmen from northwestern, but not northern or western. In mathematical language, this means when there is a village k on (xk,yk) (1≤xk≤109,1≤yk≤109), only the one who was from (xk−1,yk−1) to (xk,yk) will be able to earn vk dollars.(YJJ may get different number of dollars from different village.)

YJJ has no time to plan the path, can you help him to find maximum of dollars YJJ can get.

 

Input

The first line of the input contains an integer T (1≤T≤10),which is the number of test cases.

 

In each case, the first line of the input contains an integer N (1≤N≤105).The following N lines, the k-th line contains 3 integers, xk,yk,vk (0≤vk≤103), which indicate that there is a village on (xk,yk) and he can get vk dollars in that village.

The positions of each village is distinct.

 

Output

The maximum of dollars YJJ can get.

 

Sample Input

1 3 

1 1 1 

1 2 2 

3 3 1

 

Sample Output

3

 

分析:

题目意思很简单,给你n个点,你只能向右,向下,向右下走,当你向右下走恰巧走到某个点时,你可以获得收益,问最大收益是多少

首先对坐标进行离散化处理,线段树维护的是列的值。我们以行升序,列降序来对这n个点排序,然后按点的顺序,依次更新点所在的列的值,某点所在列的值=第1到i-1列的最大值+当前点的权值。

 

代码如下:

#include<bits/stdc++.h>

#define lson l,m,rt<<1

#define rson m+1,r,rt<<1|1

using namespace std;

int aa[100005];

int xu=0;

const int maxn=200010;

 

int maxx[maxn<<2];

 

void PushUp(int rt){

    maxx[rt]=max(maxx[rt<<1],maxx[rt<<1|1]);

}

 

void build(int l,int r,int rt){

    if(l==r){

        maxx[rt]=0;

        return ;

    }

    int m=(l+r)>>1;

    build(lson); build(rson);

    PushUp(rt);

}

 

void update(int p,int cov,int l,int r,int rt){

    if(l==r){

        maxx[rt]=cov;//这里是赋值

        return ;

    }

    int m=(l+r)>>1;

    if(p<=m) update(p,cov,lson);

    else update(p,cov,rson);

    PushUp(rt);

}

 

int query(int L,int R,int l,int r,int rt){

    if(L<=l&&r<=R) return maxx[rt];

    int m=(l+r)>>1;

    int ret=0;//较小数

    if(L<=m) ret=max(ret,query(L,R,lson));//维护最值

    if(R>m) ret=max(ret,query(L,R,rson));

    return ret;

}

 

struct point

{int x,y;

int w;

int xux;

int xuy;

};

point p[100005];

 

bool cmp1(point a , point b)

{

    return a.x<b.x;

}

bool cmp2(point a , point b)

{

    return a.y<b.y;

}

 

bool cmp3(point a , point b)

{

    if(a.x==b.x) return a.y>b.y;

    else return a.x<b.x;

}

 

int main(void)

{

    int n, m;

    int t;

    cin>>t;

    while(t--){

        scanf("%d", &n);

        xu=0;

        build(1, n, 1);

        for(int i=1;i<=n;i++)

            scanf("%d %d %d",&p[i].x,&p[i].y,&p[i].w);

 

        sort(p+1,p+n+1,cmp1);

        int hax=1;

        for(int i=1;i<=n;i++)

        {

            p[i].xux=hax;

            if(p[i+1].x!=p[i].x) hax++;

        }

 

        int hay=1;

        sort(p+1,p+n+1,cmp2);

        for(int i=1;i<=n;i++)

        {

            p[i].xuy=hay;

            if(p[i+1].y!=p[i].y) hay++;

        }

        sort(p+1,p+n+1,cmp3);

        hax--;

        hay--;

        for(int i=1;i<=n;i++)

        {

            if(p[i].xuy==1) update(p[i].xuy, p[i].w, 1, n, 1);

            else

            {

                int haha=query(1, p[i].xuy-1, 1, n, 1)+p[i].w;

                update(p[i].xuy, haha, 1, n, 1);

            }

        }

        cout<<query(1,n,1,n,1)<<endl;

        }

        return 0;

}

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值