首页 > 代码库 > Shaping Regions

Shaping Regions

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

inputoutput
20 20 32 2 18 18 20 8 19 19 38 0 10 19 4
1 912 843 1874 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|1const int maxn=3e3+10;using namespace std;ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}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=0;    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+1),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+1),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+1),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+1),p.y2=op[nt].y2;    return ans;}int main(){    int i,j;    scanf("%d%d%d",&n,&m,&k);    ans[1]=n*m;    rep(i,1,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>=1;i--)    {        ans[op[i].c]+=(j=get(op[i],i+1));        ans[1]-=j;    }    rep(i,1,2500)if(ans[i])printf("%d %d\n",i,ans[i]);    //system("Pause");    return 0;}

Shaping Regions