题目:http://acm.hdu.edu.cn/showproblem.php?pid=6638
题意:2000个点,让你用矩形框起来,使得矩阵和最大
思路:首先离散化使数据将到O(2000),按y升序排列(按x也可以)后枚举上下界,在上界浮动的时候顺便将在上界上的点扔进线段树维护。复杂度O(n^2long(n))
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef __int128 bll;
const ll maxn = 2e3+100;
const ll mod = 1e9+7;
const ld pi = acos(-1.0);
const ll inf = 1e18;
const ld eps = 1e-5;
const ld e = exp(1);
ll T,n;
struct node
{
ll x,y,w;
}arr[maxn];
struct tnode
{
ll l,r;
ll sum,gss,lgss,rgss;
}tree[maxn<<2];
vector<ll>tx,ty;
bool cmp(node a,node b)
{
if(a.y != b.y)
return a.y < b.y;
else
return a.x < b.x;
}
tnode push(tnode &now,tnode <,tnode &rt)
{
now.sum = lt.sum + rt.sum;
now.lgss = max(lt.lgss,lt.sum + rt.lgss);
now.rgss = max(rt.rgss,rt.sum + lt.rgss);
now.gss = max(max(lt.gss,rt.gss),lt.rgss+rt.lgss);
}
void build(ll now,ll l,ll r)
{
tree[now].l = l,tree[now].r = r;
tree[now].gss = tree[now].sum = tree[now].lgss = tree[now].rgss = 0;
if(l == r)
return ;
ll mid = (tree[now].l+tree[now].r)/2;
build(now*2,l,mid);
build(now*2+1,mid+1,r);
return ;
}
void update(ll now,ll dis,ll val)
{
if(tree[now].l == dis && tree[now].r == dis)
{
tree[now].gss += val;
tree[now].lgss += val;
tree[now].rgss += val;
tree[now].sum += val;
// tree[now].gss = tree[now].lgss = tree[now].rgss = (tree[now].sum += val);
return ;
}
ll mid = (tree[now].l + tree[now].r)/2;
if(dis <= mid)
update(now*2,dis,val);
else if(dis >= mid+1)
update(now*2+1,dis,val);
push(tree[now],tree[now*2],tree[now*2+1]);
return ;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin >> T;
while(T--)
{
tx.clear();
ty.clear();
cin >> n;
for(ll i = 1; i <= n; i++)
{
cin >> arr[i].x >> arr[i].y >> arr[i].w;
tx.push_back(arr[i].x);
ty.push_back(arr[i].y);
}
sort(tx.begin(),tx.end());
sort(ty.begin(),ty.end());
tx.erase(unique(tx.begin(),tx.end()), tx.end());
ty.erase(unique(ty.begin(),ty.end()), ty.end());
for(ll i = 1; i <= n; i++)
{
arr[i].x = lower_bound(tx.begin(),tx.end(),arr[i].x)-tx.begin()+1;
arr[i].y = lower_bound(ty.begin(),ty.end(),arr[i].y)-ty.begin()+1;
}
sort(arr+1,arr+1+n,cmp);
ll ans = -inf;
ll tmp = 1;
for(ll i = 1; i <= ty.size(); i++) //枚举下界
{
build(1,1,tx.size());
ll now = tmp;
for(ll j = i; j <= ty.size(); j++)
{
while(now <= n && arr[now].y == j) //
{
update(1,arr[now].x,arr[now].w);
now++;
}
ans = max(ans,tree[1].gss);
//cout << now << endl;
if(j == i)
tmp = now;
}
}
cout << ans << endl;
}
return 0;
}