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. drawableLayout的使用(转载讲的比较清晰的文章)

    创建drawbler的布局文件 初始化drawbler的列表 响应drawable列表点击事件 现在侧滑菜单使用很多,大都是通过SlidingMenu实现.现在也可以通过DrawerLayout 创建 ...

  2. 复习之webview(观看张荣超视频)

    webview定义的两张方式: 1,是在xml文件里面进行定义: 2,实在代码里面定义:   3,在webview上显示内容必要的配置:   4常见的设置: 是否支持javascript 是否支持缩放 ...

  3. 深入理解JNI(《深入理解android》(author : 邓凡平)读书札记)

    JNI的技术特点: java能够调用native代码. native代码能够调用java代码.   JNI的技术考虑: 实现java代码的平台无关型. java语言发展初期使用C和C++代码,避免重复 ...

  4. ContentPlaceHolderID属性

    用来对应包含与当前内容关联的 ContentPlaceHolder 的 ID啊 说白了就是去找母版页相应的ContentPlaceHolder ,然后把内容扔进那里面去 <asp:Content ...

  5. hdu 1166 敌兵布阵(线段树详解)

    Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任 ...

  6. how to use the curses library in unix?

    In linux, you can use the ncurses library to use the terminal as a text buffer: move the cursor arou ...

  7. 个人linux简单笔记,随时更新

    vim显示行数 :set nu 查找文件 find /home -name config.txt 重命名文件或者文件夹 mv a b centos中phpize的安装 yum install php- ...

  8. WdatePicker默认日期为当天

    不说废话,直接上代码: <input id="d4311" class="Wdate" onfocus="WdatePicker({maxDat ...

  9. libc.so.6重做链接,删除导致的缺失问题(后期需要深入研究),未能成功升级

    中间件启动,提示/lib64/libc.so.6版本过低,升级glibc后,修改临时环境变量,结果导致sgment fault错误,根据报错 ll /lib64/ |grep libc -rwxr-x ...

  10. java中的类修饰符、成员变量修饰符、方法修饰符

    类修饰符:  public(访问控制符),将一个类声明为公共类,他可以被任何对象访问,一个程序的主类必须是公共类. abstract,将一个类声明为抽象类,没有实现的方法,需要子类提供方法实现. fi ...