HDU 6638 Snowy Smile (2019杭电多校第六场)(最大连续区间和)

题目链接

Snowy Smile
Time Limit: 4000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1644 Accepted Submission(s): 501

Problem Description
There are n pirate chests buried in Byteland, labeled by 1,2,…,n. The i-th chest’s location is (xi,yi), and its value is wi, wi can be negative since the pirate can add some poisonous gases into the chest. When you open the i-th pirate chest, you will get wi 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), denoting the number of test cases.

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

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

It is guaranteed that ∑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个点,每个点有其对应的坐标和权值,现在需要一个矩形,使得矩形内的所有点权值和最大,问最大值为多少?

思路: 对于矩形的所有形状,有很多中组成方法。对于X轴,一共有n ^ 2种组合,每一种组合对应的Y轴组合又有n ^ 2,这样枚举每一种矩形的情况,显然是不可能的。所以我们可以用n ^ 2的方法计算X轴或者Y轴的所有组合,然后用权值线段树维护Y轴的组合方式及其权值,这样即变成了最大连续区间和问题。

最大连续区间和相关题目

#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i = (x); i <= (y); i++)
#define rep__(i,x,y) for(int i = (x); i < (y); i++)
#define lson (rt<<1)
#define rson ((rt<<1)|1)
#define ll long long
const int MAXN = 2010;

struct Tree
{
    ll sum;
    ll lsum;
    ll rsum;
    ll mx;
}tree[MAXN<<2];

struct Node
{
    int x,y,w;
}a[MAXN];
int b[MAXN];

void build(int rt,int L,int R)
{
    tree[rt].lsum = tree[rt].rsum = tree[rt].mx = tree[rt].sum = 0;
    if(L == R)  return ;
    int Mid = (L+R)>>1;
    build(lson,L,Mid);
    build(rson,Mid+1,R);
}

void pushup(int rt)
{
    tree[rt].sum = tree[lson].sum + tree[rson].sum;
    tree[rt].lsum = max(tree[lson].lsum, tree[lson].sum+tree[rson].lsum);
    tree[rt].rsum = max(tree[rson].rsum, tree[rson].sum+tree[lson].rsum);
    tree[rt].mx = max(max(tree[lson].mx, tree[rson].mx), tree[lson].rsum+tree[rson].lsum);
}

void update(int rt,int L,int R,int x,int w)
{
    if(L == R){
        tree[rt].lsum = tree[rt].rsum = tree[rt].mx = tree[rt].sum = tree[rt].sum + w;
        return ;
    }
    int Mid = (L+R)>>1;
    if(x <= Mid)    update(lson,L,Mid,x,w);
    else update(rson,Mid+1,R,x,w);
    pushup(rt);
}

bool cmp(Node t,Node v){ return t.x < v.x; }

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        rep(i,1,n){
            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
            b[i] = a[i].y;
        }
        sort(a+1,a+1+n,cmp);
        sort(b+1,b+1+n);
        int m = 1;
        rep(i,2,n)  if(b[m] != b[i])  b[++m] = b[i];
        rep(i,1,n)
            a[i].y=lower_bound(b+1,b+m+1,a[i].y)-b;
        ll ans = 0;
        rep(i,1,n){
            if(i == 1 || a[i].x != a[i-1].x){
                build(1,1,n);
                for(int j = i,k; j <= n; j = k){
                    for(k = j; k <= n && a[j].x == a[k].x; k++)
                        update(1,1,n,a[k].y,a[k].w);
                    ans = max(ans,tree[1].mx);
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值