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. 【读书笔记】Linux源码注释

    第二章 大概的内部组成 IO端口寻址: 统一寻址: 就是把地址归入存储器寻址范围. 独立寻址: 跟存储器分开,专门的寻址空间 没怎么理解, PC机一般都是采用独立寻址, 见下图 在linux里,可以在 ...

  2. 如何使用SecureCRT连接ubuntu

    1. 首先要明白什么是ssh? 可以把ssh看做是telnet的加强版,telnet的密码和信息都是不加密的,而ssh则加密. .2. 开启ubuntu上的ssh功能 先安装,安装后就自动开启了. s ...

  3. js函数大全(2)

    原文地址:http://phperbar.blog.163.com/blog/static/162596182201032935815391/ 1.常规函数 javascript常规函数包括以下9个函 ...

  4. cpanel 定时运行sh/php

    php -q /home/用户/public_html/cron.php                   -------------------php格式 /home/用户/public_html ...

  5. 第四十五节,logging日志模块

    用于便捷记录日志且线程安全的模块 单文件日志 basicConfig()模块函数 功能:创建日志文件和写日志模式[有参] 使用方法:模块名称.basicConfig(filename="日志 ...

  6. python paramiko基本

    基本 import paramiko blade = '192.168.0.10' port = 22 username = 'admin' password = ' localpath = '/ho ...

  7. C语言中的string.h中的内存字符串处理函数

    转载请注明出处:http://blog.csdn.net/zhubin215130/article/details/8993403 void *memcpy(void *dest, const voi ...

  8. 在JavaScript中创建命名空间的几种写法

    在JavaScript中全局变量经常会引起命名冲突,甚至有时侯重写变量也不是按照你想像中的顺序来的,可以看看下面的例子: var sayHello = function() { return 'Hel ...

  9. CentOS7 PostgreSQL 主从配置( 二)

    同步流复制配置PostgreSql的流复制是异步的,缺点是Standby上的数据落后于主库上的数据,如果使用Hot Standby做读写分离,就会存在数据一致性的问题.PostgreSql9.1版本后 ...

  10. 抛弃jQuery,拥抱原生JavaScript

    前端发展很快,现代浏览器原生 API 已经足够好用.我们并不需要为了操作 DOM.Event 等再学习一下 jQuery 的 API.同时由于 React.Angular.Vue 等框架的流行,直接操 ...