题目链接

题意

给出n个矩形,求周长并。

思路

学了区间并,比较容易想到周长并。

我是对x方向和y方向分别做两次扫描线。应该记录一个pre变量,记录上一次扫描的时候的长度,对于每次遇到扫描线统计答案的时候,使用当前的 tree[1] 去与 pre 做相减,因为这一次如果边长增加了或者减少了,那么一定和之前的值有差值,其中的差值就是这次的变化量。

但是这份代码交G++会WA,C++就A了。而且排序的时候如果权值相同,应该让状态为1(开始)的扫描线排在状态为-1(结束)的扫描线前面,因为重合的话,那个边长不可计算到答案里面的。如果先更新结束的,代表这一段边长(如果更新之后消失了的话)会导致算上这一部分的贡献,然后又更新开始的,又算上一次这一部分的贡献,等于算上了两次。然而真实情况下,是不应该算上这部分的贡献的。

这题不用离散化也可以,习惯性。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2e4 + 10;
#define lson l, m, rt<<1
#define rson m + 1, r, rt<<1|1
typedef long long LL;
struct Node {
int l, r, w, st;
Node () {}
Node (int _l, int _r, int _w, int _st) {
l = _l, r = _r, w = _w, st = _st;
}
bool operator < (const Node &rhs) const {
if(w == rhs.w) return st > rhs.st; // 开始在结束前面
return w < rhs.w;
}
} px[N], py[N];
int tree[N<<2], cnt[N<<2];
int x[N], y[N]; void pushup(int l, int r, int rt, int *axis) {
if(cnt[rt] > 0) tree[rt] = axis[r+1] - axis[l];
else if(l == r) tree[rt] = 0;
else tree[rt] = tree[rt<<1] + tree[rt<<1|1];
} void update(int L, int R, int w, int l, int r, int rt, int *axis) {
if(L <= l && r <= R) {
cnt[rt] += w;
pushup(l, r, rt, axis);
return ;
}
int m = (l + r) >> 1;
if(L <= m) update(L, R, w, lson, axis);
if(m < R) update(L, R, w, rson, axis);
pushup(l, r, rt, axis);
} int solve(int n, int *axis, Node *p, int ct) {
memset(tree, 0, sizeof(tree));
memset(cnt, 0, sizeof(cnt));
int ans = 0, pre = 0;
for(int i = 1; i <= n; i++) {
int L = lower_bound(axis + 1, axis + 1 + ct, p[i].l) - axis;
int R = lower_bound(axis + 1, axis + 1 + ct, p[i].r) - axis - 1;
update(L, R, p[i].st, 1, ct, 1, axis);
ans += abs(tree[1] - pre);
pre = tree[1];
}
return ans;
} int main() {
int n;
while(~scanf("%d", &n)) {
int cntx = 0, cnty = 0;
for(int i = 1; i <= n; i++) {
int x1, x2, y1, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
x[++cntx] = x1; x[++cntx] = x2;
y[++cnty] = y1; y[++cnty] = y2;
px[i*2-1] = Node(y1, y2, x1, 1);
px[i*2] = Node(y1, y2, x2, -1);
py[i*2-1] = Node(x1, x2, y1, 1);
py[i*2] = Node(x1, x2, y2, -1);
}
sort(x + 1, x + 1 + cntx);
sort(y + 1, y + 1 + cnty);
cntx = unique(x + 1, x + 1 + cntx) - x - 1;
cnty = unique(y + 1, y + 1 + cnty) - y - 1;
int m = 2 * n;
sort(px + 1, px + 1 + m);
sort(py + 1, py + 1 + m);
int ans = solve(m, x, py, cntx) + solve(m, y, px, cnty);
printf("%d\n", ans);
} return 0;
} /*
7
-15 0 5 10
-5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
30 10 36 20
34 0 40 16
*/

HDU 1828:Picture(扫描线+线段树 矩形周长并)的更多相关文章

  1. HDU 1828“Picture”(线段树+扫描线求矩形周长并)

    传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...

  2. hdu 1828 Picture(线段树扫描线矩形周长并)

    线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...

  3. HDU 1828 Picture(线段树扫描线求周长)

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  4. hdu 1828 Picture(线段树,扫描线)

    A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wa ...

  5. luogu P1856 [USACO5.5]矩形周长Picture 扫描线 + 线段树

    Code: #include<bits/stdc++.h> #define maxn 200007 #define inf 100005 using namespace std; void ...

  6. HDU 1542:Atlantis(扫描线+线段树 矩形面积并)***

    题目链接 题意 给出n个矩形,求面积并. 思路 使用扫描线,我这里离散化y轴,按照x坐标从左往右扫过去.离散化后的y轴可以用线段树维护整个y上面的线段总长度,当碰到扫描线的时候,就可以统计面积.这里要 ...

  7. hdu1828(线段树——矩形周长并)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 分析:与面积不同的地方是还要记录竖的边有几个(num记录),并且当边界重合的时候需要合并(用lb ...

  8. 51nod 1206 && hdu 1828 Picture (扫描线+离散化+线段树 矩阵周长并)

    1206 Picture  题目来源: IOI 1998 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 给出平面上的N个矩形(矩形的边平行于X轴 ...

  9. poj 1151 Atlantis (离散化 + 扫描线 + 线段树 矩形面积并)

    题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 映射到y轴,并且记录下每个的y坐标,并对y坐标进行离散. 然后按照x从左向右扫描. #include <io ...

随机推荐

  1. QProcess::startDetached(5.10有了一种新的方式)

    From Qt 5.10 on, there is a new way how to start detached processes with QProcess. Of course you kno ...

  2. WPF 寻找控件模板中的元素

    <Window x:Class="Wpf180706.Window10"        xmlns="http://schemas.microsoft.com/wi ...

  3. jquery表单过滤器

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  4. 原 BinaryWriter和BinaryReader(二进制文件的读写)

    原文 BinaryWriter和BinaryReader(二进制文件的读写) C#的FileStream类提供了最原始的字节级上的文件读写功能,但我们习惯于对字符串操作,于是StreamWriter和 ...

  5. [shell]流程控制----case语句

    Shell case语句为多选择语句.可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令.case语句格式如下: case 值 in 模式1) command1 command2 ...

  6. vfp9写的爬虫前段,基于webbrowser

    *基于xmlhttp不能正确获取js动态加载的数据 CLEAR ALL CLEAR PUBLIC zform zform = CREATEOBJECT([myform])zform.go(" ...

  7. eclipse 插件编写(四)

    前言 前面几篇文章讲了下如果编写简单的eclipse插件,如创建插件项目.编写右键弹出菜单等功能,接下来主要写一下如何生成代码的功能,这一片的功能跟插件本身的编写关联不太大,主要处理插件之后的业务内容 ...

  8. File handling in Delphi Object Pascal(处理record类型)

    With new users purchasing Delphi every single day, it’s not uncommon for me to meet users that are n ...

  9. 解决Delphi的Indy10组件包TIdHttpServer控件解码HTTP请求时参数乱码的问题

    Delphi6下使用Indy10组件包,其中的TIdHttpServer控件在处理HTTP请求的时候 不能正确解码含有汉字的参数,如: http://127.0.0.1/test?cmd=open&a ...

  10. c# 获取cook

    using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServi ...