POJ 1389 Area of Simple Polygons 扫描线+线段树面积并
---恢复内容开始---
题意:同POJ1151
思路:
/** @Date : 2017-07-19 13:24:45
* @FileName: POJ 1389 线段树+扫描线+面积并 同1151.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
#include <math.h>
//#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; struct yuu
{
int l, r;
int flag;
int ll, rr;
int len;
}tt[N]; struct line{
int y, x1, x2;
int flag;
line(){}
line(int _x1, int _x2, int _y, int f){
x1 = _x1;
x2 = _x2;
y = _y;
flag = f;
}
};
line li[N];
int cmp(line a, line b)
{
return a.y < b.y;
}
double a[N], b[N]; void pushup(int p)
{
if(tt[p].flag > 0)
{
tt[p].len = tt[p].rr - tt[p].ll;
return ;
}
if(tt[p].l == tt[p].r - 1)
tt[p].len = 0;
else
tt[p].len = tt[p << 1].len + tt[p << 1 | 1].len;
}
void build(int l, int r, int p)
{
tt[p].l = l;
tt[p].r = r;
tt[p].len = tt[p].flag = 0;
tt[p].ll = a[l];
tt[p].rr = a[r];
if(l == r - 1)
return ;
int mid = (l + r) >> 1;
build(l, mid, p << 1);
build(mid, r, p << 1 | 1);
} void update(int x1, int x2, int flag, int p)
{
if(x1 == tt[p].ll && x2 == tt[p].rr)
{
tt[p].flag += flag;
pushup(p);
return ;
}
if(x2 <= tt[p << 1].rr)
update(x1, x2, flag, p << 1);
else if(x1 >= tt[p << 1 | 1].ll)
update(x1, x2, flag, p << 1 | 1);
else
{
update(x1, tt[p << 1].rr, flag, p << 1);
update(tt[p << 1 | 1].ll, x2, flag, p << 1 | 1);
}
pushup(p);
} int main()
{
int x1, x2, y1, y2;
while(~scanf("%d%d%d%d", &x1, &y1, &x2, &y2) && ( (~x1) || (~x2) || (~y1) || (~y2)))
{
int cnt = 1;
li[cnt] = line(x1, x2, y1, 1);
a[cnt++] = x1;
li[cnt] = line(x1, x2, y2, -1);
a[cnt++] = x2;
while(scanf("%d%d%d%d", &x1, &y1, &x2, &y2) && (((~x1)||(~x2)||(~y1)||(~y2))))
{
li[cnt] = line(x1, x2, y1, 1);
a[cnt++] = x1;
li[cnt] = line(x1, x2, y2, -1);
a[cnt++] = x2;
}
sort(li + 1, li + cnt, cmp);
sort(a + 1, a + cnt);
build(1, cnt - 1, 1);
update(li[1].x1, li[1].x2, li[1].flag, 1);
int ans = 0;
for(int i = 2; i < cnt; i++)
{
//cout << li[i].x1 << "#" << li[i].x2 << "#" << li[i].y << endl;
ans += tt[1].len * (li[i].y - li[i - 1].y);
update(li[i].x1, li[i].x2, li[i].flag, 1);
//cout << "~";
}
printf("%d\n", ans); }
return 0;
}
---恢复内容结束---
POJ 1389 Area of Simple Polygons 扫描线+线段树面积并的更多相关文章
- POJ1389:Area of Simple Polygons——扫描线线段树题解+全套代码注释
http://poj.org/problem?id=1389 题面描述在二维xy平面中有N,1 <= N <= 1,000个矩形.矩形的四边是水平或垂直线段.矩形由左下角和右上角的点定义. ...
- POJ 1389 Area of Simple Polygons | 扫描线
请戳此处 #include<cstdio> #include<algorithm> #include<cstring> #define N 1010 #define ...
- 【POJ 1389】Area of Simple Polygons(线段树+扫描线,矩形并面积)
离散化后,[1,10]=[1,3]+[6,10]就丢了[4,5]这一段了. 因为更新[3,6]时,它只更新到[3,3],[6,6]. 要么在相差大于1的两点间加入一个值,要么就让左右端点为l,r的线段 ...
- [poj] 1389 Area of Simple Polygons
原题 线段树+扫描线 对于这样一个不规则图形,我们要求他的面积有两种方法,割和补. 补显然不行,因为补完你需要求补上去的内部分不规则图形面积-- 那么怎么割呢? 像这样: 我们就转化成了无数个矩形的和 ...
- POJ Area of Simple Polygons 扫描线
这个题lba等神犇说可以不用离散化,但是我就是要用. 题干: Description There are N, <= N <= , rectangles -D xy-plane. The ...
- HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)
Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...
- HDU 3642 - Get The Treasury - [加强版扫描线+线段树]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3642 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积
题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> ...
- POJ 2828 Buy Tickets(排队问题,线段树应用)
POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意: 排队买票时候插队. 给出一些数对,分别代表某个人的想要插入的位 ...
随机推荐
- WITH HINDSIGHT
设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们是要做一个基于文件同步展示的语音软件:感谢之前的两次项目审核,我们定义与描述得很清楚: 我们达到目标 ...
- Maven 私服安装和启动
在安装私服的时候容易碰到的两个问题,一个是安装时拒绝访问,另一个是安装完成后服务无法启动: 拒绝访问问题: 原因:没有以管理员身份运行 cmd 解决办法: 如果是 win7 的话,可以直接在 [运行- ...
- 通过父类定位到子类 先将父类当作一个dom
通过父类定位到子类 先将父类当作一个dom 在此基础上在定位子类
- vdbench测试过程中遇到的小问题
1.报Slave hd2-0 prematurely terminated 错误 首先根据提示查看hd2-0.stdout.html文件获取更多的错误信息,这个问题一般是未安装vdbench或者路径不 ...
- 【题解】AC自动机题解合集
最近貌似大家都在搞字符串?很长一段时间都没有写博客了……还是补一补坑吧. 感觉AC自动机真的非常优美了,通过在trie树上建立fail指针可以轻松解决多模匹配的问题.实际上在AC自动机上的匹配可以看做 ...
- 跟我学Spring Cloud(Finchley版)-20-Spring Cloud Config-Git仓库配置详解 原
在跟我学Spring Cloud(Finchley版)-19-配置中心-Spring Cloud Config 一节中,已实现使用Git仓库作为Config Server的后端存储,本节详细探讨如何配 ...
- 【JQuery】文档操作
一.前言 接着上一章的内容,接着JQuery的学习 二.内容 addClass 向被选元素添加一个或多个类 $(selector).addClass(class) $(selector).addCla ...
- BZOJ4033:[HAOI2015]树上染色——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4033 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将 ...
- Thuwc2018 游记
上一次没有滚粗的比赛已经是9个月前了QAQ.但我现在回过头去看那篇“zjoi游记”,却发现自己并不能从中得到收获.希望这次写下的东西,可以帮助我更好地准备即将到来的省选(雾) day 0 火车上浪10 ...
- hadoop(四)HDFS的核心设计
一.hadoop心跳机制(heartbeat) 1. Hadoop 是 Master/Slave 结构, Master 中有 NameNode 和 ResourceManager, Slave 中有 ...