51nod 1206 Picture 矩形周长求并 | 线段树 扫描线
51nod 1206 Picture 矩形周长求并 | 线段树 扫描线
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define space putchar(' ')
#define enter putchar('\n')
template <class T>
bool read(T &x){
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-') op = 1;
else if(c == EOF) return 0;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
return 1;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar('0' + x % 10);
}
const int N = 100005;
struct Query {
int l, r, h, x;
bool operator < (const Query &b) const{
return h == b.h ? x > b.x : h < b.h;
/*
对于高度相同的修改操作,要让入边在前面,为的是应对这种数据:
2
0 0 1 1
0 1 1 2
*/
}
} Q[2 * N];
int n, lst[2 * N], tot, cnt[8 * N], len[8 * N], sum[8 * N], lsum[8 * N], rsum[8 * N];
ll ans;
/*
变量解释:
lst、tot: 用于离散化
cnt: 记录线段树上一个节点是否已被覆盖
len: 记录线段树上一个节点所代表的区间被覆盖了多长
sum: 记录线段树上一个节点所代表的区间有多少条竖线
lsum、rsum: 分别记录一个区间左右、端点是否有竖线,因为用儿子更新某父亲节点竖线数时,如果左儿子的右端点和右儿子的左端点都有竖线,那么这两条竖线其实是一条竖线。
*/
void pushup(int k, int l, int r){
if(cnt[k]) {
len[k] = lst[r + 1] - lst[l];
lsum[k] = rsum[k] = 1;
sum[k] = 2;
}
else if(l == r) sum[k] = len[k] = lsum[k] = rsum[k] = 0;
else{
len[k] = len[k << 1] + len[k << 1 | 1];
lsum[k] = lsum[k << 1], rsum[k] = rsum[k << 1 | 1];
sum[k] = sum[k << 1] + sum[k << 1 | 1];
if(rsum[k << 1] && lsum[k << 1 | 1]) sum[k] -= 2;
}
}
void change(int k, int l, int r, int ql, int qr, int x){
if(ql <= l && qr >= r){
cnt[k] += x;
pushup(k, l, r);
return;
}
int mid = (l + r) >> 1;
if(ql <= mid) change(k << 1, l, mid, ql, qr, x);
if(qr > mid) change(k << 1 | 1, mid + 1, r, ql, qr, x);
pushup(k, l, r);
}
int getx(int x){
return lower_bound(lst + 1, lst + tot + 1, x) - lst;
//写给自己:注意离散化不要又双叒叕把数组范围打错!不要打成2*n!
}
int main(){
read(n);
for(int i = 1, xa, ya, xb, yb; i <= n; i++){
read(xa), read(ya), read(xb), read(yb);
lst[2 * i - 1] = xa, lst[2 * i] = xb;
Q[2 * i - 1] = (Query){xa, xb, ya, 1};
Q[2 * i] = (Query){xa, xb, yb, -1}; //进行愉快的离散化
}
sort(Q + 1, Q + 2 * n + 1);
sort(lst + 1, lst + 2 * n + 1);
tot = unique(lst + 1, lst + 2 * n + 1) - lst - 1;
for(int i = 1, lastlen = 0; i <= 2 * n; i++){
if(i != 1) ans += (ll) sum[1] * (Q[i].h - Q[i - 1].h);
change(1, 1, tot, getx(Q[i].l), getx(Q[i].r) - 1, Q[i].x);
ans += (ll) abs(lastlen - len[1]), lastlen = len[1];
}
write(ans), enter;
return 0;
}
51nod 1206 Picture 矩形周长求并 | 线段树 扫描线的更多相关文章
- POJ 1151 Atlantis 矩形面积求交/线段树扫描线
Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...
- 51NOD 1962 区间计数 单调栈+二分 / 线段树+扫描线
区间计数 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 80 两个数列 {An} , {Bn} ,请求出Ans, Ans定义如下: Ans:=Σni=1Σnj=i[max{ ...
- HDU 1828“Picture”(线段树+扫描线求矩形周长并)
传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...
- hdu1828 Picture(线段树+扫描线+矩形周长)
看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积)) 解法一·:两次扫描线 如图我们可以 ...
- hdu 1828 Picture(线段树扫描线矩形周长并)
线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...
- [51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树)
[51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树) 题面 给出一棵N个点的树,Q次询问一点编号在区间[l1,r1]内,另一点编号在区间[l2,r2]内的所有点对距离最大值.\ ...
- hdu1542 线段树扫描线求矩形面积的并
题意: 给你n个正方形,求出他们的所占面积有多大,重叠的部分只能算一次. 思路: 自己的第一道线段树扫描线题目,至于扫描线,最近会写一个总结,现在就不直接在这里写了,说下我的方 ...
- hdu 1828 线段树扫描线(周长)
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]
题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...
随机推荐
- Unity扩展编辑器一
将Test1脚本挂在摄像机上 如图展示 下面我们需要在代码中动态的编辑它,请在你的Project视图中创建一个Editor文件夹,把MyEditor放进Editor文件夹中 在OnInsp ...
- Unity3D — —存读档【转载】
详细可参考此篇博文: Unity序列化之XML,JSON--------合成与解析 简单例子(SiKi学院教程): using System.Collections; using System.Col ...
- Aria2 Linux 完整安装及使用教程
Aria2 嘛,主要是用来离线下载,功能强大,支持 http/https 直链.ftp.电驴.磁力链接等等,且可以跨平台使用,配合网页端操作,简直是一代下载神器. 安装 Debian/Ubuntu: ...
- RHEL7 利用双网卡绑定实现VLAN
使用nmcli创建bond配置 #nmcli connection add type bond ifname bond0 con-name bond0 mode active-backup #nmcl ...
- 使用Python一年多了,总结八个好用的Python爬虫技巧
用python也差不多一年多了,python应用最多的场景还是web快速开发.爬虫.自动化运维:写过简单网站.写过自动发帖脚本.写过收发邮件脚本.写过简单验证码识别脚本. 爬虫在开发过程中也有很多复用 ...
- Docker容器和本机之间的文件传输 使用Docker部署Tomcat项目
Docker容器和本机之间的文件传输. http://blog.csdn.net/leafage_m/article/details/72082011 使用Docker部署Tomcat项目 http: ...
- web.xml配置文件中<async-supported>true</async-supported>报错
web.xml配置文件中<async-supported>true</async-supported>报错 http://blog.csdn.net/dream_ll/arti ...
- [buaa-SE-2017]个人作业-week3
个人作业-week3:案例分析 分析产品:Bing词典 Part1:调研&评测 1.软件评测和Bug汇报 这次我选择Bing词典的原因是在于,首先我使用过的词典软件较多,平台涵盖PC端.网站. ...
- 20145214 《网络对抗技术》 Web安全基础实践
20145214 <网络对抗技术> Web安全基础实践 1.实验后回答问题 (1)SQL注入攻击原理,如何防御 SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的 ...
- P4: Programming Protocol-Independent Packet Processors
P4: Programming Protocol-Independent Packet Processors 摘要 P4是一门高级语言,用于编程与协议无关的数据包处理器.P4与SDN控制协议相关联,类 ...