【离散化+线段树维护区间最大子段和】2019 Multi-University Training Contest 6 Snowy Smile

题目: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 &lt,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;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值