2019 Multi-University Training Contest 6:Snowy Smile (1005)线段树

Snowy Smile

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

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

题意:再二维平面内找一个平行于xy轴的矩形,使得矩形包含的点的值的和最大

题解:先离散化一下,再按x从小到大排序,然后枚举下边界,对每次枚举的下边界建立一个线段树,维护最大子段和,然后将边界以上的点一层一层的加入到线段树里面,每加一层的点,更新一下ans。

#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
const int INF  =  0x3f3f3f3f;
const int O    =  1e6;
const int mod  =  1e6 + 3;
const int maxn =  2e3 + 5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;
const long double eps = 1e-10;

struct dd {int x, y, w;
    bool friend operator < (dd a, dd b) { return a.x < b.x; }
} a[maxn];

//离散化
#define DCT(dct) SOT(dct), ERA(dct)
#define SOT(dct) sort(dct.begin(), dct.end())
#define ERA(dct) dct.erase(unique(dct.begin(), dct.end()), dct.end())
#define GID(dct, num) (int)(lower_bound(dct.begin(), dct.end(), num) - dct.begin())

void init(int &n) {
    cin >> n;
    vector<int> x, y;
    for(int i=0; i<n; i++) {
        scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].w);
        x.push_back(a[i].x);
        y.push_back(a[i].y);
    }
    DCT(x); DCT(y);
    for(int i=0; i<n; i++) {
        a[i].x = GID(x, a[i].x);
        a[i].y = GID(y, a[i].y);
    }
    sort(a, a + n);
}

struct TR{  LL lmax, rmax, imax, sum; }tree[maxn << 4];

void update(int l, int r, int node, int pos, int x) {
    if( l == r) {
        tree[node].imax += x;
        tree[node].lmax += x;
        tree[node].rmax += x;
        tree[node].sum += x;
        return ;
    }
    int mid = (l + r) >> 1;
    if(mid >= pos) update(l, mid, node << 1, pos, x);
    else update(mid + 1, r, node << 1 | 1, pos, x);

    tree[node].imax = max( tree[node << 1].imax, tree[node <<1 | 1].imax);
    tree[node].imax = max( tree[node].imax, tree[node << 1].rmax + tree[node << 1 | 1].lmax);
    tree[node].lmax = max(tree[node << 1].lmax, tree[node << 1].sum + tree[node << 1 | 1].lmax);
    tree[node].rmax = max(tree[node << 1 | 1].rmax, tree[node << 1 | 1].sum + tree[node << 1].rmax);
    tree[node].sum = tree[node << 1].sum + tree[node << 1| 1].sum;
}

int main(){
    int T; cin >> T;
    while( T -- ){
        int n; init(n);

        LL ans = 0;
        for(int i=0; i<n; i++) {
            for(int j=0; j<4*n; j++) tree[j].imax = tree[j].rmax = tree[j].lmax = tree[j].sum = 0;
            for(int j=i; j<n;) {
                int k = j;
                while(a[k].x == a[j].x) { update(0, n-1, 1, a[j].y, a[j].w); j ++; }
                ans = max(ans, tree[1].imax);
            }
            while(a[i].x == a[i +1].x) i ++;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值