[codevs3044][POJ1151]矩形面积求并

试题描述

输入n个矩形,求他们总共占地面积(也就是求一下面积的并)

输入

可能有多组数据,读到n=0为止(不超过15组)

每组数据第一行一个数n,表示矩形个数(n<=100)

接下来n行每行4个实数x1,y1,x2,y1(0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000),表示矩形的左下角坐标和右上角坐标

输出

每组数据输出一行表示答案

输入示例

2
10 10 20 20
15 15 25 25.5
0

输出示例

180.00

数据规模及约定

见“输入

题解

扫描线 + 线段树。

线段树标记永久化,因为这题每个时刻只需要知道线段树根节点的信息,而不是每次查询一段区间,所以很容易实现,具体见代码,或者黄学长的题解。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cmath>
using namespace std;int read() {int x = 0, f = 1; char c = getchar();while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }return x * f;
}#define maxn 110struct Line {int l, r, h, tp;Line() {}Line(int _1, int _2, int _3, int _4): l(_1), r(_2), h(_3), tp(_4) {}bool operator < (const Line& t) const { return h < t.h; }
} ls[maxn<<1];
double posx[maxn<<1], posy[maxn<<1], numx[maxn<<1], numy[maxn<<1], ans;int cntv[maxn<<3];
double sumv[maxn<<3];
void maintain(int L, int R, int o) {int lc = o << 1, rc = lc | 1;if(cntv[o]) sumv[o] = numx[R] - numx[L-1];else if(L == R) sumv[o] = 0;else sumv[o] = sumv[lc] + sumv[rc];return ;
}
void update(int L, int R, int o, int ql, int qr, int v) {if(ql <= L && R <= qr) {cntv[o] += v;return maintain(L, R, o);}int M = L + R >> 1, lc = o << 1, rc = lc | 1;if(ql <= M) update(L, M, lc, ql, qr, v);if(qr > M) update(M+1, R, rc, ql, qr, v);return maintain(L, R, o);
}int main() {while(1) {int n = read(), cntx = 0, cnty = 0, cntl = 0;if(!n) break;for(int i = 1; i <= n; i++) {double x1, x2, y1, y2;scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);posx[++cntx] = x1; posx[++cntx] = x2;posy[++cnty] = y1; posy[++cnty] = y2;numx[cntx-1] = posx[cntx-1]; numx[cntx] = posx[cntx];numy[cnty-1] = posy[cnty-1]; numy[cnty] = posy[cnty];}sort(numx + 1, numx + cntx + 1);sort(numy + 1, numy + cnty + 1);for(int i = 1; i <= n; i++) {int x1, x2, y1, y2;x1 = lower_bound(numx + 1, numx + cntx + 1, posx[(i<<1)-1]) - numx;x2 = lower_bound(numx + 1, numx + cntx + 1, posx[i<<1]) - numx;y1 = lower_bound(numy + 1, numy + cnty + 1, posy[(i<<1)-1]) - numy;y2 = lower_bound(numy + 1, numy + cnty + 1, posy[i<<1]) - numy;ls[++cntl] = Line(x1, x2, y1, 1);ls[++cntl] = Line(x1, x2, y2, -1);}sort(ls + 1, ls + cntl + 1);memset(cntv, 0, sizeof(cntv));memset(sumv, 0, sizeof(sumv));ans = 0;double start = numx[1];for(int i = 1; i < cntx; i++) numx[i] = numx[i+1] - start;for(int i = 1; i < cntl; i++) {if(ls[i].l < ls[i].r) update(1, cntx - 1, 1, ls[i].l, ls[i].r - 1, ls[i].tp);ans += (numy[ls[i+1].h] - numy[ls[i].h]) * sumv[1];}printf("%.2lf\n", ans);}return 0;
}

注意:POJ 上输出格式不太一样,详见题面。

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6474671.html

[codevs3044][POJ1151]矩形面积求并相关推荐

  1. 【codevs3044】矩形面积求并【POJ1151】Atlantis,第一次的扫描线

    传送门1 传送门2 思路: 第一次写扫描线 感觉还是很资瓷的 仅此作为第一次系列的又一个新成员 代码: #include<cstdio> #include<iostream> ...

  2. [CODEVS 3044] 矩形面积求并

    描述 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) http://codevs.cn/problem/3044/ 分析 先贴个Matrix67的讲离散化的博客地址: http://www. ...

  3. POJ 1151 Atlantis 矩形面积求交/线段树扫描线

    Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...

  4. 多个矩形,求覆盖面积,周长,及交点

    问题:给出若干个矩形,(给的是矩形左上角和右下角坐标),求最后所得图形的面积/周长: 三个矩形如左图所示,而若要计算面积,看右图,用3个矩形各自的面积之和减去重复部分(红色和蓝色)的面积 人算很简单, ...

  5. HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)

    链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...

  6. C语言:编写一个程序,从键盘读入一个矩形的两个边的值(整数),求矩形面积

    /* 编写一个程序,从键盘读入一个矩形 的两个边的值(整数),求矩形面积. */ #include<stdio.h> void main() {int length,wide,area;p ...

  7. c语言直方图最大矩形面积,利用枚举法求直方图中最大矩形面积的方法实例

    求直方图中的最大矩形面积: 例如给定直方图{2,3,1,2,4,2} 则直方图中最大矩形面积为x=(3,6),|x|=3,y=2,max面积=6 思考:利用枚举法 /*当前位置往前进行枚举法*/ pu ...

  8. 已知矩形面积,求最小周长

    1283 最小周长 1.0 秒 131,072.0 KB 20 分 初学者3级题 一个矩形的面积为S,已知该矩形的边长都是整数,求所有满足条件的矩形中,周长的最小值.例如:S = 24,那么有{1 2 ...

  9. 采用面向对象思想求矩形面积

    ** 采用面向对象思想求矩形面积 ** Rctangle.java 代码: void rectangle(float length,float width) { System.out.println( ...

最新文章

  1. python3下载文件-python 3.3 下载固定链接文件并保存的方法
  2. 软件项目管理0714:简化的必要性
  3. [云炬创业学笔记]第二章决定成为创业者测试13
  4. int string java 呼转
  5. Centos7 安装Go环境
  6. 如何用Pygame写游戏(二十二)
  7. vxlan 分布式网关数据包转发过程_Vxlan基础
  8. Android 启动过程简析(一)之 init 进程
  9. 官方实锤!程序员都是农民工?
  10. 如何搭建python框架_从0到1告诉你搭建完整Python+requests接口自动化测试框架!
  11. Linux C++使用MySQL数据库
  12. 10分钟10行代码开发APP(delphi 应用案例)
  13. Unity3D的四种坐标系
  14. 大规模Web服务开发技术
  15. Win10“隐藏”了一个视频编辑器,好用,免费,很多人却不知道
  16. 纪念我的第一次面试——华为研发类面试
  17. 飞信2010分析 – SIPC验证
  18. 10组团队项目-Alpha冲刺-6/6
  19. 【烙铁使用规范】—— 烙铁头使用及保养
  20. python百度语音实时识别成文字

热门文章

  1. python3高性能网络编程_Python3 网络编程
  2. 你知道Integer和int的区别吗
  3. 局域网上传文件到服务器很慢,win10局域网内传文件很慢怎么办_win10局域网内文件传输很慢如何处理-win7之家...
  4. qvector 结构体排序_C++结构体的应用_YCOJ
  5. bat命令 修改ini文件内容_Linux文件内容查看相关命令
  6. 网工必备的存储知识详解
  7. 【干货】超全!华为交换机端口vlan详解~
  8. mac怎么合并两个容器_PDF怎样合并?在Mac上合并PDF文件的最佳方法
  9. 电脑字体模糊_2020年初电脑配件和配置单推荐!
  10. mysql2005错误_SQL Server 2005 还原数据库错误解决方法