题目链接

题意

给出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. React学习(2)——action,reducer

    action creator 是一个函数,格式如下: var actionCreator = function() { // 构建一个 action 并返回它 return { type: 'AN_A ...

  2. Html 空格与换行

    空格   换行 <br/>   调行距 <div style="line-height:10px"></div>

  3. jquery trim()的用法

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

  4. WPF获取某控件的位置,也就是偏移量

    原文:WPF获取某控件的位置,也就是偏移量 此段示例在MSDN中可见.XAML代码如下: <Window xmlns="http://schemas.microsoft.com/win ...

  5. ASP.NET MVC 学习笔记1 Talk about controller & route

    For the sake of learning programming better, I'd like to increase the frequency of using English. So ...

  6. 【WPF】wpf用MultiBinding解决Converter需要动态传参的问题,以Button为例

    原文:[WPF]wpf用MultiBinding解决Converter需要动态传参的问题,以Button为例       用Binding并通过Converter转换的时候,可能偶尔会遇到传参的问题, ...

  7. WPF属性(一)依赖属性

    原文:WPF属性(一)依赖属性 依赖属性是一种可以自己没有值,并能通过使用Binding从数据源获得值的属性,拥有依赖属性的对象称为依赖对象,在传统开发中,一个对象所占用的内存在调用new操作符进行实 ...

  8. Django 下添加左侧字段显示和搜索

    在对应的apps下建立xadmin.py from .models import EmailVerifyRecord import xadminclass EmailVerifyRecordAdmin ...

  9. Oracle 存储过程创建及调用

    --------创建存储过程------- create or replace procedure TestSPas begin update table_name set CREATE_TIMEST ...

  10. 在UWP 将BitmapImage转换为 WriteableBitmap

    原文: How to convert BitmapImage to WriteableBitmap in Universal application for windows 10? 您可以直接从文件将 ...