青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

USACO Section 3.1 Shaping Regions

Shaping Regions

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.

PROGRAM NAME: rect1

INPUT FORMAT

The order of the input lines dictates the order of laying down the rectangles. The first input line is a rectangle "on the bottom".
Line 1: A, B, and N, space separated (1 <= A,B <= 10,000)
Lines 2-N+1: Five integers: llx, lly, urx, ury, 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.

SAMPLE INPUT (file rect1.in)

20 20 3
2 2 18 18 2
0 8 19 19 3
8 0 10 19 4

INPUT EXPLANATION

Note that the rectangle delineated by 0,0 and 2,2 is two units wide and two high. Here's a schematic diagram of the input:

11111111111111111111
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
11222222442222222211
11222222442222222211
11222222442222222211
11222222442222222211
11222222442222222211
11222222442222222211
11111111441111111111
11111111441111111111

The '4's at 8,0 to 10,19 are only two wide, not three (i.e., the grid contains a 4 and 8,0 and a 4 and 8,1 but NOT a 4 and 8,2 since this diagram can't capture what would be shown on graph paper).

OUTPUT FORMAT

The output file 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 OUTPUT (file rect1.out)

1 91
2 84
3 187
4 38
Shaping Regions: Hint
An array of all 'points' is too big; 16MB maximum. Keep track of the rectangles' coordinates; split the rectangle when an overlap occurs, e.g.: 
+--------+      +-+--+--+
|        |      | |2 |  |
|        |      + +--+  |
|  +-+   |  --> | |  |  |
|  +-+   |      |1|  |3 |
|        |      | +--+  |
|        |      | | 4|  |
+--------+      +-+--+--+
 
Official analysis

A straightforward approach to this problem would be to make an array which represents the table, and then draw all the rectangles on it. In the end, the program can just count the colors from the array and output them. Unfortunately, the maximum dimensions of this problem are 10,000 x 10,000, which means the program uses 100 million integers. That's too much, so we need another approach.

An approach that does work for such large cases (and it actually is a lot faster too) is to keep track of the rectangles, and delete portions of them when they are covered by other rectangles. 
Consider this input set: 
0 0 10 10 5
5 0 15 10 10

The program first reads in the first rectangle and puts it in a list. When it reads a new rectangle it checks all items in the list if they overlap with the new rectangle. This is the case, and then it deletes the old rectangle from the list and adds all parts which aren't covered to the list. (So in this case, the program would delete the first rectangle, add 0 0 5 10 5 to the list and then add the second rectangle to the list). `` If you're unlucky, a new rectangle can create lots of new rectangles (when the new rectangle entirely fits into another one, the program creates four new rectangles which represent the leftover border:

+--------+      +-+--+--+
|        |      | |2 |  |
|        |      + +--+  |
|  +-+   |  --> | |  |  |
|  +-+   |      |1|  |3 |
|        |      | +--+  |
|        |      | | 4|  |
+--------+      +-+--+--+

This is not a problem however, because there can be only 2500 rectangles and there is plenty of memory, so rectangles have to be cut very much to run out of memory. Note that with this approach, the only thing that matters is how many rectangles there are and how often they overlap. The maximum dimensions can be as large as you want, it doesn't matter for the running time.

There is another solution to this problem, which runs in O(n*n*log n) time, but is quite tricky. First, we add one big white rectangle at the bottom - the paper. Then we make two arrays: one containing all vertical edges of the rectangles, and the other the horizontal ones. For each edge we have its coordinates and remember, whether it's the left or right edge (top or bottom). We sort these edges from left to right and from top to bottom. Then we go from left to right (sweep), jumping to every x-coordinate of vertical edges. At each step we update the set of rectangles seen. We also want to update the amount of each color seen so far. So for each x we go from top to bottom, for each y updating the set of rectagles at a point (in the structure described below) and choosing the top one, so that we can update the amounts of colors seen. The structure to hold the set of rectangles at a point should allow adding a rectangle (number from 1..1000), deleting a rectangle, and finding the top one. We can implement these operations in O(log n) time if we use a heap. To make adding and deleting run in O(log n) we must also have for each rectangle its position in the heap. So the total time spent at each point is O(log n). Thus the algorithm works in O(n*n*log n) time.

Official Code

 

#include <stdio.h>
#include 
<stdlib.h>
#include 
<string.h>

FILE 
*fp,*fo;

struct rect
{
    
int c;
    
int x1,y1,x2,y2;
}
;

int c[2501];
rect r[
10001];

int intersect(rect a,const rect &b,rect out[4])
{
    
/* do they at all intersect? */
    
if(b.x2<a.x1||b.x1>=a.x2)
        
return 0;
    
if(b.y2<a.y1||b.y1>=a.y2)
        
return 0;
    
/* they do */

    rect t;

    
if(b.x1<=a.x1&&b.x2>=a.x2&&b.y1<=a.y1&&b.y2>=a.y2)
            
return -1;

    
/* cutting `a' down to match b */
    
int nout=0;
    
if(b.x1>=a.x1) {
        t
=a,t.x2=b.x1;
        
if(t.x1!=t.x2)
            
out[nout++]=t;
        a.x1
=b.x1;
    }

    
if(b.x2<a.x2) {
        t
=a,t.x1=b.x2;
        
if(t.x1!=t.x2)
            
out[nout++]=t;
        a.x2
=b.x2;
    }

    
if(b.y1>=a.y1) {
        t
=a,t.y2=b.y1;
        
if(t.y1!=t.y2)
            
out[nout++]=t;
        a.y1
=b.y1;
    }

    
if(b.y2<a.y2) {
        t
=a,t.y1=b.y2;
        
if(t.y1!=t.y2)
            
out[nout++]=t;
        a.y2
=b.y2;
    }

    
return nout;
}


int main(void{
    fp
=fopen("rect1.in","rt");
    fo
=fopen("rect1.out","wt");

    
int a,b,n;
    fscanf(fp,
"%d %d %d",&a,&b,&n);

    r[
0].c=1;
    r[
0].x1=r[0].y1=0;
    r[
0].x2=a;
    r[
0].y2=b;

    rect t[
4];

    
int i,j,rr=1;
    
for(i=0;i<n;i++{
        
int tmp;
        fscanf(fp,
"%d %d %d %d %d",&r[rr].x1,&r[rr].y1,&r[rr].x2,&r[rr].y2,&r[rr].c);

        
if(r[rr].x1>r[rr].x2) {
            tmp
=r[rr].x1;
            r[rr].x1
=r[rr].x2;
            r[rr].x2
=tmp;
        }

        
if(r[rr].y1>r[rr].y2) {
            tmp
=r[rr].y1;
            r[rr].y1
=r[rr].y2;
            r[rr].y2
=tmp;
        }


        
int nr=rr;
        rect curr
=r[rr++];
        
for(j=0;j<nr;j++{
            
int n=intersect(r[j],curr,t);
            
if(!n)
                
continue;
            
if(n==-1{
                memmove(r
+j,r+j+1,sizeof(rect)*(rr-j-1));
                j
--;
                rr
--;
                nr
--;
                
continue;
            }

            r[j]
=t[--n];
            
for(;n-->0;)
                r[rr
++]=t[n];
        }

    }


    
for(i=0;i<rr;i++)
        c[r[i].c]
+=(r[i].x2-r[i].x1)*(r[i].y2-r[i].y1);

    
for(i=1;i<=2500;i++)
        
if(c[i])
            fprintf(fo,
"%d %d\n",i,c[i]);

    
return 0;
}


My Code


/*
ID:braytay1
PROG:rect1
mble
LANG:C++
*/

#include 
<iostream>
#include 
<fstream>
using namespace std;

struct rectangle{
    
int lx;
    
int ly;
    
int rx;
    
int ry;
    
int color;
}
;

int A,B,N;
int xs[2002],ys[2002];
int area[2501];
rectangle rec[
1001];

void swap(int *p1,int *p2){
    
int tmp;
    tmp
=*p1;
    
*p1=*p2;
    
*p2=tmp;
}

int partition(int a[],int p,int r){
    
int x,i;
    x
=a[r];
    i
=p-1;
    
for (int j=p;j<r;j++)
    
{
        
if (a[j]<=x) {i++;swap(a+i,a+j);}
    }

    swap(a
+i+1,a+r);
    
return i+1;
}

void quicksort(int a[],int p,int r){
    
if (p<r)
    
{
        
int q;
        q
=partition(a,p,r);
        quicksort(a,p,q
-1);
        quicksort(a,q
+1,r);
    }

}




int color_num(rectangle &a){
    
for (int i=N;i>=0;i--){
        
if ((a.lx>=rec[i].lx)&&(a.rx<=rec[i].rx)&&(a.ly>=rec[i].ly)&&(a.ry<=rec[i].ry))
            
return rec[i].color;
    }

    
return 1;
}

int reform(int *p,int n){
    
int tmp[2002],cur_num;
    tmp[
0]=*p;
    cur_num
=0;
    
for (int i=1;i<=n;i++){
        
if (tmp[cur_num]!=*(p+i)) {
            cur_num
++;
            tmp[cur_num]
=*(p+i);
        }

    }

    
for (int i=0;i<=n;i++*(p+i)=0;
    
for (int i=0;i<=cur_num;i++*(p+i)=tmp[i];
    
return cur_num;
}

int main(){
    ifstream fin(
"rect1.in");
    ofstream fout(
"rect1.out");
    memset(area,
0,sizeof(area));
    fin
>>A>>B>>N;
    rec[
0].lx=0;rec[0].ly=0;rec[0].rx=A;rec[0].ry=B;rec[0].color=1;
    xs[
0]=0;xs[1]=A;ys[0]=0;ys[1]=B;
    
for (int i=1;i<=N;i++){
        fin
>>rec[i].lx>>rec[i].ly>>rec[i].rx>>rec[i].ry>>rec[i].color;
        xs[i
*2]=rec[i].lx;
        xs[i
*2+1]=rec[i].rx;
        ys[i
*2]=rec[i].ly;
        ys[i
*2+1]=rec[i].ry;
    }

    
int xlong,ylong;
    quicksort(xs,
0,2*N+1);
    quicksort(ys,
0,2*N+1);
    xlong
=reform(xs,2*N+1);
    ylong
=reform(ys,2*N+1);
    quicksort(xs,
0,xlong);
    quicksort(ys,
0,ylong);
    
int k=0;
    
for (int i=0;i<ylong;i++){
        
for (int j=0;j<xlong;j++){
            rectangle single;
            
if (xs[j]==xs[j+1]||ys[i]==ys[i+1]) continue;
            single.lx
=xs[j];single.ly=ys[i];single.rx=xs[j+1];single.ry=ys[i+1];
            single.color
=color_num(single);
            area[single.color
-1]+=(single.rx-single.lx)*(single.ry-single.ly);
        }

    }

    
for (int i=0;i<2500;i++)
        
if (area[i]) fout<<i+1<<" "<<area[i]<<endl;
    
return 0;
}

posted on 2008-08-21 18:11 幻浪天空領主 閱讀(633) 評論(0)  編輯 收藏 引用 所屬分類: USACO

<2025年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

導航

統計

常用鏈接

留言簿(1)

隨筆檔案(2)

文章分類(23)

文章檔案(22)

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美在线免费| 亚洲福利视频一区二区| 欧美日韩一二三区| 国产精品美女黄网| 亚洲日本激情| 久久精品论坛| 亚洲最快最全在线视频| 欧美不卡福利| 在线日韩欧美视频| 午夜精品视频在线观看| 亚洲麻豆视频| 欧美福利专区| 国产精品一区二区女厕厕| 正在播放亚洲一区| 欧美成人精品福利| 久久激情五月激情| 国产一区二区三区在线观看免费 | 99在线精品视频在线观看| 久久久www免费人成黑人精品 | 欧美日韩亚洲三区| 亚洲每日更新| 欧美亚洲一区二区三区| 免费在线欧美黄色| 性刺激综合网| 国产欧美日韩专区发布| 久久久91精品国产| 欧美一区二区三区四区在线观看| 国产精品理论片| 午夜精品久久久久久久久久久| 亚洲视频在线看| 亚洲自拍啪啪| 欧美国产1区2区| 一本大道久久精品懂色aⅴ| 91久久国产自产拍夜夜嗨| 久色婷婷小香蕉久久| 亚洲人成精品久久久久| 亚洲国产福利在线| 欧美吻胸吃奶大尺度电影| 宅男噜噜噜66一区二区 | 欧美电影美腿模特1979在线看| 激情综合五月天| 亚洲第一天堂av| 欧美福利视频在线观看| 亚洲天堂成人在线视频| 亚洲女女女同性video| 国产精品一二| 美国十次成人| 欧美日韩亚洲国产精品| 欧美一区二区三区婷婷月色| 亚洲女同在线| 欧美日韩另类综合| 久久av一区二区三区漫画| 欧美综合国产精品久久丁香| 亚洲韩国一区二区三区| 一区二区三区欧美激情| 在线观看日韩www视频免费 | 亚洲精品美女| 亚洲国产裸拍裸体视频在线观看乱了中文| 欧美精品一区三区| 欧美一区激情视频在线观看| 美女久久网站| 久久国产精品亚洲77777| 久久精品人人爽| 亚洲资源在线观看| 久久婷婷综合激情| 亚洲一级电影| 欧美成人精品在线观看| 性伦欧美刺激片在线观看| 嫩草影视亚洲| 久久国内精品自在自线400部| 欧美精品v日韩精品v国产精品| 一区在线视频观看| 亚洲午夜视频在线| 99精品国产高清一区二区| 久久av一区| 亚洲在线1234| 欧美日韩在线播放三区| 欧美成人性网| 国产日本精品| 欧美成人一区二区三区在线观看| 国产精品中文字幕欧美| 亚洲免费av网站| 日韩亚洲成人av在线| 久久久久国产免费免费| 欧美一区二区视频网站| 欧美视频中文字幕在线| 欧美大胆人体视频| 激情国产一区二区| 性做久久久久久久久| 午夜日本精品| 国产精品剧情在线亚洲| 一本久道久久综合狠狠爱| 亚洲精品视频二区| 久久综合狠狠综合久久综合88| 久久久噜噜噜久噜久久| 国产伦精品一区二区三区| 日韩一级黄色av| 一区二区三区四区五区精品视频| 欧美成人中文字幕| 一级成人国产| 欧美午夜精品一区二区三区| 亚洲伦理在线免费看| 一本色道久久综合亚洲精品高清| 欧美精品午夜| 99re在线精品| 艳妇臀荡乳欲伦亚洲一区| 夜夜嗨av一区二区三区中文字幕 | 亚洲欧美日韩国产成人| 亚洲一区二区三区乱码aⅴ| 欧美日韩视频在线第一区| 日韩午夜电影| 在线电影国产精品| 日韩视频永久免费| 亚洲伊人伊色伊影伊综合网 | 亚洲国产欧美一区二区三区同亚洲 | 老司机久久99久久精品播放免费| 国产亚洲成年网址在线观看| 欧美中日韩免费视频| 久久久午夜精品| 精品999日本| 久久精品在线观看| 亚洲欧洲日本mm| 亚洲免费视频中文字幕| 国产欧美日韩不卡免费| 久久中文在线| 亚洲国产专区| 性欧美超级视频| 欧美视频在线免费看| 久久先锋资源| 99国产成+人+综合+亚洲欧美| 欧美日韩日本国产亚洲在线 | 免费毛片一区二区三区久久久| 一区二区动漫| 黄色综合网站| 国产综合视频在线观看| 国产精品第一区| 欧美日韩一二三区| 欧美激情欧美狂野欧美精品| 久久中文欧美| 久久综合一区二区三区| 久久午夜电影网| 久久综合久久综合九色| 久久婷婷麻豆| 免费成人网www| 久久精选视频| 久久精品欧美日韩| 欧美一区日韩一区| 欧美一区二区高清| 午夜精品一区二区三区在线视| 亚洲自拍高清| 香蕉av福利精品导航| 性欧美xxxx视频在线观看| 午夜在线精品偷拍| 欧美一区二区视频网站| 欧美影院在线| 毛片av中文字幕一区二区| 免费亚洲一区二区| 欧美激情综合亚洲一二区| 欧美a级一区二区| 欧美精品一区二区三区四区| 欧美日韩国产片| 国产精品在线看| 精品1区2区3区4区| 亚洲日本无吗高清不卡| 亚洲精选国产| 亚洲欧美日韩国产一区| 久久久久9999亚洲精品| 美国十次成人| 91久久午夜| 午夜精品婷婷| 免费欧美高清视频| 欧美天天视频| 国产一区二区三区的电影| 亚洲黄色在线观看| 亚洲天堂网在线观看| 久久精品五月婷婷| 亚洲国产欧美另类丝袜| 亚洲一区二区三区影院| 久久精品国产成人| 欧美日韩成人在线播放| 国产深夜精品| 亚洲美女淫视频| 久久久久国产精品一区二区| 亚洲欧洲精品一区二区三区不卡| 亚洲深夜福利| 欧美aa在线视频| 国产日韩视频| 在线亚洲精品| 欧美成人69av| 亚洲欧美国产精品桃花| 欧美丰满少妇xxxbbb| 国产午夜精品一区二区三区欧美| 亚洲欧洲视频| 久久久国产精品一区| 9人人澡人人爽人人精品| 久久精品av麻豆的观看方式| 国产精品成人一区二区三区夜夜夜 | 亚洲欧美日韩国产| 亚洲第一网站| 久久精品亚洲乱码伦伦中文|