HDU 6638(稀疏矩阵的最大子矩阵和)

Snowy Smile

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

 

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

 

 题意:给2000个点,每一个点有一个值 w ,选择一个矩形,使矩形内的 w 和最大(可以不选,为0),问最大的 w 和

思路:首先肯定是离散化,那个这个问题就转化成了最大子矩阵和,一般情况下的最大子矩阵和复杂度是 n ^ 3。但对于这个题,你会发现离散化之后点还是很稀疏的。所以可以利用这一点,对这个题进行优化。

假设现在已经离散化好了,那么只需要枚举上下边界,对每一次的下边界枚举都对这一边界上的点  单点更新,也就是维护了一个带修改的最大子段和,这样复杂度是过得去的。如2000个点,你要构成 2000 * 2000 矩阵情况,那么每行每列也就一个点,复杂度是 2000 * 2000 * log2000,也就是说每一行点的个数增加时,那么行数必定会减少很多。

Code:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define CLR(a) while(!(a).empty()) a.pop()

using namespace std;

const int maxn = 2e3 + 10;
struct xx{
    int x,y;
    LL w;
}point[maxn];
int Lsx[maxn],Lsy[maxn],px,py;

inline int read() {
    int X = 0, w = 0;
    char ch = 0;
    while(!isdigit(ch)) {
        w |= ch == '-';
        ch = getchar();
    }
    while(isdigit(ch))
        X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}

vector<xx>Loc[maxn];

struct Tree{
    int l,r;
    LL lmax,rmax,maxx,sum;
}tr[maxn << 2];

void build(int i,int l,int r){
    tr[i].l = l; tr[i].r = r;
    tr[i].lmax = tr[i].rmax = tr[i].maxx = tr[i].sum = 0LL;
    if(l == r) return ;
    int mid = (l + r) >> 1;
    build(ls,l,mid);
    build(rs,mid + 1,r);
}

void pushup(int i){
    tr[i].lmax = max(tr[ls].lmax,tr[ls].sum + tr[rs].lmax);
    tr[i].rmax = max(tr[rs].rmax,tr[rs].sum + tr[ls].rmax);
    tr[i].maxx = max(tr[ls].maxx,max(tr[rs].maxx,tr[ls].rmax + tr[rs].lmax));
    tr[i].sum = tr[ls].sum + tr[rs].sum;
}

void update(int i,int pos,LL val){
    if(tr[i].l == tr[i].r){
        tr[i].lmax += val;
        tr[i].rmax += val;
        tr[i].maxx += val;
        tr[i].sum += val;
    }
    else {
        int mid = (tr[i].l + tr[i].r) >> 1;
        if(pos <= mid) update(ls,pos,val);
        if(pos > mid) update(rs,pos,val);
        pushup(i);
    }
}

int main() {
//#ifndef ONLINE_JUDGE
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
//#endif
    int T = read();
    while(T --){
        int n = read();
        px = py = 0;
        for(int i = 1;i <= n;++ i){
            point[i].x = read();
            point[i].y = read();
            point[i].w = read();
            Lsx[++ px] = point[i].x;
            Lsy[++ py] = point[i].y;
        }
        sort(Lsx + 1,Lsx + 1 + px);
        px = unique(Lsx + 1,Lsx + 1 + px) - Lsx - 1;
        sort(Lsy + 1,Lsy + 1 + py);
        py = unique(Lsy + 1,Lsy + 1 + py) - Lsy - 1;
        for(int i = 0;i < maxn;++ i) Loc[i].clear(), Loc[i].reserve(maxn);
        for(int i = 1;i <= n;++ i){
            point[i].x = lower_bound(Lsx + 1,Lsx + 1 + px,point[i].x) - Lsx;
            point[i].y = lower_bound(Lsy + 1,Lsy + 1 + py,point[i].y) - Lsy;
            Loc[point[i].x].pb(point[i]);
        }
        LL ans = 0;
        for(int i = 1;i <= px;++ i){
            build(1,1,py);
            for(int j = i;j <= px;++ j){
                for(auto it : Loc[j])
                    update(1,it.y,it.w);
                ans = max(ans,tr[1].maxx);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值