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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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 x, y;
    for(int i=0; i> 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

 

你可能感兴趣的:(简单线段树)