ural 1147. Shaping Regions

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 ;
}
上一篇:sql逻辑查询 理论知识


下一篇:MySQL 通配符学习小结