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

分析:经典的覆盖问题,参考http://blog.csdn.net/skyprophet/article/details/4514926,冰块上浮法;

   倒序计算,在计算到当前矩形时,看在他上面的矩形有没有重叠的,有就去掉那个部分,一直递归下去即可;

代码:  

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=3e3+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,ans[maxn];
struct node
{
int x1,x2,y1,y2,c;
node(){}
node(int _x1,int _x2,int _y1,int _y2,int _c)
{
x1=_x1,x2=_x2,y1=_y1,y2=_y2,c=_c;
}
}op[maxn];
int get(node p,int nt)
{
int ans=;
while(nt<=k&&((p.x1>=op[nt].x2)||(p.x2<=op[nt].x1)||(p.y1>=op[nt].y2)||(p.y2<=op[nt].y1)))
nt++;
if(nt>k)return (p.x2-p.x1)*(p.y2-p.y1);
if(p.x1<op[nt].x1)
ans+=get(node(p.x1,op[nt].x1,p.y1,p.y2,op[nt].c),nt+),p.x1=op[nt].x1;
if(p.x2>op[nt].x2)
ans+=get(node(op[nt].x2,p.x2,p.y1,p.y2,op[nt].c),nt+),p.x2=op[nt].x2;
if(p.y1<op[nt].y1)
ans+=get(node(p.x1,p.x2,p.y1,op[nt].y1,op[nt].c),nt+),p.y1=op[nt].y1;
if(p.y2>op[nt].y2)
ans+=get(node(p.x1,p.x2,op[nt].y2,p.y2,op[nt].c),nt+),p.y2=op[nt].y2;
return ans;
}
int main()
{
int i,j;
scanf("%d%d%d",&n,&m,&k);
ans[]=n*m;
rep(i,,k)scanf("%d%d%d%d%d",&op[i].x1,&op[i].y1,&op[i].x2,&op[i].y2,&op[i].c);
for(i=k;i>=;i--)
{
ans[op[i].c]+=(j=get(op[i],i+));
ans[]-=j;
}
rep(i,,)if(ans[i])printf("%d %d\n",i,ans[i]);
//system("Pause");
return ;
}

ural1147 Shaping Regions的更多相关文章

  1. ural 1147. Shaping Regions

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

  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. 如何对Site Settings页面进行定制化 添加一个setting 链接

    下面在Site Settings页 >Site Administration里添加一个Ruby Setting 超链接,点击进入到rubySetting.aspx 1.在SharePoint p ...

  2. VC MFC工具栏(CToolBar)控件

    一.工具栏 工具栏控件在控件面板里没有对应的选项(图标),但有一个工具栏控件类CToolBar,所以我们如果要创建一个工具栏控件并显示在窗口里的话,只能用代码来完成,事实上任何一种控件,都可以用代码创 ...

  3. iOS之图文混排

    首先从github下载 https://github.com/wezm/RegexKitLite 导入.h和.m文件后为RegexKitLite.m添加编译标记-fno-objc-arc.添加动态库l ...

  4. poj 2533 Longest Ordered Subsequence(LIS)

    Description A numeric sequence of ai is ordered ifa1 <a2 < ... < aN. Let the subsequence of ...

  5. Java良葛格 学习笔记

    学习一个新的事物时,如果遇到一些概念无法很快理解,这可能是因为要理解概念会需要其它概念先建立起来,所以先暂时放下这个疑问也是一个学习方法,称之为“存疑” ,在以后的学习过程中待必要的概念学会后,目前的 ...

  6. 给EditText设置边框

    布局文件中加入background属性: <EditText android:layout_width="200dp" android:layout_height=" ...

  7. pro文件常用内容

    qmake生成的pro文件中常用变量 SUBDIRS 指定子目录 TARGET 指定生成的应用程序名(默认为项目名) DEPENDPATH 指定程序编译时依赖的相关路径 INCLUDEPATH 指定头 ...

  8. perl-cgi基础

    来源: http://www.cnblogs.com/itech/archive/2012/09/22/2698553.html 代码: http://xxxxx/cgi/perl-cgi.cgi?n ...

  9. **ERROR: Ninja build tool not found.

    | if which ninja-build ;\| then \| ln -s `which ninja-build` bin/ninja ;\| else \| echo "***ERR ...

  10. sql server数据库查询同义词

    查询数据库同义词: select * from sys.synonyms, 查询同义词个数:select count(1) from sys.synonyms