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

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

導航

統(tǒng)計

常用鏈接

留言簿(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>
            国产综合视频| 亚洲欧美视频一区二区三区| 最新国产成人av网站网址麻豆 | 亚洲免费视频网站| 欧美在线二区| 精品成人一区| 欧美v日韩v国产v| 亚洲精品在线观| 亚洲尤物在线| 国产一区视频网站| 裸体女人亚洲精品一区| 亚洲精品一二三| 亚洲女同同性videoxma| 国产午夜精品一区二区三区欧美| 欧美伊人久久| 亚洲国产美女| 亚洲午夜av在线| 好吊一区二区三区| 欧美精品一区二区三区在线看午夜 | 欧美电影资源| 在线综合欧美| 国产综合18久久久久久| 欧美国产在线视频| 久久天堂国产精品| 欧美电影在线免费观看网站| 亚洲天堂久久| 亚洲成人在线| 国产精品九九| 久久一二三区| 亚洲一区二区三区免费在线观看 | 亚洲小说区图片区| 欧美 日韩 国产精品免费观看| 亚洲无限乱码一二三四麻| 国产揄拍国内精品对白| 欧美理论片在线观看| 欧美一区二区三区久久精品| 亚洲韩日在线| 久久一区二区三区超碰国产精品| 99热在这里有精品免费| 极品日韩久久| 国产精品亚洲欧美| 欧美激情视频给我| 久久久久久婷| 香蕉久久a毛片| av成人动漫| 亚洲成人在线视频播放 | 亚洲高清色综合| 欧美资源在线观看| 亚洲一区二区日本| 亚洲精品一区二| 在线观看国产精品网站| 国产日产高清欧美一区二区三区| 欧美日韩国产一区| 男男成人高潮片免费网站| 欧美在线免费视屏| 亚洲一区视频在线| 一区二区三区欧美成人| 亚洲日本在线观看| 欧美激情91| 免费在线播放第一区高清av| 久久国产精品免费一区| 亚洲综合色噜噜狠狠| av成人手机在线| 99www免费人成精品| 亚洲高清免费| 91久久国产精品91久久性色| 精品91在线| 在线观看91精品国产入口| 好看的av在线不卡观看| 韩曰欧美视频免费观看| 国产真实乱子伦精品视频| 国产亚洲精品成人av久久ww| 国产精品影视天天线| 国产精品毛片va一区二区三区 | 你懂的一区二区| 久久综合激情| 免费亚洲一区| 欧美另类一区二区三区| 欧美精品在线免费| 欧美三级网址| 国产精品高清免费在线观看| 国产精品v欧美精品v日韩| 欧美午夜免费| 国产伦精品一区二区三区视频黑人 | 午夜日韩电影| 欧美一区二区视频在线观看| 欧美在线电影| 久久综合图片| 欧美国产日韩一区| 亚洲国产网站| 亚洲另类黄色| 亚洲午夜精品久久久久久app| 亚洲一区二区欧美日韩| 欧美一级大片在线免费观看| 久久不射2019中文字幕| 麻豆精品精华液| 欧美日韩福利| 国产精品乱码一区二区三区| 国产视频精品xxxx| 伊人婷婷欧美激情| 亚洲乱码一区二区| 亚洲性视频网站| 久久久久亚洲综合| 欧美激情麻豆| 亚洲一二三区在线| 久久躁狠狠躁夜夜爽| 欧美经典一区二区| 国产精品婷婷| 亚洲青色在线| 午夜久久久久久| 欧美电影在线观看| 亚洲午夜精品一区二区| 久久激情一区| 欧美日韩一区二区三区在线视频 | 国产精品99久久久久久白浆小说| 亚洲欧美日韩直播| 欧美成年人网| 国产精品一二三四| 日韩网站在线| 久久精品国产v日韩v亚洲| 欧美激情精品久久久久久久变态 | 欧美在线影院| 欧美日韩成人| 国产综合网站| 亚洲视频第一页| 免费成人av| 亚洲深夜激情| 欧美凹凸一区二区三区视频| 欧美性大战久久久久久久蜜臀| 狠狠色丁香久久综合频道| 一区二区三区视频在线播放| 久久人人爽人人| 在线中文字幕日韩| 欧美www视频| 一区二区三区在线不卡| 亚洲男同1069视频| 亚洲精品欧美在线| 免费观看日韩| 黄色亚洲免费| 欧美在线视频一区二区| 99国内精品| 欧美大片在线看| 在线观看福利一区| 久久久中精品2020中文| 亚洲在线观看| 国产精品毛片在线看| 一本色道久久综合一区| 亚洲成人资源| 久久综合伊人77777蜜臀| 国模叶桐国产精品一区| 亚洲欧美日韩一区在线| 99re6这里只有精品| 欧美激情视频一区二区三区在线播放 | 久久久久久91香蕉国产| 国产日韩精品一区| 欧美一级午夜免费电影| 亚洲视频导航| 国产精品久久影院| 国产精品99久久久久久有的能看 | 久久久久久亚洲精品杨幂换脸| 国产视频精品va久久久久久| 香蕉成人伊视频在线观看 | 老鸭窝毛片一区二区三区| 欧美伊人精品成人久久综合97| 国产精品亚洲综合一区在线观看 | 欧美日本一道本| 日韩一级网站| 亚洲精品极品| 欧美日本国产| aa级大片欧美三级| 99国产一区| 国产精品地址| 欧美一区二区三区精品| 亚洲欧美国产精品桃花| 国产一区日韩二区欧美三区| 久久久久久久久久码影片| 久久精品综合网| 亚洲区在线播放| 亚洲精品日本| 国产精品卡一卡二| 欧美一区二区啪啪| 久久久久免费视频| 亚洲老板91色精品久久| 日韩性生活视频| 国产精品毛片在线| 久久在线免费观看| 免费精品视频| 亚洲天堂成人在线观看| 亚洲欧美在线网| 亚洲国产成人午夜在线一区| 亚洲区免费影片| 国产精品成人v| 久久久精品性| 欧美高清视频| 午夜精品久久久久99热蜜桃导演| 午夜精品视频在线| 亚洲国产另类 国产精品国产免费| 亚洲精品九九| 国产一区亚洲一区| 91久久久亚洲精品|