【poj1151】【離散化】Atlantis

Atlantis

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 21743 Accepted: 8184

Description

There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.

Input

The input consists of several test cases. Each test case starts with a line containing a single integer n (1 <= n <= 100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don’t process it.

Output

For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.
Output a blank line after each test case.

Sample Input

2
10 10 20 20
15 15 25 25.5
0

Sample Output

Test case #1
Total explored area: 180.00

Source

Mid-Central European Regional Contest 2000

題目大意就是說給你n個矩形,然後讓你求面積
然後只有100個矩陣,但是大小很大,而且是實數,所以要離散,然後掃面線求出面積
我比較懶。不想寫那麼多字,就推薦你們看這個:http://blog.csdn.net/youngyangyang04/article/details/7787693

然後差不多就這樣,我的代碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<stack>
#define INF 2100000000
#define ll long long
#define clr(x)  memset(x,0,sizeof(x))
#define clrmax(x)  memset(x,127,sizeof(x))

using namespace std;

inline int read()
{
    char c;
    int ret=0;
    while(!(c>='0'&&c<='9'))
        c=getchar();
    while(c>='0'&&c<='9')
    {
        ret=(c-'0')+(ret<<1)+(ret<<3);
        c=getchar();
    }
    return ret;
}

#define M 10005

struct node
{
    double x,y1,y2;
    int flag,no;
}a[M*2];
#define x(y) a[y].x
#define y1(x) a[x].y1
#define y2(x) a[x].y2
#define flag(x) a[x].flag
#define no(x) a[x].no
struct tree2
{
    tree2 *lson,*rson;
    int s,bo;
    double len,l,r;
}*root,dizhi[M];

int T;
const double eps=1E-8;
int n,t,vis[M],cnt;
double t1[M],t2[M],t3[M],t4[M];
double y[M],ans;

bool com(node a,node b)
{
    return a.x<b.x;
}

bool equal(double a,double b)
{
    return (a+eps>=b&&a-eps<=b);
}

void bulid(tree2 *tree,int l,int r)
{
    tree->l=y[l];
    tree->r=y[r];
    tree->s=tree->len=0;
    if(r-l==1)
    {
        tree->bo=1;
        return ;
    }
    int mid=(l+r)>>1;
    tree->lson=&dizhi[T++];
    tree->rson=&dizhi[T++];
    bulid(tree->lson,l,mid);
    bulid(tree->rson,mid,r);
}

void update(tree2 *&tree)
{
    if(tree->s>0)
    {
        tree->len=tree->r-tree->l;
        return ;
    }
    if(tree->bo)
    {
        tree->len=0;
        return ;
    }
    tree->len=tree->lson->len+tree->rson->len;
}

void change(tree2 *tree,int l,int r,node x)
{
    if(equal(x.y1,x.y2))return ;
    if(equal(tree->l,x.y1)&&equal(tree->r,x.y2))
    {
        tree->s+=x.flag;
        update(tree);
        return ;
    }
    int mid=(l+r)>>1;
    if(x.y2<tree->lson->r)change(tree->lson,l,mid,x);
    else if(x.y1>tree->rson->l)change(tree->rson,mid,r,x);
    else
    {
        node temp=x;
        temp.y2=tree->lson->r;
        change(tree->lson,l,mid,temp);
        temp=x;
        temp.y1=tree->rson->l;
        change(tree->rson,mid,r,temp);
    }
    update(tree);
}



int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    while(cin>>n)
    {
        cnt++;
        if(n==0)return 0;
        clr(a);clr(vis);ans=0;t=0;
        clr(dizhi);T=0;
        root=&dizhi[T++];
        for(int i=1;i<=n;i++)
        {
            double x1,x2,y1,y2;
            scanf("%lf%lf%lf%lf",&t1[i],&t2[i],&t3[i],&t4[i]);
            x1=t1[i];x2=t3[i];
            y1=t2[i];y2=t4[i];
            y[++t]=y1;
            x(t)=x1;y1(t)=y1;y2(t)=y2;flag(t)=1;no(t)=i;
            y[++t]=y2;
            x(t)=x2;y1(t)=y1;y2(t)=y2;flag(t)=-1;no(t)=i;
        }
        sort(a+1,a+2*n+1,com);
        sort(y+1,y+2*n+1);
        bulid(root,1,2*n);
        double last=x(1);
        vis[no(1)]=flag(1);
        for(int i=1;i<=2*n;i++)
        {
            ans+=(x(i)-x(i-1))*root->len;
            change(root,1,2*n,a[i]);
        }
        printf("Test case #%d\nTotal explored area: %.2f\n\n",cnt,ans);
    }
    return 0;
}

大概就是這個樣子,如果有什麼問題,或錯誤,請在評論區提出,謝謝。

發佈了76 篇原創文章 · 獲贊 10 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章