POJ1177

 摘要:矩形覆蓋,求周長,線段樹+離散+掃描,注意分別是X,Y方向兩次掃描都求對應的“橫邊”

#include <stdio.h>
#include <cassert>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

const int size = 20000;
const int rectangle_num = 5000;

typedef struct Vertical{
    int x;
    int up;
    int low;
    int flag;
}Vertical;

typedef struct Rectangle{
    int x1;
    int x2;
    int y1;
    int y2;
}Rectangle;

Rectangle data[rectangle_num];
Vertical  verticals[rectangle_num * 2];

vector<int> hash_map;
typedef struct Node{
    int up;
    int low;
    int sc;
    int uf;
    int lf;
    int cover_len;
    int flag;
    Node *left_child;
    Node *right_child;

    void Insert(int, int, int);
    void Construct(int, int);
    void Update();
}Node;

int len=1;
Node sTree[size * 3];
Node *root = &sTree[0];

void Node::Construct(int l, int u)
{
    up = u;
    low = l;
    sc = 0;
    uf = 0;
    lf = 0;
    cover_len = 0;
    flag = 0;
   
    if( low+1 == up){
        left_child = right_child = NULL;
        return;
    }   

    int mid = ( up + low ) >> 1;
    left_child = &sTree[len++];
    right_child = &sTree[len++];

    left_child->Construct(low, mid);
    right_child->Construct(mid, up);
}

void Node::Insert(int l, int u, int f)
{
    if( low>=l && up<=u ){
        flag += f;
        return;
    }
       
    int mid = ( low + up ) >> 1;
    if( l < mid ){
        left_child->Insert(l, u, f);
    }
    if( u > mid){
        right_child->Insert(l, u, f);
    }
}

void Node::Update()
{
    if( flag > 0 ){
        lf = 1;
        uf = 1;
        cover_len = hash_map[up] - hash_map[low];
        sc = 1;   
        return;
    }
    if( low + 1 == up ){
        lf = 0;
        uf = 0;
        cover_len = 0;
        sc = 0;
        return;
    }       
    assert(left_child && right_child);
    left_child->Update();
    right_child->Update();
   
    lf = left_child->lf;
    uf = right_child->uf;
    cover_len = left_child->cover_len + right_child->cover_len;
    sc = left_child->sc + right_child->sc;
    if( left_child->uf==1 && right_child->lf==1){
        sc--;
    }       
}

bool compareVertical(Vertical one, Vertical two)
{
    return one.x < two.x;
}

int findIndex(int left, int right, int value)
{
    while( left <= right ){
        int mid = (left + right) >> 1;
        if(hash_map[mid] == value){
            return mid;
        }
        if( hash_map[mid] > value ){
            right = mid - 1;
        }else{
            left = mid + 1;
        }
    }

}


int main()
{
    int n;
    scanf("%d", &n);
    if( n == 0 ){
        printf("0/n");
        return 0;
    }   
    for(int i=1; i<=n; i++){
        Rectangle elem;   
        scanf("%d%d%d%d", &elem.x1, &elem.y1, &elem.x2, &elem.y2);
        data[i] = elem;
    }
   
    int index = 0;
    hash_map.clear();
    for(int i=1; i<=n; i++){
        Vertical elem;
        elem.x= data[i].x1;
        elem.low = data[i].y1;
        elem.up = data[i].y2;
        elem.flag = 1;
        verticals[index++] = elem;
       
        elem.x=data[i].x2;
        elem.flag = -1;
        verticals[index++] = elem;
       
        hash_map.push_back(data[i].y1);   
        hash_map.push_back(data[i].y2);   
    }
   
    sort(verticals, verticals+index, compareVertical);
    sort(hash_map.begin(), hash_map.end());
    vector<int>::iterator iter=unique(hash_map.begin(), hash_map.end());
    hash_map.erase(iter, hash_map.end());
    int hash_len = hash_map.size();
    len = 1;
    root->Construct(0, hash_len-1);
   
    int previous_x = verticals[0].x;
    int perimeter = 0;
    int last_sc = 0;
    int last_len = 0;
    root->Insert(findIndex(0, hash_len-1, verticals[0].low), findIndex(0, hash_len-1, verticals[0].up), verticals[0].flag);   
    for(int i=1; i<index; i++){
        if(verticals[i].x == verticals[i-1].x){
            root->Insert(findIndex(0, hash_len-1, verticals[i].low), findIndex(0, hash_len-1, verticals[i].up), verticals[i].flag);   
        }else{
            root->Update();
            perimeter += 2 * last_sc * (verticals[i-1].x-previous_x);
            last_sc = root->sc;
            last_len = root->cover_len;
            previous_x = verticals[i-1].x;       
       
            root->Insert(findIndex(0, hash_len-1, verticals[i].low), findIndex(0, hash_len-1, verticals[i].up), verticals[i].flag);   
        }
    }   
    root->Update();
    perimeter += 2 * last_sc * (verticals[index-1].x-previous_x);


    index = 0;
    hash_map.clear();
    for(int i=1; i<=n; i++){
        Vertical elem;
        elem.x= data[i].y1;
        elem.low = data[i].x1;
        elem.up = data[i].x2;
        elem.flag = 1;
        verticals[index++] = elem;
       
        elem.x=data[i].y2;
        elem.flag = -1;
        verticals[index++] = elem;
       
        hash_map.push_back(data[i].x1);   
        hash_map.push_back(data[i].x2);   
    }
   
    sort(verticals, verticals+index, compareVertical);
    sort(hash_map.begin(), hash_map.end());
    iter=unique(hash_map.begin(), hash_map.end());
    hash_map.erase(iter, hash_map.end());
    hash_len = hash_map.size();
    len = 1;
    root->Construct(0, hash_len-1);
   
    previous_x = verticals[0].x;
    last_sc = 0;
    last_len = 0;
    root->Insert(findIndex(0, hash_len-1, verticals[0].low), findIndex(0, hash_len-1, verticals[0].up), verticals[0].flag);   
    for(int i=1; i<index; i++){
        if(verticals[i].x == verticals[i-1].x){
            root->Insert(findIndex(0, hash_len-1, verticals[i].low), findIndex(0, hash_len-1, verticals[i].up), verticals[i].flag);   
        }else{
            root->Update();
            perimeter += 2 * last_sc * (verticals[i-1].x-previous_x);
            last_sc = root->sc;
            last_len = root->cover_len;
            previous_x = verticals[i-1].x;       
       
            root->Insert(findIndex(0, hash_len-1, verticals[i].low), findIndex(0, hash_len-1, verticals[i].up), verticals[i].flag);   
        }
    }   
    root->Update();
    perimeter += 2 * last_sc * (verticals[index-1].x-previous_x);


    printf("%d/n", perimeter);   

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章