zoj 3664 Split the Rectangle

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3664


题目大意:

Bob可以在一个原始空矩形或由一条垂线分割出的空矩形中画一条水平线,反之画一条垂线.

线画完之后,Bob会在原始矩形内选两个目标点,且目标点不会在画的线上,Alice可以删掉画的线使得两点在同一空矩形中,删掉的线必须保证两边都是空矩形,且不能使图不连通,求出删完线后最多能剩几个空矩形.


题目思路:

可以看出构造图的过程是一个二叉树建立的过程,要使两个点合在一起只要知道两点所在矩形的LCA即可,把LCA里画的线全删掉就是了.而且是1000空间复杂度,暴力LCA即可.


代码:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define ll long long
#define ls rt<<1
#define rs ls|1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle l+r>>1
#define inf 0x3F3F3F3F
#define eps (1e-9)
#define MOD 1000000007
typedef pair<int,int> pii;
typedef multiset<int> mset;
typedef multiset<int>::iterator mst_it;
//const double pi=acos(-1.0);
#define M 2000 +5
template <class T> T _max(T x,T y){return x>y? x:y;}
template <class T> T _min(T x,T y){return x<y? x:y;}
template <class T> void _swap(T &x,T &y){T z=x;x=y;y=z;}
int ts,cas=0;

int n,m;
int xl,yl,xr,yr;

struct pot{
	int x,y;
	pot(){}
	pot(int _x,int _y): x(_x),y(_y){}
	void read(){scanf("%d%d",&x,&y);}
};

struct mtx{
	pot a,b;
	int l,r;
	void insert(pot _a,pot _b){a=_a,b=_b;}
	void read(){a.read(),b.read();}
	bool inner(mtx t){
		return (a.x<=t.a.x && t.b.x<=b.x && a.y<=t.a.y && t.b.y<=b.y);
	}
};

mtx val[M],c;
int cnt[M],l[M],r[M],tot;

void update(int rt){
	cnt[rt]++;
	if(l[rt]==-1){
		val[l[rt]=++tot].insert(val[rt].a,c.b);
		val[r[rt]=++tot].insert(c.a,val[rt].b);
		return;
	}
	if(val[l[rt]].inner(c)) update(l[rt]);
	else if(val[r[rt]].inner(c)) update(r[rt]);
}

int query(int rt){
	if(l[rt]==-1) return cnt[rt];
	if(val[l[rt]].inner(c)) return query(l[rt]);
	if(val[r[rt]].inner(c)) return query(r[rt]);
	return cnt[rt];
}

void run(){
    int i,j;
	scanf("%d%d",&n,&m);
	memset(cnt,0,sizeof(cnt));
	memset(l,-1,sizeof(l));
	memset(r,-1,sizeof(r));
	val[tot=1].insert(pot(xl,yl),pot(xr,yr));
	for(i=1;i<=n;i++){
		c.read();
		if(c.a.x>c.b.x) _swap(c.a.x,c.b.x);
		if(c.a.y>c.b.y) _swap(c.a.y,c.b.y);
		update(1);
	}
	while(m--){
		c.read();
		if(c.a.x>c.b.x) _swap(c.a.x,c.b.x);
		if(c.a.y>c.b.y) _swap(c.a.y,c.b.y);
		printf("%d\n",n-query(1)+1);
	}
}

void preSof(){
}

int main(){
    //freopen("1.in","r",stdin);
    //freopen("1.out","w",stdout);
	preSof();
    //run();
    while(~scanf("%d%d%d%d",&xl,&yl,&xr,&yr)) run();
    //for(scanf("%d",&ts),cas=1;cas<=ts;cas++) run();
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值