日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

發布時間:2023/12/20 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 (最大字段和)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。