2019 Multi-University Training Contest 6 Snowy Smile (最大子段和)

Snowy Smile

Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 2997    Accepted Submission(s): 954


 

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

 

 

Source

2019 Multi-University Training Contest 6

 

 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6730 6729 6728 6727 6726 

题目大意:给一个矩阵,求一个子矩阵的和最大。

解题思路:将x离散化后,我们可以枚举左右边界。然后对y轴求一个最大子段和就可以了。

刚开始的时候pushup函数写错了。。。。。。

#include<bits/stdc++.h>
#define LL long long
using namespace std;
#define pb(x) push_back(x)
#define sca(x) scanf("%d",&x)
#define mp(x,y) make_pair(x,y)

const int N = 2000+5;
struct poi
{
    int x,y,w;
} poi[2005];
vector<int>X,Y;
typedef pair<int,int>pii;
vector<pii>b[N];


void pre_work(int n)
{
    for(int i=0; i<N; i++)b[i].clear();

    sort(X.begin(),X.end());
    sort(Y.begin(),Y.end());

    X.erase(unique(X.begin(),X.end()),X.end());
    Y.erase(unique(Y.begin(),Y.end()),Y.end());

    for(int i=1; i<=n; i++)
    {
        poi[i].x=lower_bound(X.begin(),X.end(),poi[i].x)-X.begin()+1;
        poi[i].y=lower_bound(Y.begin(),Y.end(),poi[i].y)-Y.begin()+1;
        b[poi[i].x].pb(mp(poi[i].y,poi[i].w));
    }
    for(int i=1; i<=X.size(); i++)
    {
        sort(b[i].begin(),b[i].end());
    }
}


struct node
{
    LL lsum;
    LL rsum;
    LL msum;
    LL sum;
} t[N*4];


void push_up(int rt)
{
    t[rt].lsum=max(t[rt<<1].lsum,t[rt<<1].sum+t[rt<<1|1].lsum);
    t[rt].rsum=max(t[rt<<1|1].rsum,t[rt<<1|1].sum+t[rt<<1].rsum);
    t[rt].msum=max(t[rt<<1].msum,max(t[rt<<1|1].msum,t[rt<<1].rsum+t[rt<<1|1].lsum));
    t[rt].sum=t[rt<<1].sum+t[rt<<1|1].sum;
}

void upd(int rt,int l,int r,int pos,LL val)
{
    if(l==r)
    {
        t[rt].lsum+=val;
        t[rt].rsum+=val;
        t[rt].msum+=val;
        t[rt].sum+=val;
        return ;
    }
    int m=(l+r)>>1;
    if(pos<=m) upd(rt<<1,l,m,pos,val);
    else upd(rt<<1|1,m+1,r,pos,val);
    push_up(rt);
}

int main()
{
    int _t;
    cin>>_t;
    while(_t--)
    {
        int n;
        sca(n);
        X.clear();
        Y.clear();
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d%d",&poi[i].x,&poi[i].y,&poi[i].w);
            X.pb(poi[i].x);
            Y.pb(poi[i].y);
        }
        pre_work(n);
        int len1=X.size();
        int len2=Y.size();
        LL ans=0;

        for(int i=1; i<=len1; i++)
        {
            memset(t,0,sizeof(t));
            for(int j=i; j<=len1; j++)
            {
                for(int k=0; k<b[j].size(); k++)
                {
                    pii tmp=b[j][k];
                    upd(1,1,len2,tmp.first,tmp.second);
                }
                ans=max(ans,t[1].msum);
            }
        }
        printf("%lld\n",ans);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值