线段树+离散化。
对所有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);
}