ural1147(Shaping Regions)矩形切割

來源:互聯網
上載者:User

標籤:style   blog   http   color   get   2014   

題目連結: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 32 2 18 18 20 8 19 19 38 0 10 19 4*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.