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. T4模板

    T4,即4个T开头的英文字母组合:Text Template Transformation Toolkit. T4文本模板,即一种自定义规则的代码生成器.根据业务模型可生成任何形式的文本文件或供程序调 ...

  2. iOS 简单提示view

    +(void)showMessage:(NSString *)message{    UIWindow * window = [UIApplication sharedApplication].key ...

  3. sql语句的join用法

    sql的join分为三种,内连接.外连接.交叉连接. 以下先建2张表,插入一些数据,后续理解起来更方便一些. create table emp(empno int, name char(20),dep ...

  4. Java 内存区域和GC机制

    目录 Java垃圾回收概况 Java内存区域 Java对象的访问方式 Java内存分配机制 Java GC机制 垃圾收集器 Java垃圾回收概况 Java GC(Garbage Collection, ...

  5. HTML5+CSS3的响应式网页设计:自动适应屏幕宽度

    这几天都在修改博客上面的样式.本来用的是d83.0的模板.自己又修改了许多地方,其中自己修改的一些地方在手机里面显示的效果不是很理想,于是想改成自适应的效果.对CSS3不是特别的熟练,只能去网上找找案 ...

  6. 算法系列:XXX

    转载自http://www.cnblogs.com/skynet/p/3372855.html 这次分享的宗旨是——让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择 ...

  7. 二、activity与Intent

    (一) 多个activity之间的跳转(无值传递) 第一步:创建activity(其实就是jave文件),并进行注册 在AndroidManifest.xml中 <activity androi ...

  8. DateTime还是DateTimeOffset?Now还是UtcNow?

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:新年第一篇文章,就来谈谈关于时间的简单技术问题:该用DateTime还是DateTim ...

  9. web.xml中同一servlet/filter配置多个url-pattern

    转自:http://blog.sina.com.cn/s/blog_4c2c2a0c0100dh67.html 若你的servlet要多个地址,或你的filter需要过滤不同的url如有*.jsp,* ...

  10. EditPlus+VisualStudio配置VC简易开发环境环境

         对于C++开发, 我想在Windows下大家用的最多的应该是MS的VC++.但其强大的功能背后却有着"启动速度慢","占用资源多"的缺点,尤其是VS后 ...