SPOJ AMR12J 题解

14 篇文章 0 订阅
2 篇文章 0 订阅

线段树+离散化。

对所有x轴坐标排序后,从左往右扫,对于新添加和撤销的矩阵更新y轴范围所对应的矩阵序号。

查询操作不需要两个y点都查询,因为矩阵不会相交。


//Memory:14336K
//Time:1610ms
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;

const int N=100010;

struct Room
{
	int x1,x2,y1,y2,id;
}room[N];

struct X
{
	int loc,id;
}x[N<<1];

int y[N<<1],ylen;
int n,xlen;
int ans[N],tree[N<<3],lazy[N<<3];

bool cmp(X A,X B)
{
	return A.loc==B.loc? A.id<B.id : A.loc<B.loc;
}

int BS(int a)
{
	int L=0,R=ylen-1,mid;
	while(L<R) {
		mid=L+((R-L+1)>>1);
		if(y[mid]>a) R=mid-1;
		else L=mid;
	}
	return L;
}

int query(int pos,int l,int r,int key)
{
	if(tree[pos]!=-1) return tree[pos];
	if(lazy[pos]!=-1) {
		tree[pos<<1]=lazy[pos];
		tree[pos<<1|1]=lazy[pos];
		lazy[pos<<1]=lazy[pos];
		lazy[pos<<1|1]=lazy[pos];
		lazy[pos]=-1;
	}
	int mid=(l+r)>>1;
	if(key<=mid) return query(pos<<1,l,mid,key);
	return query(pos<<1|1,mid+1,r,key);
}

void update(int pos,int L,int R,int l,int r,int key)
{
	if(L==l&&r==R) {
		tree[pos]=key;
		lazy[pos]=key;
		return;
	}
	if(lazy[pos]!=-1) {
		tree[pos<<1]=lazy[pos];
		tree[pos<<1|1]=lazy[pos];
		lazy[pos<<1]=lazy[pos];
		lazy[pos<<1|1]=lazy[pos];
		lazy[pos]=-1;
	}
	int mid=(L+R)>>1;
	if(mid>=r) update(pos<<1,L,mid,l,r,key);
	else if(l>mid) update(pos<<1|1,mid+1,R,l,r,key);
	else {
		update(pos<<1,L,mid,l,mid,key);
		update(pos<<1|1,mid+1,R,mid+1,r,key);
	}
	tree[pos]=-1;
}

int main()
{
	memset(lazy,-1,sizeof lazy);
	scanf("%d",&n);
	for(int i=1;i<=n;++i) {
		scanf("%d%d%d%d",&room[i].x1,&room[i].y1,&room[i].x2,&room[i].y2);
		room[i].id=i;
		x[xlen].loc=room[i].x1;
		x[xlen++].id=i;
		x[xlen].loc=room[i].x2;
		x[xlen++].id=-i;
		y[ylen++]=room[i].y1;
		y[ylen++]=room[i].y2;
	}
	sort(x,x+xlen,cmp);
	sort(y,y+ylen);
	ylen=unique(y,y+ylen)-y;
	for(int i=0;i<xlen;++i) {
		int d=x[i].id;
		if(d>0) {
			int a=BS(room[d].y1);
			int b=BS(room[d].y2);
			ans[d]=query(1,0,ylen-1,a);
			update(1,0,ylen-1,a,b,d);
		}
		else {
			int a=BS(room[-d].y1);
			int b=BS(room[-d].y2);
			update(1,0,ylen-1,a,b,ans[-d]);
		}
	}
	for(int i=1;i<=n;++i)
		printf("%d\n",ans[i]-1);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值