Picture poj1177

Picture

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

Description

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.

Input

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.

Output

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.

Sample Input

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

Sample Output

228

Source

IOI 1998

这题恶心,不想写题解

解释都在代码里了,自己看代码吧

#include <stdio.h>
#include <algorithm>
#define LEN 10000
using namespace std;

struct Node
{
  int left;
  int right;
  int count;//被覆盖次数
  int line;//所包含的区间数量
  int lbd;//左端点是否被覆盖
  int rbd;//右端点是否被覆盖
  int m;//测度,即覆盖的区间长度,如[2,8]就为6
};

struct ScanLine
{
  int x;//横坐标
  int y1;//扫描线的下端点
  int y2;//扫描线的上端点
  int flag;//若该扫描线属于矩形的左边的竖边,
  //如AB,则叫做入边,值为1,若属于矩形的右边的竖边,如CD,则叫做出边,值为0
};

struct Node node[LEN*4];
struct ScanLine scan[LEN];
int y[LEN];

void build(int l, int r, int i)
{
  node[i].left = l;
  node[i].right = r;
  node[i].count = 0;
  node[i].m = 0;
  node[i].line = 0;
  if (r - l > 1)
  {
    int middle = (l + r)/2;
    build(l, middle, 2*i + 1);
    build(middle, r, 2*i + 2);
  }
}

//更新测度m
void update_m(int i)
{
  if (node[i].count > 0)
    node[i].m = y[node[i].right] - y[node[i].left];
  else if (node[i].right - node[i].left == 1)
    node[i].m = 0;
  else
  {
    node[i].m = node[2*i + 1].m + node[2*i + 2].m;
  }
}

//更新line
void update_line(int i)
{
  if (node[i].count > 0)
  {
    node[i].lbd = 1;
    node[i].rbd = 1;
    node[i].line = 1;
  }
  else if (node[i].right - node[i].left == 1)
  {
    node[i].lbd = 0;
    node[i].rbd = 0;
    node[i].line = 0;
  }
  else
  {
    node[i].lbd = node[2*i + 1].lbd;
    node[i].rbd = node[2*i + 2].rbd;
    node[i].line = node[2*i + 1].line + node[2*i + 2].line
    - node[2*i + 1].rbd*node[2*i + 2].lbd;
  }
}

void insert(int l, int r, int i)//l和r分别是这条扫描线的下端点和上端点的纵坐标
//相当于区间修改
{
  //在这里要取离散化之前的原值进行比较
  if (y[node[i].left] >= l && y[node[i].right] <= r)
  //如果这个区间内最下的点的纵坐标大于insert扫描线的上端点的纵坐标
  //且这个区间内最上的点的纵坐标小于insert扫描线的下端点的纵坐标
    (node[i].count)++;//这个区间包含于这条线段,一定覆盖了这个区间内的扫描线
  else if (node[i].right - node[i].left == 1)return;//因为坐标都是整数
  else
  {
    int middle = (node[i].left + node[i].right)/2;
    if (r <= y[middle])//这条扫描线完全包含于这个区间的中点到最下
      insert(l, r, 2*i + 1);
    else if (l >= y[middle])//完全包含于这个区间的最上到中点
      insert(l, r, 2*i + 2);
    else//穿过中点
    {
      insert(l, y[middle], 2*i + 1);
      insert(y[middle], r, 2*i + 2);
    }
  }
  update_m(i);
  update_line(i);
}

void remove(int l, int r, int i)
{
  //在这里要取离散化之前的原值进行比较
  if (y[node[i].left] >= l && y[node[i].right] <= r)
    (node[i].count)--;//完全被包含就删去这条边
  else if (node[i].right - node[i].left == 1)
    return;
  else
  {
    int middle = (node[i].left + node[i].right)/2;
    if (r <= y[middle])
      remove(l, r, 2*i + 1);
    else if (l >= y[middle])
      remove(l, r, 2*i + 2);
    else
    {
      remove(l, y[middle], 2*i + 1);
      remove(y[middle], r, 2*i + 2);
    }
  }
  update_m(i);
  update_line(i);
}

bool cmp(struct ScanLine line1, struct ScanLine line2)
{
  if (line1.x == line2.x)
    return line1.flag > line2.flag;
  return (line1.x < line2.x);
}

int main()
{
  int n;
  scanf("%d", &n);//输入有几个矩形
  int x1, y1, x2, y2;
  int i = 0;
  while (n--)
  {
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);//(x1,y1)左下,(x2,y2)右上
    scan[i].x = x1;//加扫描线
    scan[i].y1 = y1;
    scan[i].y2 = y2;
    scan[i].flag = 1;
    y[i++] = y1;//y[]记录所有点的纵坐标
    scan[i].x = x2;
    scan[i].y1 = y1;
    scan[i].y2 = y2;
    scan[i].flag = 0;
    y[i++] = y2;
  }
  sort(y, y + i);//所有纵坐标从下到上排序
  sort(scan, scan + i, cmp);//所有扫描线从左到右排序
  int unique_count = unique(y, y + i) - y;//y数组中不重复的个数
  build(0, unique_count - 1, 0);//离散化,建立线段树

  int perimeter = 0;
  int now_m = 0;
  int now_line = 0;

  for (int j = 0; j < i; j++)//枚举每条扫描线
  {
    if (scan[j].flag)//如果是左边
      insert(scan[j].y1, scan[j].y2, 0);
    else//如果是右边
      remove(scan[j].y1, scan[j].y2, 0);
    if (j >= 1)
      perimeter += 2*now_line*(scan[j].x - scan[j-1].x);
    perimeter += abs(node[0].m - now_m);
    now_m = node[0].m;
    now_line = node[0].line;
  }

  printf("%d\n", perimeter);
  return 0;
}
时间: 2024-10-10 02:12:06

Picture poj1177的相关文章

【poj1177】 Picture

http://poj.org/problem?id=1177 (题目链接) 题意 求矩形周长并. Solution 转自:http://www.cnblogs.com/Booble/archive/2010/10/10/1847163.html 先看图: 为了解决这个问题 我们先把一坨一坨的矩形 进行矩形切割: 我们考虑周长由哪些部分构成 其中,红线是需要统计入周长的竖边,绿线是需要统计入周长的横边 我们称两条蓝线之间的部分为统计区间 我们需要依次统计从左到右的统计区间内的需要计数的矩形边,累加

poj-1177 Picture(矩形周长并,线段树+扫描线)

题目链接:点击打开链接 Picture Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 11706   Accepted: 6175 Description A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical o

【HDOJ1828&amp;&amp;POJ1177】Picture(线段树,扫描线)

题意:给定n个矩形,求他们的并的周长 n<=5e3,abs(x[i])<=1e4 思路:From https://www.cnblogs.com/kuangbin/archive/2013/04/10/3013437.html 真实"线段"树 1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<ios

[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 t

hdu1828 (Picture) &amp;poj 1177( Picture)&amp;sdibt 2390(5.5.1 Picture 矩形周长)(线段树+扫描)

题目地址:hdu1828 (Picture)  & poj 1177( Picture) & sdibt 2390(5.5.1 Picture 矩形周长) 题目大意: 给你N个矩形,求出N个矩形构成的周长. 解题思路: 线段数+扫描线. 推荐博客: POJ1177 Picture(线段树求轮廓周长) POJ 1177 Picture (线段树+离散化+扫描线) 详解 注意事项: 该题在求面积的基础上有了升级,多写了一个被调需要求出投影与Y轴有几段,不然样例会少算20,就是给出图形中间的小矩

HDU 1828 Picture(矩形周长并)

HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4个方向都加完就是答案 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 5005; int n; struct Rec { int

usaco Picture

/* ID: modengd1 PROG: picture LANG: C++ */ #include <iostream> #include <stdio.h> #include <memory.h> #include <vector> #include <algorithm> using namespace std; int N; struct edge { int y,x1,x2; bool isbegin; bool friend ope

Hole puncher Show Picture

import sys reload(sys) sys.setdefaultencoding('utf8') import matplotlib.pyplot as plt import string file = open("GD.txt") i = 0 x = range(0) y = range(0) for line in file: x.append(string.atoi(line[0:7], 10)) y.append(string.atoi(line[8:], 10))

杭电1162--Eddy&#39;s picture(Prim()算法)

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8070    Accepted Submission(s): 4084 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to b