码迷,mamicode.com
首页 > 其他好文 > 详细

D - Snowy Smile HDU - 6638

时间:2020-03-12 11:25:40      阅读:49      评论:0      收藏:0      [点我收藏+]

标签:lse   sub   nes   base   ref   ring   ack   mat   into   

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638

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.

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

In each test case, there is one integer n(1n2000)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(109xi,yi,wi109)xi,yi,wi(−109≤xi,yi,wi≤109), denoting each pirate chest.

It is guaranteed that n10000∑n≤10000.
OutputFor 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
题目大意:二维坐标中有一些点,每个点有一个值,你有一个任意大小的矩形,问你最大能框出的值是多少
思路:点的个数在2000个以内,我们可以枚举矩形的左右边界,对于左右边界以内的点,用线段树维护区间最大值就行了
有个需要注意的地方就是既然要枚举边界,就要一次性枚举完,不然算出来的值肯定是不准确的
看代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stack>
#include<map>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define sc1(a) scanf("%lld",&a)
#define pf1(a) printf("%lld\n",a)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const LL INF=1e18;
const ull base=2333;
const int maxn=2e3+50;
const int maxm=1e3+50;
const int maxv=1e6+5;
const int mod=1e9+7;
const int ba=3e5;
struct Node
{
    LL x,y,w;
    bool operator < (const Node &b) const
    {
        return b.x<x;
    }
}a[maxn];
struct Tree
{
    LL ll,rr,lm,sum;//
}t[maxn<<2];
LL ly[maxn];
LL len;
LL get_id(LL w)
{
    return lower_bound(ly+1,ly+1+len,w)-(ly);
}
void Push_up(LL rt)
{
    t[rt].ll=max(t[rt<<1].ll,t[rt<<1].sum+t[rt<<1|1].ll);
    t[rt].rr=max(t[rt<<1|1].rr,t[rt<<1|1].sum+t[rt<<1].rr);
    t[rt].lm=max(t[rt<<1].lm,max(t[rt<<1|1].lm,t[rt<<1].rr+t[rt<<1|1].ll));
    t[rt].sum=t[rt<<1].sum+t[rt<<1|1].sum;
}
void Update(LL l,LL r,LL rt,LL L,LL v)
{
    if(l==r&&l==L)
    {
        t[rt].lm+=v;
        t[rt].ll=t[rt].rr=t[rt].sum=t[rt].lm;
        return ;
    }
    LL mid=(l+r)>>1;
    if(L<=mid) Update(l,mid,rt<<1,L,v);
    else Update(mid+1,r,rt<<1|1,L,v);
    Push_up(rt);
}
int main()
{
    LL T;sc1(T);
    while(T--)
    {
        LL ans=0;
        LL N;sc1(N);
        for(LL i=1;i<=N;i++)
        {
            sc1(a[i].x);sc1(a[i].y);sc1(a[i].w);
            ly[i]=a[i].y;//离散化y
        }
        sort(a+1,a+1+N);
        sort(ly+1,ly+1+N);
        len=unique(ly+1,ly+1+N)-(ly+1);
        for(LL i=1;i<=N;i++) //枚举矩形左边界
        {
            if(i!=1&&a[i].x==a[i-1].x) continue;
            for(LL j=1;j<=(len<<2);j++) t[j].ll=t[j].rr=t[j].sum=t[j].lm=0;
            for(LL j=i;j<=N;j++) //枚举矩形右边界
            {
                LL v=get_id(a[j].y);
                Update(1,len,1,v,a[j].w);
                if(j==N||a[j].x!=a[j+1].x)//这里是一个需要注意的点 我们必须对于每一条边界 一定要枚举完
                ans=max(ans,t[1].lm);
            }
        }
        pf1(ans);
    }
    return 0;
}
/**

*/

 

D - Snowy Smile HDU - 6638

标签:lse   sub   nes   base   ref   ring   ack   mat   into   

原文地址:https://www.cnblogs.com/caijiaming/p/12467862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!