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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值