[POJ1177]Picture

试题描述

A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.

Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.

The corresponding boundary is the whole set of line segments drawn in Figure 2.

The vertices of all rectangles have integer coordinates.

输入

Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.

0 <= number of rectangles < 5000 
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.

输出

Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.

输入示例

-
-
-
-

输出示例


数据规模及约定

见“输入

题解

可以发现水平方向的边与竖直方向的边是独立的,那么我们可以分别做两次扫描线。

不难发现扫描线运行时要进行区间修改操作,所以会想到线段树。会发现单纯地打懒标记像一般的题那样做是不行的,这里引入一个新的维护方法,在这里,我们不用标记下传;每次区间的修加就是对对应区间的节点打上一个标记,区间删就是对对应区间的节点回收那个标记,标记是静态的,不能下传。

显然对答案的贡献一定是在某个矩形的上下边界时产生的,也就是说只用在扫描线扫到需要加或需要删的操作时才累计答案。对于加操作,我们计加之后与加之前相差为 t,那么把答案累加 t;删操作同理。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 20010
#define maxr 5010
int n;
struct Rec {
int x1, y1, x2, y2;
Rec() {}
Rec(int _1, int _2, int _3, int _4): x1(_1), y1(_2), x2(_3), y2(_4) {}
} rs[maxr];
struct Line {
int l, r, x;
Line() {}
Line(int _1, int _2, int _3): l(_1), r(_2), x(_3) {}
bool operator < (const Line& t) const { return x < t.x; }
} ad[maxr], mi[maxr];
int ca, cm, ans; int cntv[maxn<<2], sumv[maxn<<2];
void update(int L, int R, int o, int ql, int qr, int v) {
int M = L + R >> 1, lc = o << 1, rc = lc | 1;
if(ql <= L && R <= qr) {
cntv[o] += v;
if(cntv[o]) sumv[o] = R - L + 1;
else if(L == R) sumv[o] = 0;
else sumv[o] = sumv[lc] + sumv[rc];
return ;
}
if(ql <= M) update(L, M, lc, ql, qr, v);
if(qr > M) update(M+1, R, rc, ql, qr, v);
sumv[o] = cntv[o] ? R - L + 1 : sumv[lc] + sumv[rc];
return ;
} void solve() {
sort(ad + 1, ad + ca + 1);
sort(mi + 1, mi + ca + 1);
memset(sumv, 0, sizeof(sumv));
memset(cntv, 0, sizeof(cntv));
int ka = 1, km = 1;
for(int i = 1; i <= 20001; i++) {
while(ka <= ca && ad[ka].x == i) {
int tmp = sumv[1];
update(1, 20001, 1, ad[ka].l, ad[ka].r - 1, 1);
ans += sumv[1] - tmp;
// printf("add[%d, %d]: %d <- %d\n", ad[ka].l, ad[ka].r - 1, sumv[1], tmp);
ka++;
}
while(km <= cm && mi[km].x == i) {
int tmp = sumv[1];
update(1, 20001, 1, mi[km].l, mi[km].r - 1, -1);
ans += tmp - sumv[1];
// printf("del[%d, %d]: %d <- %d\n", mi[km].l, mi[km].r - 1, sumv[1], tmp);
km++;
}
}
return ;
} int main() {
n = read();
for(int i = 1; i <= n; i++) {
int x1 = read() + 10001, y1 = read() + 10001, x2 = read() + 10001, y2 = read() + 10001;
rs[i] = Rec(x1, y1, x2, y2);
ad[++ca] = Line(y1, y2, x1);
mi[++cm] = Line(y1, y2, x2);
} solve();
ca = cm = 0;
for(int i = 1; i <= n; i++) {
int x1 = rs[i].x1, x2 = rs[i].x2, y1 = rs[i].y1, y2 = rs[i].y2;
ad[++ca] = Line(x1, x2, y1);
mi[++cm] = Line(x1, x2, y2);
}
solve(); printf("%d\n", ans); return 0;
}

[POJ1177]Picture的更多相关文章

  1. poj1177 Picture 矩形周长并

    地址:http://poj.org/problem?id=1177 题目: Picture Time Limit: 2000MS   Memory Limit: 10000K Total Submis ...

  2. POJ1177 Picture —— 求矩形并的周长 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/POJ-1177 A number of rectangular posters, photographs and other pict ...

  3. CQOI2005 三角形面积并 和 POJ1177 Picture

    1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 1664  Solved: 443[Submit][Stat ...

  4. POJ1177 Picture 线段树+离散化+扫描线

    求最终的覆盖图形周长,写这种代码应该短而精确,差的比较远 /* Problem: 1177 User: 96655 Memory: 348K Time: 32MS Language: C++ Resu ...

  5. POJ-1177 Picture 矩形覆盖周长并

    题目链接:http://poj.org/problem?id=1177 比矩形面积并麻烦点,需要更新竖边的条数(平行于x轴扫描)..求横边的时候,保存上一个结果,加上当前长度与上一个结果差的绝对值就行 ...

  6. IOI1998 hdu1828 poj1177 Picture

    写了一发扫描线竟然狂WA不止,hdu死活过不了,poj和当时IOI的数据(还花了我1dsdn积分..)都过了. 然后看到谋篇blog里有评论,把数据拿下来发现WA了. 数据是 20 0 1 11 0 ...

  7. 【poj1177】 Picture

    http://poj.org/problem?id=1177 (题目链接) 题意 求矩形周长并. Solution 转自:http://www.cnblogs.com/Booble/archive/2 ...

  8. 【HDOJ1828&&POJ1177】Picture(线段树,扫描线)

    题意:给定n个矩形,求他们的并的周长 n<=5e3,abs(x[i])<=1e4 思路:From https://www.cnblogs.com/kuangbin/archive/2013 ...

  9. Picture poj1177

    Picture Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 12265   Accepted: 6484 Descript ...

随机推荐

  1. 创建 sequence

    -- Create sequence create sequence XRMKSD_DET_SEQminvalue 1maxvalue 999999999999999999999999999start ...

  2. Runner站立会议02

    开会时间:21.10~21.30 地点:二教103 今天做了什么:学习五大布局的使用方法 明天准备做什么:学习数据的存储 遇到的困难:知识点太多,信心受挫 站立会议图: 燃尽图:

  3. JavaScript学习笔记——数组

    javascript数组数组是一个可以存储 一组 或是 一系列 相关数据 的 容器. 一.为什么要使用数组. (1)为了解决大量相关数据的存储和使用的问题. (2)模拟真是的世界. 二.如何创建数组 ...

  4. assert()函数用法总结

    assert()函数用法总结 assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: #include <assert.h> ...

  5. UITextView限制输入文字

    一.设置UITextView的delegate为控制器 二.实现代理方法 #pragma mark - UITextViewDelegate - (BOOL)textView:(UITextView ...

  6. SCI答复审稿人的策略和答复信的写作技巧

    SCI论文被录用的最后一步 –---答复审稿人的策略和答复信的写作技巧 [好文转载] : 一篇稿子从酝酿到成型历经艰辛,投出去之后又是漫长的等待,好容易收到编辑的回信,得到的往往又是审稿人不留情面的一 ...

  7. python 多线程学习

    多线程(multithreaded,MT),是指从软件或者硬件上实现多个线程并发执行的技术 什么是进程? 计算机程序只不过是磁盘中可执行的二进制(或其他类型)的数据.它们只有在被读取到内存中,被操作系 ...

  8. JS获取当前对象大小以及屏幕分辨率等...

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <meta nam ...

  9. monit 监控并自动重启服务

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://coolerfeng.blog.51cto.com/133059/50126 Mo ...

  10. XMAL语法系列之-(2)---WPF控件继承图

    WPF控件继承图 1 FrameworkElement 1.1 Panel(面板类元素) 1.1.1 Canvas 1.1.2 DockPanel 1.1.3 Grid 1.1.4 TabPanel ...