欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

【HDU2019多校】E - Snowy Smile (最大字段和)

发布时间:2023/12/20 编程问答 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【HDU2019多校】E - Snowy Smile (最大字段和) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
There are nn pirate chests buried in Byteland, labeled by 1,2,,n1,2,…,n. The ii-th chest's location is (xi,yi)(xi,yi), and its value is wiwi, wiwi can be negative since the pirate can add some poisonous gases into the chest. When you open the ii-th pirate chest, you will get wiwi 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.

InputThe first line of the input contains an integer T(1T100)T(1≤T≤100), denoting the number of test cases. 

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

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

It is guaranteed that n10000∑n≤10000.
OutputFor 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

SOLUTION:
都是都是套路,每次枚举矩形的上边界
然后每个下边界加入线段树后,查询最大字段和。

CODE:
#include <bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; typedef long long ll;const int N = 2005;struct T{ll x, y, w;T(){}T(ll x, ll y, ll w): x(x), y(y), w(w){} }a[N];bool cmp(const T& a, const T& b) {return a.x < b.x; }int b[N];struct {int l, r;ll lx, rx, mx;ll sum; }tree[N << 2];void build(int k, int l, int r) {tree[k].l = l;tree[k].r = r;tree[k].sum = tree[k].lx = tree[k].rx = tree[k].mx = 0;if(l == r)return;int mid = (l + r) / 2;build(2*k, l, mid);build(2*k + 1, mid + 1, r); }void push_up(int k) {tree[k].sum = tree[2*k].sum + tree[2*k+1].sum;tree[k].lx = max(tree[2*k].lx, tree[2*k].sum + tree[2*k+1].lx);tree[k].rx = max(tree[2*k+1].rx, tree[2*k+1].sum + tree[2*k].rx);tree[k].mx = max(max(tree[2*k].mx, tree[2*k+1].mx), tree[2*k].rx + tree[2*k+1].lx); }void insert(int k, int x, int w) {if(tree[k].l == tree[k].r){tree[k].sum = tree[k].lx = tree[k].rx = tree[k].mx = tree[k].mx + w;return;}int mid = (tree[k].l + tree[k].r) / 2;if(x <= mid)insert(2*k, x, w);elseinsert(2*k+1, x, w);push_up(k); }inline ll query() {return tree[1].mx; }int main() {ios::sync_with_stdio(false);int t;cin >> t;while(t --){int n;cin >> n;for(int i = 1; i <= n; i ++){ll x, y, w;cin >> x >> y >> w;a[i] = T(x, y, w);b[i] = y;}sort(b + 1, b + n + 1);int k = unique(b + 1, b + n + 1) - b - 1;for(int i = 1; i <= n; i ++){int y = lower_bound(b + 1, b + k + 1, a[i].y) - b;a[i].y = y;}sort(a + 1, a + n + 1, cmp);ll ans = 0;for(int i = 1; i <= n; i ++){if(i != 1 && a[i].x == a[i - 1].x)continue;build(1, 1, k);for(int j = i; j <= n; j ++){if(j != i && a[j].x != a[j - 1].x)ans = max(ans, query());insert(1, a[j].y, a[j].w);}ans = max(ans, query());}cout << ans << endl;}return 0; }

  









转载于:https://www.cnblogs.com/zhangbuang/p/11386932.html

总结

以上是生活随笔为你收集整理的【HDU2019多校】E - Snowy Smile (最大字段和)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。