ZOJ 3664 Split the Rectangle 第37届ACM/ICPC长春赛区现场赛 J 题(模拟建树,暴力 求LCA)

Split the Rectangle

Time Limit: 2 Seconds      Memory Limit: 32768 KB

Alice comes up with a new game and wants to play with Bob.

There is one rectangle on the paper initially, we define its lower-left corner's coordinate is (xL, yL) and upper-right corner's coordinate is (xR, yR).

Bob has executed the step as description below N times:

Bob should select one empty rectangle. If the rectangle is the initial one or is split by one vertical segment, he should split it into two parts by drawing one horizontal segment; otherwise he should split the rectangle into two parts by drawing one vertical segment. An empty rectangle means there is no segment in this rectangle except its only four boundary segments.

You should pay attention that there are only two kinds segments: vertical segment and horizontal segment.

Now Bob has several queries. In each query, Bob selects two target points in the paper. (You can assume that all given target points are always located inside the initial rectangle and not in any drawing segments.) He wants Alice to answer the question as soon as possible: Alice can erase several existing segments, and make two target points in one empty rectangle, and she should answer how many empty rectangles at most would be left at last.

But there are some restrictions: Alice cannot erase segments of the initial rectangle (the (xL, yL) to (xR, yR) one), she can only erase segments drew by Bob; if Alice want to erase one segment, both sides of the segment must be empty rectangles, and after erase it, the two empty rectangles must combine to one bigger empty rectangle; if erasing an existing segment will lead to a disconnected graph, the operation is forbidden.

Input

There are multiple test cases.

The first line contains four integers xL, yL, xR, yR indicating the coordinates of the lower-left corner and the upper-right corner of the initial huge rectangle respectively. (-100,000 ≤ xL, yL, xR, yR ≤ 100,000, xL< xR, yL< yR)

The next line contains two integers N and Q. (1 ≤ N, Q ≤ 1000)

The next N lines each line contains four integers x1, y1, x2, y2 indicating the coordinates of two endpoints of one drawing segments. (-100,000 ≤ x1, y1, x2, y2 ≤ 100,000, x1=x2|y1=y2)

The next Q lines each line contains four integers xA, yA, xB, yB indicating the coordinates of two target points in this query. (-100,000 ≤ xA, yA, xB, yB ≤ 100,000).

Output

For each test case, output Q lines, output the answer of each query in each line.

Sample Input

-10 -10 10 10
5 1
-10 0 10 0
5 -10 5 0
-5 0 -5 10
-5 5 10 5
5 -5 10 -5
0 -3 7 -3
0 0 4 4
3 2
0 2 4 2
2 0 2 2
2 2 2 4
1 1 1 3
1 1 3 1
-10 -10 10 10
3 4
-10 0 10 0
0 -10 0 0
0 0 0 10
-9 -9 -8 -8
-9 -9 -9 9
-9 -9 9 -9
-9 -9 9 9

Sample Output

4
1
3
4
1
3
1

Contest: The 2012 ACM-ICPC Asia Changchun Regional Contest

 

此题长春赛时杯具的连题目都没有看。

其实这题是不难的,注意是看懂题目意思。

只要按照加入的边建立树。

然后每次的查询就是求出这个点所在的叶子结点编号。然后求出这两个叶子结点的LCA,暴力求LCA即可。

然后答案就是 n+1-(LCA所在结点下面的叶子结点个数)+1

 

很好的一道题。

注意给的点是没有顺序的。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std;
const int MAXN=2020;

struct Node
{
    int lson,rson,father;
    int dep;//深度
    int xl,yl,xr,yr;
    void init(int de,int f)
    {
        lson=rson=0;
        father=f;
        dep=de;
    }
    void update(int a,int b,int c,int d)
    {
        xl=a;yl=b;xr=c;yr=d;
    }
}node[MAXN];//结点
int tol;
int num[MAXN];//每个结点下的叶子结点个数

int root;//根结点

int find(int root,int x,int y)
{
    int tmp=root,tt;
    while(1)
    {
        if(node[tmp].lson==0)return tmp;
        tt=node[tmp].lson;
        if(x<=node[tt].xr&&x>=node[tt].xl&&y>=node[tt].yl&&y<=node[tt].yr)tmp=tt;
        else tmp=node[tmp].rson;
    }
}

int get_num(int now)
{
    num[now]=0;
    if(node[now].lson==0)
    {
        return num[now]=1;
    }
    else
    {
        num[now]+=get_num(node[now].lson);
        num[now]+=get_num(node[now].rson);
    }
    return num[now];
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int xl,yl,xr,yr;
    int n,q;

    while(scanf("%d%d%d%d",&xl,&yl,&xr,&yr)!=EOF)
    {
        root=0;
        node[root].init(0,-1);
        node[root].update(xl,yl,xr,yr);
        tol=1;
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d%d",&xl,&yl,&xr,&yr);
            if(xl>xr)swap(xl,xr);
            if(yl>yr)swap(yl,yr);
            int pos=find(root,(xl+xr)/2,(yl+yr)/2);
            int dep=node[pos].dep;

            node[pos].lson=tol;
            node[tol].init(dep+1,pos);
            node[tol].update(node[pos].xl,node[pos].yl,xr,yr);
            tol++;

            node[pos].rson=tol;
            node[tol].init(dep+1,pos);
            node[tol].update(xl,yl,node[pos].xr,node[pos].yr);
            tol++;
        }
        get_num(root);
        while(q--)
        {
            scanf("%d%d%d%d",&xl,&yl,&xr,&yr);
            int tmp1=find(root,xl,yl);
            int tmp2=find(root,xr,yr);

            while(tmp1!=tmp2)
            {
                if(node[tmp1].dep<node[tmp2].dep)tmp2=node[tmp2].father;
                else if(node[tmp1].dep>node[tmp2].dep)tmp1=node[tmp1].father;
                else
                {
                    tmp1=node[tmp1].father;
                    tmp2=node[tmp2].father;
                }
            }
            printf("%d\n",n+1-num[tmp1]+1);
        }
    }
    return 0;
}

 

人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想

posted on 2012-10-19 13:54  kuangbin  阅读(1065)  评论(0编辑  收藏  举报

导航

JAVASCRIPT: