HDU-6638 Snowy Smile 区间最大子段和

题目:

There are nn pirate chests buried in Byteland, labeled by 1,2,…,n1,2,…,n. The ii-th chest's location is (xi,yi)(xi,yi), and its value is wiwi, wiwi can be negative since the pirate can add some poisonous gases into the chest. When you open the ii-th pirate chest, you will get wiwi value. 

You want to make money from these pirate chests. You can select a rectangle, the sides of which are all paralleled to the axes, and then all the chests inside it or on its border will be opened. Note that you must open all the chests within that range regardless of their values are positive or negative. But you can choose a rectangle with nothing in it to get a zero sum. 

Please write a program to find the best rectangle with maximum total value.

Input

The first line of the input contains an integer T(1≤T≤100)T(1≤T≤100), denoting the number of test cases. 

In each test case, there is one integer n(1≤n≤2000)n(1≤n≤2000) in the first line, denoting the number of pirate chests. 

For the next nn lines, each line contains three integers xi,yi,wi(−109≤xi,yi,wi≤109)xi,yi,wi(−109≤xi,yi,wi≤109), denoting each pirate chest. 

It is guaranteed that ∑n≤10000∑n≤10000.

Output

For each test case, print a single line containing an integer, denoting the maximum total value.

Sample Input
2
4
1 1 50
2 1 50
1 2 50
2 2 -500
2
-1 1 5
-1 1 1
Sample Output
100
6

题目大意

给你n(n<=2000)个点的坐标(x, y)以及该点的值(-1e9<val<1e9),让你找出一个矩形把它们框住,使得这个矩形内点值的和最大,求这个最大和。

解题思路

由于n小于2000,可以枚举这个矩形的左右两条边,然后对于这两条边内的点,我们用线段树维护最大子段和就可以了。注意要将坐标离散化一下。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cmath>

using namespace std;
const int maxn = 2056;

struct node{
    int x,y;
    long long w;
}p[maxn];
int numx[maxn];
int n,m,g;
long long ans;

int idx(int x)
{
    return lower_bound(numx+1,numx+1+m,x)-numx;
}

int cmp(node a,node b)
{
    return a.y<b.y;
}

long long sum[maxn<<2],suml[maxn<<2],sumr[maxn<<2],ansSum[maxn<<2];
void add(int pos, long long val, int l, int r, int rt)
{
//    if(ans==150)
//    {
//        cout << l<<" "<<r<<" "<< val<<" "<< sum[rt]<<" "<< suml[rt]<<" "<< sumr[rt]<<" "<< ansSum[rt]<<endl;
//    }
    if(l==r)
    {
        sum[rt]+=val;
        suml[rt]+=val;
        sumr[rt]+=val;
        ansSum[rt]+=val;
        return ;
    }
    int mid = (l+r)/2;
    if(pos<=mid) add(pos,val,l,mid,rt<<1);
    else add(pos,val,mid+1,r,rt<<1|1);
    ansSum[rt] = max(ansSum[rt<<1],ansSum[rt<<1|1]);
    ansSum[rt] = max(ansSum[rt],sumr[rt<<1]+suml[rt<<1|1]);
    sum[rt] = sum[rt<<1]+sum[rt<<1|1];
    suml[rt] = max(suml[rt<<1],sum[rt<<1]+suml[rt<<1|1]);
    sumr[rt] = max(sumr[rt<<1|1],sum[rt<<1|1]+sumr[rt<<1]);
}

void init()
{
    memset(sum,0,sizeof(sum));
    memset(suml,0,sizeof(suml));
    memset(sumr,0,sizeof(sumr));
    memset(ansSum,0,sizeof(ansSum));

}

void solve()
{
    for(int l = 1; l <= n; l++)
    {
        if(l!=1&&p[l].y==p[l-1].y)
            continue;
        init();
        int r = l;
//        cout <<"$$ "<< l<<" "<<r<<endl;
        for(r = l; r<= n; r++)
        {
            if(r!=l&&p[r].y!=p[r-1].y)
                ans = max(ans,ansSum[1]);
            add(idx(p[r].x),p[r].w,1,m,1);
//            cout <<" ** "<<p[r].w<<endl;
//            cout <<"%% "<< p[r].y<<" "<<p[r+1].y<<endl;
//            if(r==n||p[r].y!=p[r+1].y){
                cout <<" "<< l<<" "<<r<<endl;
//                ans = max(ans,ansSum[1]);
//                if(ans==150)
//                    cout <<"$$ "<<l<<" "<<r<<endl;
//            }

        }

//        cout <<"$$ "<< l<<" "<<r<<" "<<sum[1]<<" "<<m<<endl;
        ans = max(ans,ansSum[1]);
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d%lld", &p[i].x, &p[i].y, &p[i].w);
            numx[i] = p[i].x;
        }
        sort(numx+1,numx+1+n);
        m = unique(numx+1,numx+1+n)-numx-1;
        sort(p+1,p+1+n,cmp);
        ans = 0;
        solve();
        printf("%lld\n", ans);
    }
    return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值