【POJ 1151】 Atlantis(线段树,扫描线,离散化)

本文介绍了一种解决地图重叠区域计算问题的高效算法,通过离散化和扫描线技术,结合线段树数据结构,实现了O(nlogn)的时间复杂度,避免了O(n^3)的暴力解法。

题目

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 ) n (1 <= n <= 100) n(1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x 1 , y 1 , x 2 , y 2 ( 0 < = x 1 < x 2 < = 100000 ; 0 < = y 1 < y 2 < = 100000 ) x1,y1,x2,y2 (0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000) x1,y1,x2,y2(0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values ( x 1 , y 1 ) (x1, y1) (x1,y1) and ( x 2 , y 2 ) (x2,y2) (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 0 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 k k is the number of the test case (starting with 1 1 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

题目传送门
题目传送门(vjudge)

思路

线段树 + + + 扫描线 + + + 离散化。
如果这 n n n 个矩形的坐标都是整数,那么……:直接暴力打标记!
但是不是这样的。

坐标大小很大,而且是实数,于是我们想到离散化,然后……
我们继续BF,打标记
离散化玩暴力打标记的时间复杂度为 O ( n 3 ) O(n^{3}) O(n3),无法接受 (其实完全可以接受)
我们要的是正解,正解!!!

于是,map 扫描线就来了。
首先,我们对纵坐标离散化。
然后,每个纵向边作为扫描线,从左往右扫,当前扫描线和下一个扫描线的距离 × \times ×已覆盖长度就是一部分的面积,最后累加起来。
具体怎么实现呢?
线段树大法好!
只需要用一颗线段树维护即可。
时间复杂度 O ( n l o g n ) O(n log n) O(nlogn)

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>

using namespace std;
double y[10010];
int cnt, cas, n;
struct node {
    double x;
    double y1, y2;
    bool lft;

    bool operator < (const node& e) const {
        return x < e.x;
    }
} a[10010];
struct Node {
    int L, R, cv;
    double len;
} tr[1010010];

int to(double x) {
    return lower_bound(y, cnt+y, x)-y;
}

void mlen(int k) {
    if (tr[k].cv > 0) tr[k].len = y[tr[k].R] - y[tr[k].L];
    else if (tr[k].L + 1 == tr[k].R) tr[k].len = 0;
    else tr[k].len = tr[k*2].len + tr[k*2+1].len;
}

void build(int k, int L, int R) {
    tr[k].L = L; tr[k].R = R;
    tr[k].len = tr[k].cv = 0;
    if (L + 1 == R) return;
    int mid = (L + R) >> 1;
    build(k*2, L, mid);
    build(k*2+1, mid, R);
}

void insert(int k, int ql, int qr) {
    if (ql <= tr[k].L && tr[k].R <= qr) {
        tr[k].cv++;
        mlen(k);
        return;
    }
    int mid = (tr[k].L+tr[k].R) >> 1;
    if (ql < mid) insert(k*2, ql, qr);
    if (qr > mid) insert(k*2+1, ql, qr);
    mlen(k);
}

void del(int k, int ql, int qr) {
    if (ql <= tr[k].L && tr[k].R <= qr) {
        tr[k].cv--;
        mlen(k);
        return;
    }
    int M = (tr[k].L+tr[k].R)  >>  1;
    if (ql < M) del(k*2, ql, qr);
    if (qr > M) del(k*2+1, ql, qr);
    mlen(k);
}

int main() {
    double x1, y1, x2, y2;
    while (~scanf("%d", &n)) {
    	if (n == 0) return 0;
        for (int i = 0; i < n; i++) {
            scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
            a[i*2].x = x1; a[i*2].y1 = y1; a[i*2].y2 = y2; a[i*2].lft = true;
            a[(i*2)+1].x = x2; a[(i*2)+1].y1 = y1; a[(i*2)+1].y2 = y2; a[(i*2)+1].lft = false;
            y[i*2] = y1; y[(i*2)+1] = y2;
        }
        sort(y, y + 2*n);
        cnt = unique(y, y + 2*n) - y;
        sort(a, a + 2*n);
        build(1, 0, cnt-1);
        double ans = 0;
        for (int i = 0; i < 2*n-1; i++) {
            if (a[i].lft) insert(1, to(a[i].y1), to(a[i].y2));
            else del(1, to(a[i].y1), to(a[i].y2));
            ans += (a[i+1].x - a[i].x) * tr[1].len;
        }
        printf("Test case #%d\n", ++cas);
        printf("Total explored area: %.2f\n\n", ans);
    }
    return 0;
}
**项目名称:** 基于Vue.js与Spring Cloud架构的博客系统设计与开发——微服务分布式应用实践 **项目概述:** 本项目为计算机科学与技术专业本科毕业设计成果,旨在设计并实现一个采用前后端分离架构的现代化博客平台。系统前端基于Vue.js框架构建,提供响应式用户界面;后端采用Spring Cloud微服务架构,通过服务拆分、注册发现、配置中心及网关路由等技术,构建高可用、易扩展的分布式应用体系。项目重点探讨微服务模式下的系统设计、服务治理、数据一致性及部署运维等关键问题,体现了分布式系统在Web应用中的实践价值。 **技术架构:** 1. **前端技术栈:** Vue.js 2.x、Vue Router、Vuex、Element UI、Axios 2. **后端技术栈:** Spring Boot 2.x、Spring Cloud (Eureka/Nacos、Feign/OpenFeign、Ribbon、Hystrix、Zuul/Gateway、Config) 3. **数据存储:** MySQL 8.0(主数据存储)、Redis(缓存与会话管理) 4. **服务通信:** RESTful API、消息队列(可选RabbitMQ/Kafka) 5. **部署与运维:** Docker容器化、Jenkins持续集成、Nginx负载均衡 **核心功能模块:** - 用户管理:注册登录、权限控制、个人中心 - 文章管理:富文本编辑、分类标签、发布审核、评论互动 - 内容展示:首页推荐、分类检索、全文搜索、热门排行 - 系统管理:后台仪表盘、用户与内容监控、日志审计 - 微服务治理:服务健康检测、动态配置更新、熔断降级策略 **设计特点:** 1. **架构解耦:** 前后端完全分离,通过API网关统一接入,支持独立开发与部署。 2. **服务拆分:** 按业务域划分为用户服务、文章服务、评论服务、文件服务等独立微服务。 3. **高可用设计:** 采用服务注册发现机制,配合负载均衡与熔断器,提升系统容错能力。 4. **可扩展性:** 模块化设计支持横向扩展,配置中心实现运行时动态调整。 **项目成果:** 完成了一个具备完整博客功能、具备微服务典型特征的分布式系统原型,通过容器化部署验证了多服务协同运行的可行性,为云原生应用开发提供了实践参考。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值