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. NYOJ题目842整除的尾数

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsUAAAIMCAIAAACSTkYzAAAgAElEQVR4nO3dO3KjzBrG8bMJ5VqIYx ...

  2. java 缩略图

    http://www.cnblogs.com/digdeep/p/4829471.html http://www.jb51.net/article/57648.htm http://blog.csdn ...

  3. 20.策略者模式(Stragety Pattern)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. 在asp.net利用jquery.MultiFile实现多文件上传(转载)

    转载地址:http://www.cnblogs.com/scy251147/archive/2010/09/30/1839313.html 官网链接:http://www.fyneworks.com/ ...

  5. AOJ789 买酒

    买酒 Time Limit: 1000 ms   Case Time Limit: 1000 ms   Memory Limit: 64 MBTotal Submission: 70   Submis ...

  6. git revert 和 git reset的区别

    git revert 撤销 某次操作,此次操作之前和之后的commit和history都会保留,并且把这次撤销 作为一次最新的提交    * git revert HEAD               ...

  7. 攻城狮在路上(肆)How tomcat works(零) 前言说明

    最近几篇是关于How tomcat works一书的读书笔记. 通过数个章节逐渐实现一个tomcat的功能. 源码下载地址:http://zhidao.baidu.com/share/7007af0f ...

  8. windows下Tomcat配置多实例

    详情参见tomcat安装目录下RUNNING.txt中Advanced Configuration - Multiple Tomcat Instances部分. 问题源于下面这段tomcat官方文档的 ...

  9. JavaScript - 引用类型

    对象在JavaScript中被称为引用类型的值. 引用类型和传统面向对象设计中的类相似,但实现不同. Object是一个基础类型,其他所有类型都从Object继承了基本的行为. Array类型,Dat ...

  10. vector的主要操作

    vector常用方法 assign() 对Vector中的元素赋值 void assign( input_iterator start, input_iterator end ); // void a ...