ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1147. Shaping Regions

Hi, I got AC with N*N*logN too, but there's a beautiful Algo with recursive...
Posted by Pham Hung Son 27 Dec 2005 03:30
Hi,
I just passed Tests of USACO, and then I saw this beautiful solution in "Analysis".Then I tried to implement it. But then I got TLE on test #17 ( in USACO, I submitted this sol, and it runs 10 times faster than N^2logN ). I'm wondering, if this Algo is N^3 . Is it true ?
(Sorry for my bad English :p )

Hier is the code :

//TLE : #17 acm ru, but 10 times faster in USACO (in comparasion to Heap Algo)
#include <iostream>
#include <vector>
using namespace std;
#define FOR(i,n) for(int i=0;i<n;i++)
#define CLEAR(A) memset(A,0,sizeof(A))
#define FILL(A,v) fill(A.begin(),A.end(),v)
const   int maxn = 1002;
const   int maxC = 2502;

class Rect {
      public: int x1,y1,x2,y2,c;
              Rect() {}
              Rect(int xx1,int yy1,int xx2,int yy2,int cc)
              { x1=xx1;x2=xx2;y1=yy1;y2=yy2;c=cc; }
};
vector<int> area(maxC);
int         N;
vector<Rect>  rect(0);
void Partition(int id,Rect R)
{   if (R.x1 >= R.x2 || R.y1 >= R.y2 ) return;
    if (id   >  N                    )
       { area[R.c] += (R.x2-R.x1)*(R.y2-R.y1); return; }
    if (R.x1 >= rect[id].x2 || R.x2 <= rect[id].x1 ||
        R.y1 >= rect[id].y2 || R.y2 <= rect[id].y1)       Partition(id+1,R);
    else{
        if (rect[id].x2 < R.x2 ) Partition(id+1,Rect(rect[id].x2,R.y1,R.x2,R.y2,R.c));
        if (rect[id].x1 > R.x1 ) Partition(id+1,Rect(R.x1,R.y1,rect[id].x1,R.y2,R.c));
        if (rect[id].y1 > R.y1 ) Partition(id+1,Rect(max(R.x1,rect[id].x1),R.y1,min(R.x2,rect[id].x2),rect[id].y1,R.c));
        if (rect[id].y2 < R.y2 ) Partition(id+1,Rect(max(R.x1,rect[id].x1),rect[id].y2,min(R.x2,rect[id].x2),R.y2,R.c));
    }
}
int main()
{   FILL(area,0);
    int A,B,x1,y1,x2,y2,c;
    cin>> A >> B >> N; rect.resize(N+1);
    rect[0] = Rect(0,0,A,B,1);
    for(int i=1;i<=N;i++)
    { cin>>x1>>y1>>x2>>y2>>c; rect[i]=Rect(x1,y1,x2,y2,c); }
    for(int i=0;i<=N;i++) Partition(i+1,rect[i]);
    for(int i=1;i<maxC;i++)
        if (area[i] > 0) cout<<i<<" "<<area[i]<<endl;
    return 0;
}
Re: Hi, I got AC with N*N*logN too, but there's a beautiful Algo with recursive...
Posted by Vladimir Yakovlev (USU) 27 Dec 2005 13:51
Tests from USACO are weak. This solution is really O(N^3). Try this test:

1003 1003 1000
1 2 1002 3 2
2 1 3 1002 3
1 4 1002 5 2
4 1 5 1002 3
1 6 1002 7 2
6 1 7 1002 3
...
1 998 1002 999 2
998 1 999 1002 3
1 1000 1002 1001 2
1000 1 1001 1002 3
Re: Hi, I got AC with N*N*logN too, but there's a beautiful Algo with recursive...
Posted by ACM.Tolstobrov_Anatoliy[Ivanovo SPU] 28 Jun 2006 19:04
 Vladimir I cheating avoid test 17

add this test to stop me

1003 1003 1000
1003 1003 1
1 2 1002 3 2
2 1 3 1002 3
1 4 1002 5 2
4 1 5 1002 3
1 6 1002 7 2
6 1 7 1002 3
...
1 998 1002 999 2
998 1 999 1002 3
1 1000 1002 1001 2

ID of my submit 1225870