1147. Shaping Regions

Time limit: 0.5 second
Memory limit: 64 MB
N opaque rectangles (1 ≤ N ≤ 1000) of various colors are placed on a white sheet of paper whose size is A wide by B long. The rectangles are put with their sides parallel to the sheet's borders. All rectangles fall within the borders of the sheet so that different figures of different colors will be seen.
The coordinate system has its origin (0, 0) at the sheet's lower left corner with axes parallel to the sheet's borders.

Input

The order of the input lines dictates the order of laying down the rectangles. The first input line is a rectangle “on the bottom”. First line contains AB and N, space separated (1 ≤ AB ≤ 10000). Lines 2, …, N + 1 contain five integers each: llxllyurxury, color: the lower left coordinates and upper right coordinates of the rectangle whose color is color (1 ≤ color ≤ 2500) to be placed on the white sheet. The color 1 is the same color of white as the sheet upon which the rectangles are placed.

Output

The output should contain a list of all the colors that can be seen along with the total area of each color that can be seen (even if the regions of color are disjoint), ordered by increasing color. Do not display colors with no area.

Sample

input output
20 20 3
2 2 18 18 2
0 8 19 19 3
8 0 10 19 4
1 91
2 84
3 187
4 38
 
Difficulty: 833
 
题意:有一张n*m的白纸,一开始颜色为1。然后有k张各种颜色的纸放在这张纸上,问最后每种颜色的数量。
分析:
显然又是经典题。
 
一种做法是从后往前做,用并查集维护每行是否被覆盖,总的复杂度是O(n*m),当然这里的是离散化之后的n和m。
只是要注意行和列分开离散化,否则很容易MLE
 
另一种做法是暴力使用切割法,几乎相当于暴力计算对于每块纸会被其他纸遮住多少。
 
我使用了后者,因为离散化太麻烦。
 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = , M = ;
struct Rectangle
{
int lx, rx, uy, dy, color;
inline void Read()
{
scanf("%d%d%d%d%d", &lx, &dy, &rx, &uy, &color);
}
} arr[N];
int width, height, n;
int ans[M]; inline void Input()
{
scanf("%d%d%d", &width, &height, &n);
for(int i = ; i <= n; i++)
arr[i].Read();
} inline int Work(int lx, int dy, int rx, int uy, int index)
{
if(lx >= rx || dy >= uy) return ;
while(index <= n && (
lx >= arr[index].rx ||
rx <= arr[index].lx ||
dy >= arr[index].uy ||
uy <= arr[index].dy)) index++;
if(index > n) return (rx - lx) * (uy - dy);
int ret = ;
ret += Work(lx, dy, min(rx, arr[index].lx), uy, index + );
lx = max(lx, min(rx, arr[index].lx)); ret += Work(max(lx, arr[index].rx), dy, rx, uy, index + );
rx = min(rx, max(lx, arr[index].rx)); ret += Work(lx, dy, rx, min(uy, arr[index].dy), index + );
dy = min(dy, max(uy, arr[index].dy)); ret += Work(lx, max(dy, arr[index].uy), rx, uy, index + );
uy = min(uy, max(dy, arr[index].uy)); return ret;
} inline void Solve()
{
ans[] = width * height;
for(int i = n; i >= ; i--)
{
int area = Work(arr[i].lx,
arr[i].dy,
arr[i].rx,
arr[i].uy,
i + );
ans[arr[i].color] += area;
ans[] -= area;
} for(int i = ; i < M; i++)
if(ans[i]) printf("%d %d\n", i, ans[i]);
} int main()
{
Input();
Solve();
return ;
}

ural 1147. Shaping Regions的更多相关文章

  1. ural1147 Shaping Regions

    Shaping Regions Time limit: 0.5 secondMemory limit: 64 MB N opaque rectangles (1 ≤ N ≤ 1000) of vari ...

  2. Shaping Regions(dfs)

    Shaping Regions Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 124  Solved: 39[Submit][Status][Web B ...

  3. USACO 6.2 Shaping Regions

    Shaping Regions N opaque rectangles (1 <= N <= 1000) of various colors are placed on a white s ...

  4. OI暑假集训游记

    莞中OI集训游记 Written BY Jum Leon. I        又是一载夏,本蒟蒻以特长生考入莞中,怀着忐忑的心情到了8月,是集训之际.怀着对算法学习的向往心情被大佬暴虐的一丝恐惧来到了 ...

  5. USACO 完结的一些感想

    其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...

  6. [LeetCode] Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  7. 验证LeetCode Surrounded Regions 包围区域的DFS方法

    在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...

  8. Leetcode: Surrounded regions

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  9. LEETCODE —— Surrounded Regions

    Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' a ...

随机推荐

  1. Lattice 的 DDR IP核使用调试笔记之工程建立

    DDR3的IP核的使用相当重要,尤其是对视频处理方面. 下面接收DDR3 的IP 核的生成步骤. 1. 选择DDR IP核的生成路径.名字以及哪种语言之后就可以设置DDR IP 的参数了. 2.选择存 ...

  2. 磁盘空间占满inode结点没用完 并删除了文件但是释放不了

    lsof  |grep delete lsof(list system open file )可显示系统打开的文件,以root身份运行. 很多时候文件正在被占用,即使删除了,也无法释放空间,只有停 了 ...

  3. 关于plsql连接oracle数据库session失效时间设置

    http://bbs.csdn.net/topics/350152441 http://www.linuxidc.com/Linux/2015-09/123286.htm

  4. 【转】JQuery插件ajaxFileUpload 异步上传文件(PHP版)

    前几天想在手机端做个异步上传图片的功能,平时用的比较多的JQuery图片上传插件是Uploadify这个插件,效果很不错,但是由于手机不支持flash,所以不得不再找一个文件上传插件来用了.后来发现a ...

  5. 在ubuntu上搭建开发环境10---英文版ubuntu安装中文输入法

    之前安装 ubuntu时候选择安装英文版,但是在查资料的时候难免的要输入中文所以自己弄了一下中文输入法的安装 我安装的是fcitx小企鹅输入法 下面介绍一下安装的过程.....   ubuntu默认的 ...

  6. 10g 11g配置Logical Standby

    1.创建一个物理Standby数据库 详细见11g Physical Standby配置 2.Standby数据库取消managed  recovery ALTER DATABASE RECOVER ...

  7. shell test 數值 字符串 文件比較

    數值比較 描述 n1 –eq n2 等於 n1 –gt  n2 大於 n1 –ge n2 大於等於 n1 –lt  n2 小於 n1 –le n2 小於等於 n1 –ne n2 不等於   字符串比較 ...

  8. WPF程序最小化到任务通知栏

    我们通常使用的桌面软件,都可以最小化到任务通知栏,并且可以从任务通知栏再打开当前软件,或者通过软件的快捷方式从任务通知栏呼出. 我们可以通过下面的方式把WPF程序最小化到任务栏.由于WPF并没有实现N ...

  9. Ubuntu 14.04下翻译软件的安装与比较

    转载:sixipiaoyang.blog.163.com/blog/static/6232358820144146386437/

  10. python 类型判断-- isinstance函数

    判断类型 函数isinstance()可以判断一个变量的类型,既可以用在Python内置的数据类型如str.list.dict,也可以用在我们自定义的类,它们本质上都是数据类型. 假设有如下的 Per ...