ural1147(Shaping Regions)矩形切割

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1147

题意:一个10000*10000的矩阵,初始颜色都为1,然后最多2500次涂色,每次涂色将一个矩形的面积涂成某个特定颜色,问涂完之后每种颜色最终的面积。

解法:倒序计算,矩形切割

代码:

/******************************************************
* @author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std;

#define eps 1e-8
#define zero(_) (abs(_)<=eps)
const double pi=acos(-1.0);
typedef long long LL;
const int Max=2510;
const int INF=1e9+7;

struct rec
{
    int x1,x2,y1,y2;
    int color;
} recs[Max];
int A,B;
int n;
int  getans(int x1,int x2,int y1,int y2,int p)
{
    while((p<=n)&&(x1>=recs[p].x2||x2<=recs[p].x1||y1>=recs[p].y2||y2<=recs[p].y1))
        p++;
    if(p>n)
        return (x2-x1)*(y2-y1);
    int ans=0;
    if(x1<recs[p].x1)
        ans+=getans(x1,recs[p].x1,y1,y2,p+1),x1=recs[p].x1;
    if(x2>recs[p].x2)
        ans+=getans(recs[p].x2,x2,y1,y2,p+1),x2=recs[p].x2;
    if(y2>recs[p].y2)
        ans+=getans(x1,x2,recs[p].y2,y2,p+1),y2=recs[p].y2;
    if(y1<recs[p].y1)
        ans+=getans(x1,x2,y1,recs[p].y1,p+1);
    return ans;
}
int ans[Max];
int main()
{
    while(~scanf("%d%d%d",&A,&B,&n))
    {
        memset(ans,0,sizeof ans);
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d%d%d%d",&recs[i].x1,&recs[i].y1,&recs[i].x2,&recs[i].y2,&recs[i].color);
        }
        ans[1]=A*B;
        for(int i=n; i>=1; i--)
        {
            int tool=getans(recs[i].x1,recs[i].x2,recs[i].y1,recs[i].y2,i+1);
            ans[recs[i].color]+=tool;
            ans[1]-=tool;
        }
        for(int i=1; i<=2500; i++)
            if(ans[i])
                printf("%d %d\n",i,ans[i]);
    }
    return 0;
}
/*
20 20 3
2 2 18 18 2
0 8 19 19 3
8 0 10 19 4
*/

ural1147(Shaping Regions)矩形切割,布布扣,bubuko.com

时间: 2024-08-04 22:05:16

ural1147(Shaping Regions)矩形切割的相关文章

Shaping Regions(dfs)

Shaping Regions Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 124  Solved: 39[Submit][Status][Web Board] Description 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 rec

ural 1147. Shaping Regions

1147. Shaping Regions Time limit: 0.5 secondMemory 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

Shaping Regions

Shaping Regions Time limit: 0.5 secondMemory 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 border

USACO5.3 IDDFS_强连通_二维树状数组_斐蜀定理_矩形切割

启发式搜索 启发式搜索的主要思想是通过评价一个状态有"多好"来改进对于解的搜索. 方法#1:启发式剪枝 估价函数最简单最普通的用法是进行剪枝.假设有一个求最小代价的一个搜索,使用一个可行的估价函数.如果搜到当前状态时代价为A,这个状态的估价函数是B,那么从这个状态开始搜所能得到的最小代价是A+B.如果当前最优解是C满足C 方法#2:最佳优先搜索 最佳搜索可以看成贪心的深度优先搜索. 与一般搜索随意扩展后继节点不同,最优优先搜索按照估价函数所给的他们的"好坏"的顺序扩

Poj 3695-Rectangles 矩形切割

Rectangles Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3846   Accepted: 1124 Description You are developing a software for painting rectangles on the screen. The software supports drawing several rectangles and filling some of them w

Poj 2528-Mayor&#39;s posters 线段切割

题目:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 55156   Accepted: 16000 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have

uva 12296(切割凸多边形)

题意:有一个长l宽w的矩形,左下角坐标是(0,0),现在有n条线段把这个矩形切割,保证每条线段的两个端点落在矩形不同的边上,把矩形分成了若干区域,现在有m个圆画进矩形,问圆覆盖了哪些区域,并把这些区域的面积排序输出. 题解:先要切割矩形,方法是每读入一条线段都拿去处理切割出的新的区域把原先的区域替换,最后可以得到一个区域的vector,然后判断圆和区域的情况: 1.如果圆没有公共点,可以是圆在某个区域内,或某些区域在圆内.这时需要判断多边形是否有顶点在圆内,或圆心是否在那个多边形内. 2.判断圆

【poj1177】 Picture

http://poj.org/problem?id=1177 (题目链接) 题意 求矩形周长并. Solution 转自:http://www.cnblogs.com/Booble/archive/2010/10/10/1847163.html 先看图: 为了解决这个问题 我们先把一坨一坨的矩形 进行矩形切割: 我们考虑周长由哪些部分构成 其中,红线是需要统计入周长的竖边,绿线是需要统计入周长的横边 我们称两条蓝线之间的部分为统计区间 我们需要依次统计从左到右的统计区间内的需要计数的矩形边,累加

2016.6.13 考试总结

一.哲哲回家 [题目描述]: 哲哲放学了,准备坐公交车回家.因为急着回家看MSN战士,所以他想用最短的时间回家,但是因为学校离家里比较远,有时候他需要转几趟车才可以回家.等车是需要一段时间的,而且每辆公交车的速度不一样,哲哲晕掉了,不知道怎样才可以在最短的时间里回家,作为一名cjoier,你应该帮帮他. 现在一直哲哲所在的城市有N个公交站点(学校在一号站点,哲哲家在N号站点)和M条公交线路,每条公交线路是一个有序集合,公交线路i包含Pi个站点a[i,1],a[i,2]…a[i,Pi],表示i号公