P1904 手打堆80ptsWA第五个点求助
查看原帖
P1904 手打堆80ptsWA第五个点求助
209691
Red_Alert_star楼主2023/8/8 19:43
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct line
{
	int x;
	int y;
	int side;
	int num;
};
int n;
line a[20001],b[20001],h[20001];
int cnt,len=0,vis[20001],yyy=0,xxx=0;
int check1(line x,line y)
{
	if(x.x==y.x)
	{
		if(x.side==1&&x.side==y.side)
		{
			return x.y>y.y;
		 } 
		else if(x.side==2&&x.side==y.side)
		{
			return x.y<=y.y;
		 } 
		return x.side<y.side;
	 } 
	return x.x<y.x;
}
void merge(int l,int r)
{
	int mid=l+r>>1;
	int h=l,t=mid+1;
	for(int i=l;i<=r;i++)
	{
		if(h<=mid&&(t>r||check1(a[h],a[t])))
		{
			b[i].x=a[h].x;
			b[i].y=a[h].y;
			b[i].side=a[h].side;
			b[i].num=a[h++].num;
		}
		else 
		{
			b[i].x=a[t].x;
			b[i].y=a[t].y;
			b[i].side=a[t].side;
			b[i].num=a[t++].num;
		}
	}
	for(int i=l;i<=r;i++) a[i]=b[i];
}
void msort(int l,int r)
{
	if(l==r) return ;
	int mid=l+r>>1;
	msort(l,mid);
	msort(mid+1,r);
	merge(l,r);
}
int check(line x,line y)
{
	if(x.y==y.y) return x.x<y.x;
	return x.y>y.y;
}
int check2(line x,line y)
{
	if(x.y==y.y) return x.x>y.x;
	return x.y<y.y;
}
void shiftdown(int x)
{
	line t=h[x];
	while(x*2<=len)
	{
		x*=2;
		if(x+1<=len&&check2(h[x],h[x+1])) x++;
		if(check2(t,h[x])) h[x/2]=h[x];
		else break;
	}
	h[x]=t;
}
void shiftup(int x)
{
	line t=h[x];
	while(x>1&&check(t,h[x/2]))
	{
		h[x]=h[x/2];
		x/=2;
	}
	h[x]=t;
}
void _delete(int x)
{
	line yy=h[len];
	len--;
	if(!len)
	{
		return ;
	}
	h[x]=yy;
	shiftdown(x);
}
void _insert(line x)
{
	if(x.side==1)
	{
		yyy=h[1].y;
		h[++len]=x;
		shiftup(len);
		if(yyy!=h[1].y)
		{
			yyy=h[1].y;
			cout<<x.x<<" "<<yyy<<" ";
		}
	}
	else
	{
		vis[x.num]=1;
//		if(x.x==2146248979)
//		{
//			for(int i=1;i<=len;i++) cout<<x.x<<" "<<x.y<<" "<<x.num<<endl;
//		}
		while(len&&vis[h[1].num])
		{
			_delete(1);
		}
		if(!len||h[1].y!=yyy)
		{	
			if(!len)
			{
				yyy=0;
				len=0;
			 } 
			else yyy=h[1].y;
			cout<<x.x<<" "<<yyy<<" ";
		}
	}
}
int main()
{
	memset(vis,0,sizeof(vis));
	cin>>n;
	for(int i=1,x,y,z;i<=n;i++)
	{
		cin>>x>>y>>z;
		a[++cnt].x=x;
		a[cnt].y=y;
		a[cnt].side=1;
		a[cnt].num=(cnt+1)/2;
		a[++cnt].x=z;
		a[cnt].y=y;
		a[cnt].side=2;
		a[cnt].num=(cnt+1)/2;
	}
	msort(1,cnt);
	for(int i=1;i<=cnt;i++)
	{
		_insert(a[i]);
	}
	return 0;
}
2023/8/8 19:43
加载中...