[codevs3044][POJ1151]矩形面积求并
[codevs3044][POJ1151]矩形面积求并
试题描述
输入
可能有多组数据,读到n=0为止(不超过15组)
每组数据第一行一个数n,表示矩形个数(n<=100)
接下来n行每行4个实数x1,y1,x2,y1(0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000),表示矩形的左下角坐标和右上角坐标
输出
每组数据输出一行表示答案
输入示例
25.5
输出示例
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 110 struct 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 上输出格式不太一样,详见题面。
[codevs3044][POJ1151]矩形面积求并的更多相关文章
- codves 3044 矩形面积求并
codves 3044 矩形面积求并 题目等级 : 钻石 Diamond 题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Desc ...
- poj-1151矩形面积并-线段树
title: poj-1151矩形面积并-线段树 date: 2018-10-30 22:35:11 tags: acm 刷题 categoties: ACM-线段树 概述 线段树问题里的另一个问题, ...
- codevs 3044 矩形面积求并
3044 矩形面积求并 题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不 ...
- [Codevs] 矩形面积求并
http://codevs.cn/problem/3044/ 线段树扫描线矩形面积求并 基本思路就是将每个矩形的长(平行于x轴的边)投影到线段树上 下边+1,上边-1: 然后根据线段树的权值和与相邻两 ...
- [codevs3044]矩形面积求并
题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行 ...
- 3044 矩形面积求并 - Wikioi
题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行 ...
- POJ 1151 Atlantis 矩形面积求交/线段树扫描线
Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...
- 矩形面积求并(codevs 3044)
题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行 ...
- poj1151==codevs 3044 矩形面积求并
Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21511 Accepted: 8110 Descrip ...
随机推荐
- 转】RDD与DataFrame的转换
原博文出自于: http://www.cnblogs.com/namhwik/p/5967910.html RDD与DataFrame转换1. 通过反射的方式来推断RDD元素中的元数据.因为RDD本身 ...
- VMware Workstation安装CentOS 7和开发环境
VMware Workstation新建虚拟机 此处使用的是VMware Workstation 10,其安装过程即是常规Windos系统下软件安装方式,略过. 安装完成双击图标: 打开虚拟机主界面: ...
- SpringMVC的简单传值
之前学习SpringMVC时感觉他的传值很神奇:简便,快捷,高效. 今天写几个简单的传值与大家分享,希望能对大家有帮助. 一. 从后往前传: (1) @Controller @RequestMappi ...
- 免费大数据搜索引擎 xunsearch 实践
以前在IBM做后端开发时,也接触过关于缓存技术,当时给了n多文档来学习,后面由于其他紧急的项目,一直没有着手去仔细研究这个技术,即时后来做Commerce的时候,后台用了n多缓存技术,需要build ...
- oracle 安装,启动 ,plsql 连接
1.下载oracle 服务器端,正常安装,在选择桌面类或者是服务器类的时候选择服务器类. 2.下载oracle 客户端解压版 下载地址 链接:https://pan.baidu.com/s/1mi ...
- Vuex/Vue 练手项目 在线汇率转换器
详情请点击: https://zhuanlan.zhihu.com/p/33362758
- org.springframework.orm.hibernate4.support.OpenSessionInterceptor
/* * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Vers ...
- 物联网初学者智能家居必备迅为iTOP-4412开发板
更情点击了解:http://www.topeetboard.com 1. 手把手全视频教程: 第一部分:迅为电子开发板入门视频 第二部分:Linux系统编程 第三部分:Itop-4412开发板硬件设 ...
- Beta冲刺提交-星期五
Beta冲刺提交-星期五 这个作业属于哪个课程 软件工程 这个作业要求在哪里 <作业要求的链接> 团队名称 唱跳RAP编程 这个作业的目标 1.进行每日例会,每个成员汇报自己今天完成 ...
- 面包屑 asp代码记录 newsmulu_class 内部函数
'id 这里其实是 classId 'mbStr1 最右边的栏目模板 由于是当前本页面 就不带链接了 建议默认值:<span class='mbxC'>$title</span> ...