D - Snowy Smile HDU - 6638

Posted caijiaming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D - Snowy Smile HDU - 6638相关的知识,希望对你有一定的参考价值。

题目链接: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
",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的主要内容,如果未能解决你的问题,请参考以下文章

2019杭电多校第六场hdu6638 Snowy Smile(线段树+枚举)

[hdu-6638]Snowy Smile 线段树维护 带修改的区间最大字段和 最大子矩阵 2019多校6

二分三角形的时候尤其需要注意!!! HDU 5115 二分+模拟

BC#92 B

hdu55272015ACM/ICPC亚洲区长春站 Too Rich

Count the Sheep 二分图+思维