BZOJ 4548: 小奇的糖果 单调栈 链表+树状数组/主席树

4548: 小奇的糖果

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 262  Solved: 118
[Submit][Status][Discuss]

Description

有 N 个彩色糖果在平面上。小奇想在平面上取一条水平的线段,并拾起它上方或下方的所有糖果。求出最多能够拾起多少糖果,使得获得的糖果并不包含所有的颜色。

Input

包含多组测试数据,第一行输入一个正整数 T 表示测试数据组数。

接下来 T 组测试数据,对于每组测试数据,第一行输入两个正整数 N、K,分别表示点数和颜色数。
接下来 N 行,每行描述一个点,前两个数 x, y (|x|, |y| ≤ 2^30 - 1) 描述点的位置,最后一个数 z (1 ≤ z ≤
 k) 描述点的颜色。
对于 100% 的数据,N ≤ 100000,K ≤ 100000,T ≤ 3

Output

对于每组数据在一行内输出一个非负整数 ans,表示答案

Sample Input

1
10 3
1 2 3
2 1 1
2 4 2
3 5 3
4 4 2
5 1 2
6 3 1
6 7 1
7 2 3
9 4 2

Sample Output

5

这个题还是挺水的

显然只有三种符合条件的矩形

1.被两个相同颜色夹在中间的上下界无限的矩形

2.被一个点封住上界两个与其相同颜色夹在中间的下界无限的矩形

3.被一个点封住下界两个与其相同颜色夹在中间的上界无限的矩形

第一种情况显然很好处理

第二种情况 考虑枚举每一个卡住上/下界的点

那么贡献最大的肯定是左右第一个比它纵坐标小的卡住的矩形

单调栈扫一下就行

第三种则与第二种同理

这样就是一个二维数点了 主席树显然可做


但是很多题解用双向链表写

BJ觉得比主席树要妙的多 也说下

先按x排序得到pre 和 next

之后按y排序枚举每一个点x

删掉所有y<=x.y的点

查询pre,nt区间内的点 之后在链表上删除该点

就解决了上界无限的矩形

下界无限的就把y翻过来就行

但是BJ觉得细节有点多...调了好久才搞出来

// 也就两三天...太菜了


#include<cmath>
#include<ctime>
#include<cstdio>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;

typedef long long ll;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=100100;

int n,K,tot;

struct node{int x,y,c,pos;}p[N];

inline bool cmp1(const node &x,const node &y)
{return x.x<y.x;}

inline bool cmp2(const node &x,const node &y)
{return x.y<y.y;}

int bit[N];

inline void modify(int x,int val)
{for(;x<=tot;x+=(x&-x)) bit[x]+=val;}

inline int query(int x)
{
	int res(0);
	for(;x;x-=(x&-x)) res+=bit[x];//cout<<res<<" ";
	return res;
}

int ans;
int X[N],last[N],pre[N],nt[N];

void solve()
{
	memset(last,0,sizeof(last));
	register int i,j,tmp;
	X[n+1]=tot;
	for(i=1;i<=n;++i) modify(p[i].x,1);
	for(i=1;i<=n;++i)
	{
		p[i].pos=i;X[i]=p[i].x;
		pre[i]=last[p[i].c];nt[i]=n+1;
		if(last[p[i].c]) nt[last[p[i].c]]=i;
		last[p[i].c]=i;
		ans=max(ans,query(X[i]-1)-query(X[pre[i]]));
	}
	for(i=1;i<=K;++i)
		ans=max(ans,query(tot)-query(X[last[i]]));
	sort(p+1,p+1+n,cmp2);
	for(i=1,j=1;i<=n;++i)
	{
		tmp=p[i].pos;
		while(j<=n && p[i].y==p[j].y)
			modify(p[j].x,-1),j++;
		nt[pre[tmp]]=nt[tmp];
		pre[nt[tmp]]=pre[tmp];
		ans=max(ans,query(X[nt[tmp]]-1)-query(X[pre[tmp]]));
	}
}

int main()
{
	register int T=read(),i;
	while(T--)
	{
		n=read();K=read();
		for(i=1;i<=n;++i)
			p[i].x=read(),p[i].y=read(),p[i].c=read();
		sort(p+1,p+1+n,cmp1);
		tot=1;
		for(i=1;i<n;++i)
			p[i].x==p[i+1].x ? p[i].x=tot : p[i].x=tot++;
		p[n].x=tot++;
		
		ans=0;
		solve();
		
		for(i=1;i<=n;++i) p[i].y=-p[i].y;
		sort(p+1,p+1+n,cmp1);
		solve();
		
		cout<<ans<<endl;
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值