USACO Shaping Regions

1、呼。。。终于过了这道,考察离散化+线段树的区间修改。因为是零基础,所以专门跑到POJ找了一道相似的题目做(POJ 2528)。。。终于写出来了~

2、所谓离散化,就是将无限空间映射到有限区域,通俗一点讲,就是只记录边界线。本题对x和y坐标均离散化,然后枚举x点并更新(合法的)x点对应的线段树(也就是说x要在该操作所包围的范围内),接着统计每种颜色的数量。要注意setv【0】初始值应设为1(表示刚开始是白纸)。

3、几个关键地方要注意,第一,y2=a【j】【3】-1。为什么要减一?因为题目给出的区间是【a,b)(左闭右开),但是线段树查询时是左闭右闭,因此应该查询【a,b-1】。第二,color应该加上(x【i+1】-x【i】)*(y【R+1】-y【L】)。因为一个坐标的末尾点(不含)是相邻的下一个坐标的始点(含)。不能理解的话,想一下L==R时就明白了——难道这个时候的面积是0吗?

/*
ID:mrxy564
PROG:rect1
LANG:C++
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<map>
#include<utility>
using namespace std;
typedef pair<const int,int> Pair;
map<int,int> mx,my;
int A,B,N,a[1010][5],x[2010],y[2010],color[2510],xn,yn,
 setv[8010],y1,y2,v,maxc=0;
void init(){
    scanf("%d%d%d",&A,&B,&N);
 int cnt=1;
 for(int i=0;i<N;i++){
  scanf("%d%d%d%d%d",&a[i][0],&a[i][1],&a[i][2],&a[i][3],&a[i][4]);
  if(a[i][4]>maxc) maxc=a[i][4];
  x[cnt]=a[i][0];x[cnt+1]=a[i][2];
  y[cnt]=a[i][1];y[cnt+1]=a[i][3];
  cnt+=2;
 }
    sort(x+1,x+1+cnt);sort(y+1,y+1+cnt);
    xn=unique(x+1,x+1+cnt)-(x+1);yn=unique(y+1,y+cnt+1)-(y+1);
    for(int i=1;i<=xn;i++)
  mx.insert(Pair(x[i],i));
 for(int i=1;i<=yn;i++)
  my.insert(Pair(y[i],i));
 for(int i=0;i<N;i++){
     for(int j=0;j<3;j+=2){
      a[i][j]=mx[a[i][j]];
   a[i][j+1]=my[a[i][j+1]];
  }
 }
}
void pushdown(int o){
    int lc=o*2,rc=o*2+1;
 if(setv[o]>=0){
     setv[lc]=setv[rc]=setv[o];
  setv[o]=-1;
 }
}
void update(int o,int L,int R){
    int lc=o*2,rc=o*2+1;
 if(y1<=L&&y2>=R){
     setv[o]=v;
 }else{
     pushdown(o);
  int M=L+(R-L)/2;
  if(y1<=M) update(lc,L,M);
  if(y2>M) update(rc,M+1,R);
 }
}
void query(int o,int L,int R,int base){
    if(setv[o]>0){
  color[setv[o]]+=(y[R+1]-y[L])*base;
  return;
 }
 else{
    if(L==R) return;
    int M=L+(R-L)/2;
    query(o*2,L,M,base);
    query(o*2+1,M+1,R,base);
 }
}
int main(){
 freopen("rect1.in","r",stdin);
 freopen("rect1.out","w",stdout);
 init();
 memset(color,0,sizeof(color));
 x[xn+1]=A;y[yn+1]=B;
    for(int i=1;i<=xn;i++){
     memset(setv,-1,sizeof(setv));
            setv[1]=1;
  for(int j=0;j<N;j++)
     if(mx[x[i]]>=a[j][0]&&mx[x[i]]<a[j][2]){
      y1=a[j][1];y2=a[j][3]-1;v=a[j][4];
      update(1,1,yn);
     }
  int base=x[i+1]-x[i];
                query(1,1,yn,base);
 }
        color[1]=0;
 for(int i=2;i<=maxc;i++)
        color[1]+=color[i];
 color[1]=A*B-color[1];
 for(int i=1;i<=maxc;i++)
  if(color[i])
     printf("%d %d\n",i,color[i]);
    return 0;
}

官方题解:

Analysis by Mathijs Vogelzang

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.

Further Analysis by Tomek Czajka

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.

And a solution from mrsigma:

#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;
}

And another fast solution from Saber Fadaee:

In Shaping Regions, I changed the whole A*B page into (2*N) * (2*N).

  Program rrect1;
  Var
    Inf,Outf            : Text;
    A,B,N,I,Z,Middle,J  : Longint;
    Color               : Array [1..2500] of Longint;
    D                   : Array [1..5000,1..5000] of boolean;
    Xar,Yar             : Array [0..2500] of Longint;
    Col                 : Array [1..10000] of Record
                                               x1 : Longint;
                                               x2 : Longint;
                                               y1 : Longint;
                                               y2 : Longint;
                                               c  : Longint;
                                             End;

  Function Find (K1 : integer) : Longint;
  Var
    Pointer,N1,N2                          : Longint;
  Begin
    N1 := 1;
    N2 := N + N;
    While N1 > 0 Do Begin
        Pointer := (N1 + N2) Div 2;
        If Xar[Pointer] = K1 then Begin
           Find := Pointer;
           Exit;
        End;
        If Xar[Pointer] > K1 Then
          N2 := Pointer - 1;
        If Xar[Pointer] < K1 Then
          N1 := Pointer + 1;
    End;
  End;

  Function Find1 (K2 : Longint) : Longint;
  Var
    Pointer,N1,N2                          : Longint;
  Begin
    N1 := 1;
    N2 := N + N;
    While N1 > 0 Do Begin
        Pointer := (N1 + N2) Div 2;
        If Yar[Pointer] = K2 then Begin
           Find1 := Pointer;
           Exit;
        End;
        If Yar[Pointer] > K2 Then
          N2 := Pointer - 1;
        If Yar[Pointer] < K2 Then
          N1 := Pointer + 1;
    End;
  End;

  Procedure Partition1 ( Lf , Rg : Longint );
  Var
    Pivot,L,R,Temp                 : Longint;
  Begin
    Pivot := Yar[Lf];
    L := Lf;
    R := Rg;
    While L < R Do Begin
        While (Yar[L] <= Pivot) and (L <= R) Do Inc(L);
        While (Yar[R] > Pivot) And (R >= L) Do Dec(R);
        If L < R Then
          begin
            Temp := Yar[L];
            Yar[L] := Yar[R];
            Yar[R] := Temp;
          end;
      End;
      Middle := R;
      Temp := Yar[Lf];
      Yar[Lf] := Yar[R];
      Yar[R] := Temp;
  End;

  Procedure QSort1 ( Left , Right : Longint );
  Begin
    if Left < Right Then
      Begin
        Partition1 (Left,Right);
        QSort1 (Left,Middle-1);
        QSort1 (Middle + 1, Right);
      End;
  End;

  Procedure Partition ( Lf , Rg : Longint );
  Var
    Pivot,L,R,Temp              : Longint;
  Begin
    Pivot := Xar[Lf];
    L := Lf;
    R := Rg;
    While L < R Do
      Begin
        While (Xar[L] <= Pivot) and (L <= R) Do Inc(L);
        While (Xar[R] > Pivot) And (R >= L) Do Dec(R);
        If L < R Then
          begin
            Temp := Xar[L];
            Xar[L] := Xar[R];
            Xar[R] := Temp;
          end;
      End;
      Middle := R;
      Temp := Xar[Lf];
      Xar[Lf] := Xar[R];
      Xar[R] := Temp;
  End;

  Procedure QSort ( Left , Right : Longint );
  Begin
    if Left < Right Then
      Begin
        Partition (Left,Right);
        QSort (Left,Middle-1);
        QSort (Middle + 1, Right);
      End;
  End;


  Begin
    Assign (Inf ,'rect1.in');
    Reset (Inf);
    Readln (Inf,A,B,N);
    For I := 1 To N Do
        Readln (Inf , Col[I].x1,col[i].y1,col[i].x2,col[i].y2,col[i].c);
    Close (Inf);
    For I := 1 to 2500 do Color[I] := 0;
    Color[1] := A * B;
    For I := 0 to n do
      For J := 0 to n do
        D[I,J] := False;
    Xar[0] := 0;
    Xar[n+n+1] := a;
    For I := 1 To N do Xar[i] := Col[i].x1;
    For I := N + 1 To N + N do Xar[i] := Col[i - n].x2;
      Qsort (1,N + N);
    Yar[0] := 0;
    Yar[n+n+1] := b;
    For I := 1 To N Do Yar[i] := Col[i].y1;
    For I := N + 1 To N + N do Yar[i] := col[i - n].y2;
      Qsort1 (1,N + N);
    For I := N Downto 1 Do
      For J := find(Col[i].x1) + 1 to find(col[i].x2) do
        For Z := find1(col[i].y1) + 1 to find1(col[i].y2) do
          If not D[J,Z] then
            Begin
              If col[i].c > 1 then
                Begin
                  Middle := (Xar[j] - Xar[j-1]) * (Yar[z] - Yar[z-1]);
                  Color[Col[i].c] := color[Col[i].c] + Middle;
                  Color[1] := Color[1] - Middle;
                End;
              D[J,Z] := True;
            End;
    Assign (Outf, 'rect1.out');
    Rewrite (Outf);
    For I := 1 To 2500 Do
      if color[i] > 0 then
        Writeln (Outf,i,' ', Color[i]);
    Close(Outf);
  End.

And, finally, an unbelievably compact recursive solution from Christoph Roick:

This solution uses recursion. We start with painting the last rectangle and go through to the first (the white paper). We save the edges of every rectangle, because we shouldn't paint over the rectangles above it. Now we have to divide the rectangles in 0 to 4 pieces around the rectangles covering it. In the end we have a lot of small rectangles and the colors can be added to an array by calculating the areas of the rectangle. So, we won't get any problems concerning too less memory.

program rect1;

var F: Text;
    i,a,b,n,cused,maxcolor:word;
    inform: array[1..1001,1..5] of word;
    used: array[1..1001,1..4] of word;
    countcolor: array[1..2500] of longint;

procedure cac(count,x1,y1,x2,y2,color: word); //cut and count
var py1,py2 : word;
begin
    if count<cused then begin
        if (x1>used[count,3]) or (x2<used[count,1]) or (y1>used[count,4]) or (y2<used[count,2]) then
            cac(succ(count),x1,y1,x2,y2,color)  //if there are no difficulties with the other rectangle
        else begin
            if y1>used[count,2] then py1:=y1 else py1:=used[count,2];
            if y2<used[count,4] then py2:=y2 else py2:=used[count,4];
            if y1<used[count,2] then cac(succ(count),x1,y1,x2,pred(used[count,2]),color);
            if y2>used[count,4] then cac(succ(count),x1,succ(used[count,4]),x2,y2,color);
            if x1<used[count,1] then cac(succ(count),x1,py1,pred(used[count,1]),py2,color);
            if x2>used[count,3] then cac(succ(count),succ(used[count,3]),py1,x2,py2,color);
        end;
    end else inc(countcolor[color],succ(x2-x1)*succ(y2-y1));
end;

begin
    Assign(F,'rect1.in');
    Reset(F);
    Readln(F,a,b,n);
    inc(n);
    for i:=2 to n do
        Readln(F,inform[i,1],inform[i,2],inform[i,3],inform[i,4],inform[i,5]);  //x1,y1,x2,y2,color
    Close(F);
    inform[1,1]:=0; inform[1,2]:=0; //white paper
    inform[1,3]:=a; inform[1,4]:=b; inform[1,5]:=1;
    maxcolor:=1;
    cused:=1;

    for i:=n downto 1 do begin
        cac(1,inform[i,1],inform[i,2],pred(inform[i,3]),pred(inform[i,4]),inform[i,5]);
        if inform[i,5]>maxcolor then maxcolor:=inform[i,5];  //we don't have to check all 2500 colors
        used[cused,1]:=inform[i,1];  //saving the coordinates of the rectangle
        used[cused,2]:=inform[i,2];
        used[cused,3]:=pred(inform[i,3]);
        used[cused,4]:=pred(inform[i,4]);
        inc(cused);
    end;

    Assign(F,'rect1.out');
    Rewrite(F);
    for i:=1 to maxcolor do
        if countcolor[i]>0 then
            Writeln(F,i,' ',countcolor[i]);
  Close(F);
end.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值