POJ 1151 Atlantis

Atlantis
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16244 Accepted: 6183

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


 原理是线段的切割
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define N 100000
using namespace std;
struct num
{
    double x1,y1,x2,y2;
}a[N],b[N];
int Top,pos;
bool uv;
bool cmp(const struct num &p1,const struct num &p2)
{
    return p1.x1<p2.x1;
}
int main()
{
    //freopen("data.txt","r",stdin);
    void ch();
    void add(double x1,double y1,double x2,double y2,int p);
    int n;
    int T=1;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
        {
            break;
        }
        for(int i=0;i<=n-1;i++)
        {
            scanf("%lf %lf %lf %lf",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);
        }
        sort(a,a+n,cmp);
        Top = 0;
        for(int i=0;i<=n-1;i++)
        {
            pos = Top;
            double x3 = a[i].x1,y3 = a[i].y1;
            double x4 = a[i].x2,y4 = a[i].y2;
            bool in = false;
            for(int j=0;j<=Top-1;j++)
            {
               double x1 = b[j].x1,y1 = b[j].y1;
               double x2 = b[j].x2,y2 = b[j].y2;
               if(x3>x2||x1>x4||y3>y2||y1>y4)
               {
                   continue;
               }
               in = true;
               pos = j;
               uv = false;
               double k1 = max(x1,x3);
               double k2 = min(x4,x2);
               if(k1>x1)
               {
                   add(x1,y1,k1,y2,pos);
                   ch();
               }
               if(k2<x2)
               {
                   add(k2,y1,x2,y2,pos);
                   ch();
               }
               double k3 = max(y3,y1);
               double k4 = min(y4,y2);
               if(k3>y1)
               {
                   add(k1,y1,k2,y3,pos);
                   ch();
               }
               if(k4<y2)
               {
                   add(k1,y4,k2,y2,pos);
                   ch();
               }
            }
            add(x3,y3,x4,y4,pos);
            if(!in)
            {
                Top++;
                continue;
            }
            ch();
        }
        double sum = 0;
        for(int i=0;i<=Top-1;i++)
        {
            sum+=(b[i].x2-b[i].x1)*(b[i].y2-b[i].y1);
        }
        printf("Test case #%d\n",T++);
        printf("Total explored area: %.2lf\n",sum);
        printf("\n");
    }
    return 0;
}
void ch()
{
    if(uv)
    {
        Top++;
    }
    pos = Top;
    uv = true;
}
void add(double x1,double y1,double x2,double y2,int p)
{
    b[p].x1 = x1;
    b[p].y1 = y1;
    b[p].x2 = x2;
    b[p].y2 = y2;
}

以上是用矩形切割的想法做的,由于数据量小,没问题,如果矩形较多的话,会tle,今天学了一下扫描线,面积并

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define N 500
using namespace std;
struct num
{
    double x,y1,y2;
    int deg;
}infor[N];
struct tree
{
    int l,r,c;
    double ly,ry,dis;
}a[4*N];
double b[N];
bool cmp(num p1,num p2)
{
    return p1.x<p2.x;
}
int main()
{
    void build(int k,int l,int r);
    void update(int k,num p);
    int n;
    int T = 1;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
        {
            break;
        }
        int t = 1;
        for(int i=1;i<=n;i++)
        {
            double x1,y1,x2,y2;
            scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
            infor[t].x = x1;
            infor[t].y1 = y1;
            infor[t].y2 = y2;
            infor[t].deg = 1;
            b[t++] = y1;
            infor[t].x = x2;
            infor[t].y1 = y1;
            infor[t].y2 = y2;
            infor[t].deg = -1;
            b[t++] = y2;
        }
        sort(infor+1,infor+t,cmp);
        sort(b+1,b+t);
        build(1,1,t-1);
        update(1,infor[1]);
        double res = 0;
        for(int i=2;i<=t-1;i++)
        {
            res+=(infor[i].x-infor[i-1].x)*a[1].dis;
            update(1,infor[i]);
        }
        printf("Test case #%d\n",T++);
        printf("Total explored area: %.2lf\n",res);
        printf("\n");
    }
    return 0;
}
void build(int k,int l,int r)
{
    a[k].l = l;
    a[k].r = r;
    a[k].dis = a[k].c = 0;
    a[k].ly = b[l];
    a[k].ry = b[r];
    if(l+1==r)
    {
        return ;
    }
    int mid = (l+r)>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid,r);
}
void cal(int k)
{
    if(a[k].c>0)
    {
        a[k].dis = a[k].ry - a[k].ly;
        return ;
    }
    if(a[k].l+1==a[k].r)
    {
        a[k].dis = 0;
    }else
    {
        a[k].dis = a[k<<1].dis+a[k<<1|1].dis;
    }
}
void update(int k,num p)
{
    if(a[k].ly==p.y1&&a[k].ry==p.y2)
    {
        a[k].c+=p.deg;
        cal(k);
        return ;
    }
    if(p.y2<=a[k<<1].ry)
    {
        update(k<<1,p);
    }else if(p.y1>=a[k<<1|1].ly)
    {
        update(k<<1|1,p);
    }else
    {
        num e = p;
        e.y2 = a[k<<1].ry;
        update(k<<1,e);
        e = p;
        e.y1 = a[k<<1|1].ly;
        update(k<<1|1,e);
    }
    cal(k);
}



1. 用户与权限管理模块 角色管理: 学生:查看实验室信息、预约设备、提交耗材申请、参与安全考核 教师:管理课题组预约、审批学生耗材申请、查看本课题组使用记录 管理员:设备全生命周期管理、审核预约、耗材采购与分发、安全检查 用户操作: 登录认证:统一身份认证(对接学号 / 工号系统,模拟实现),支持密码重置 信息管理:学生 / 教师维护个人信息(联系方式、所属院系),管理员管理所有用户 权限控制:不同角色仅可见对应功能(如学生不可删除设备信息) 2. 实验室与设备管理模块 实验室信息管理: 基础信息:实验室编号、名称、位置、容纳人数、开放时间、负责人 功能分类:按学科(计算机实验室 / 电子实验室 / 化学实验室)标记,关联可开展实验类型 状态展示:实时显示当前使用人数、设备运行状态(正常 / 故障) 设备管理: 设备档案:名称、型号、规格、购置日期、单价、生产厂家、存放位置、责任人 全生命周期管理: 入库登记:管理员录入新设备信息,生成唯一资产编号 维护记录:记录维修、校准、保养信息(时间、内容、执行人) 报废处理:登记报废原因、时间,更新设备状态为 "已报废" 设备查询:支持按名称、型号、状态多条件检索,显示设备当前可用情况 3. 预约与使用模块 预约管理: 预约规则:学生可预约未来 7 天内的设备 / 实验室,单次最长 4 小时(可设置) 预约流程:选择实验室→选择设备→选择时间段→提交申请(需填写实验目的) 审核机制:普通实验自动通过,高危实验(如化学实验)需教师审核 使用记录: 签到 / 签退:到达实验室后扫码签到,离开时签退,系统自动记录实际使用时长 使用登记:填写实验内容、设备运行情况(正常 / 异常),异常情况需详细描述 违规管理:迟到 15 分钟自动取消预约,多次违规限制预约权限 4. 耗材与安全管理模块 耗材管理: 耗材档案:名称、规格、数量、存放位置、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值