Winter Day48: HDU1542-Atlantis- rectangular area and - scan line segment tree +

Topic links: HDU - 1542 

 

Example:

Sample Input
2
10 10 20 20
15 15 25 25.5
0

Sample Output
Test case #1
Total explored area: 180.00 

 

The meaning of problems: a given n, n ≠ 0, gives the n rows, each row of a given The corners of the rectangle (x1, y1, x2, y2)

N outputs each data covered rectangular area above the graph

 

Ideas: tree line scanning line template title

 

note:

line array need to open twice because each is given two rectangular coordinates.

segment tree tree array of scan lines need to open eight times, four times generally open segment tree.

 

Need arrays and structures used:

const  int N = 110 ; 

Double y [N << . 1 ]; // record the y-coordinate 

struct Node 
{ 
    int L, R & lt, C; // around point segment tree, cover overlapping coverage 
    Double len, LF, RF; // actual 
} Tree [<< N . 3 ]; 

struct Line 
{ 
    int F; // rectangle: 1 into the left side, the right-side -1 
    Double X, up, down; // line abscissa, ordinate vertical 
} line [N << . 1 ];

 

 

Operation achievements:

void build(int i,int L,int R)
{
    tree[i].l=L,tree[i].r=R;
    tree[i].c=tree[i].len=0;
    tree[i].lf=y[L],tree[i].rf=y[R];
    if(L+1==R)
        return ;
    int mid=(L+R)>>1;
    build(i<<1,L,mid);
    build(i<<1|1,mid,R);//不是mid+1,是[1,2][2,3],不是[1,2][3,4]
}

 

pushup operation:

void a pushup ( int I) // update the information to maintain the entire tree 
{
     IF (Tree [I] .c) 
    { 
        Tree [I] .LEN = Tree [I] .rf- Tree [I] .lf;
         return ; 
    } 
    // the following are the tree [i] .c == 0, indicating that is not covered 
    IF (Tree [I] .L + . 1 == Tree [I] .r) // is a leaf node, 
        Tree [I]. = len 0 ;
     the else // is an entire section of line is not completely covered 
        Tree [I] = .LEN Tree [I << . 1 ] + Tree .LEN [I << . 1 | . 1 ] .LEN; 
}

 

update operation:

void update(int i,Line a)//加入一条新线段、更新线段树
{
    if(a.down==tree[i].lf&&a.up==tree[i].rf)
    {
        tree[i].c+=a.f;
        pushup(i);
        return;
    }
    if(a.up<=tree[i<<1].rf)
        update(i<<1,a);
    else if(a.down>=tree[i<<1|1].lf)
        update(i<<1|1,a);
    else
    {
        Line b=a,c=a;
        b.up=tree[i<<1].rf,c.down=tree[i<<1|1].lf;
        update(i<<1,b);
        update(i<<1|1,c);
    }
    pushup(i);
}

 

AC Code:

#include <stdio.h> 
#include <the iostream> 
#include < String .h> 
#include <algorithm> 
#include <Queue>
 the using  namespace STD;
 #define INF 0x3f3f3f3f 
typedef Long  Long LL;
 const  int N = 110 ; 

Double Y [N << . 1 ]; // record the y-coordinate 

struct Node 
{ 
    int L, R & lt, C; // around point segment tree, cover overlapping coverage 
    Double len, LF, RF; // actual 
} tree [N << 3 ];

struct Line 
{ 
    int F; // rectangle: 1 into the left side, the right-side -1 
    Double X, up, Down; // abscissa line segment, the vertical ordinate 
} Line [N << 1 ]; 

BOOL CMP1 (Line X, Y Line) 
{ 
    return XX < YX; 
} 

void Build ( int I, int L, int R & lt) 
{ 
    Tree [I] .L = L, Tree [I] .r = R & lt; 
    Tree [I] .c = Tree [I] .LEN = 0 ; 
    Tree [I] .lf = Y [L], Tree [I] .rf = Y [R & lt];
     IF (L + . 1 ==R & lt)
         return ;
     int MID = (L + R & lt) >> . 1 ; 
    Build (I << . 1 , L, MID); 
    Build (I << . 1 | . 1 , MID, R & lt); // not a mid + 1, is [1,2] [2,3] not [1,2] [3,4-] 
} 

void a pushup ( int I) // update the information to maintain the entire tree 
{
     IF (Tree [I] .c) 
    { 
        Tree [I] .LEN = Tree [I] .rf- Tree [I] .lf;
         return ; 
    } 
    // the following are the tree [i] .c == 0, indicating that is not covered 
    IF (Tree [I] + .L . 1 == Tree [I] .r) //Is a leaf node, 
        Tree [I] .LEN = 0 ;
     the else // is an entire section of line is not completely covered 
        Tree [I] = .LEN Tree [I << . 1 ] + Tree .LEN [I << . 1 | . 1 ] .LEN; 
} 

void update ( int I, Line A) // Add a new segment, segment tree updates 
{
     IF (a.down == tree [I] == .lf && a.up tree [I] .rf) 
    { 
        Tree [I] .c + = AF; 
        a pushup (I); 
        return ; 
    } 
    IF (a.up <= Tree [I << . 1 ] .rf) 
        Update (I << . 1 , A);
    else if(a.down>=tree[i<<1|1].lf)
        update(i<<1|1,a);
    else
    {
        Line b=a,c=a;
        b.up=tree[i<<1].rf,c.down=tree[i<<1|1].lf;
        update(i<<1,b);
        update(i<<1|1,c);
    }
    pushup(i);
}

int main()
{
    int n,tt=1;
    while(~scanf("%d",&n)&&n)
    {
        int k=1;
        for(int i=1; i<=n; i++)
        {
            double x1,y1,x2,y2;
            scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
            line[k].x=x1,line[k].down=y1,line[k].up=y2,line[k].f=1;
            y[k++]=y1;
            line[k].x=x2,line[k].down=y1,line[k].up=y2,line[k].f=-1;
            y[k++] = Y2; 
        } 
        Sort (Line + . 1 , Line + K, CMP1); 
        Sort (Y + . 1 , Y + K); 
        Build ( . 1 , . 1 , - K- . 1 ); 
        Update ( . 1 , Line [ . 1 ]);
         Double ANS = 0 ;
         for ( int I = 2 ; I <K; I ++) // scan line 
        { 
            ANS + = Tree [ . 1 ] .LEN * (Line [I] .x-Line [I- . 1 ] .x) ; // add a new area
            update(1,line[i]);
        }
        printf("Test case #%d\n",tt++);
        printf("Total explored area: %.2f\n\n",ans);//两个\n
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/OFSHK/p/12465693.html