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代表该区 ...
随机推荐
- opengl绘制三角形
顶点数组对象:Vertex Array Object,VAO 顶点缓冲对象:Vertex Buffer Object,VBO 索引缓冲对象:Element Buffer Object,EBO或Inde ...
- 报错android.view.InflateException: Binary XML file line #11: Attempt to invoke virtual method 'boolean
出现这种问题,打开Android monitor的调试信息发现是 android.view.InflateException: Binary XML file line #11: Attempt to ...
- centos6.9+lnmp1.5环境部署swoole记录
hiredis下载地址:https://github.com/redis/hiredis/releasesunzip hiredis-v0.13.3.zipmake -jsudo make insta ...
- Python3中的函数 大全
Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.Python提供了许多内建函数,比如print().但也可以自己创建 ...
- zookeeper客户端相关命令
windows环境: 本机 直接 点机zkcli.cmd linux环境: 连接到zookeeper server ./zkCli.sh -server localhost:2181 help命 ...
- 慢吞吞的pip切换源
http://blog.csdn.net/gz_liuyun/article/details/52778198
- 基于spec评论作品
组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶 刘佳瑞 公冶令鑫 杨磊 杨金铭 张宇 卢帝同 一.测试目标:拉格朗日2018——飞词 下面是他们的小游戏在运行时的一些截图画面: 1.开始: ...
- Scrum Meeting 报告
Scrum Meeting 报告 ----团队项目所需时间估计以及任务分配 由于能力有限,我们还不能构架好一个大框架.但是初步可以完成任务的流程和分配.任务所需要的具体实现可以参看<学霸系统的N ...
- web07-jdbcBookStore
新建web项目,名字 新建servlet,名字CreateDBServlet 内容为: ---- 配置web.xml 数据库的URL.driveclass.user.passWord都写在web.xm ...
- Leetcode题库——35.搜索插入位置
@author: ZZQ @software: PyCharm @file: searchInsert.py @time: 2018/11/07 19:20 要求:给定一个排序数组和一个目标值,在数组 ...