POJ3277

 摘要:简单的矩形覆盖,用线段树+离散

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

const int size = 40000;
typedef struct Node{
    int left;
    int right;
    int height;
    Node * left_child;
    Node * right_child;
    int max_height;
    void Insert(int, int, int);
    void Construct(int, int);
    void Calculate();
}Node;

typedef struct Elem{
    int left;
    int right;
    int height;
}Elem;

int len = 1;
Node sTree[size*6];
int silhouette[size*2+1] = {0};
Node *root = &sTree[0];

void Node::Construct(int l, int r)
{
    left = l;
    right = r;
    height = 0;
    max_height = 0;
    if(l+1 == r){
        left_child = NULL;
        right_child = NULL;
        return;           
    }   
    int mid = (l + r) >> 1;
    left_child = &sTree[len++];
    right_child = &sTree[len++];
   
    assert(left_child && right_child);
    left_child->Construct(l, mid);
    right_child->Construct(mid, r);
}

void Node::Insert(int l, int r, int h)
{
    if(h <= height){
        return;
    }
    if(l==left && r==right && max_height<=h){
        max_height = max(h, max_height);
        //cout << "/tInsert:" << l << "," << r << "," << h << endl;
        height = h;
        return;
    }
   
    max_height = max(h, max_height);
   
    if(height != -1){
        left_child->height = height;
        right_child->height = height;
    }
    height = -1;
    int mid = (left + right) >> 1;

    assert(left_child && right_child);   
    if(l >= mid){
        right_child->Insert(l, r, h);
        return;   
    }
    if(r <= mid){
        left_child->Insert(l, r, h);
        return;   
    }

    left_child->Insert(l, mid, h);
    right_child->Insert(mid, r, h);   
}

void Node::Calculate()
{
    if( height != -1 ){
        for(int i=left; i<right; i++){
            silhouette[i] = height;       
        }
        return;
    }   
    left_child->Calculate();
    right_child->Calculate();
}

int findC(int value, vector<int> & coordinate)
{
    int left = 0;
    int right = coordinate.size()-1;
    while( left <= right ){
        int mid = (left + right) >> 1;
        if( coordinate.at(mid) == value ){
            return mid;
        }
        if(coordinate.at(mid) > value){
            right = mid - 1;
        }else{
            left = mid + 1;
        }       
    }
   
    return -1;
}

int main()
{
    int n;
    scanf("%d", &n);
    vector<Elem> rectangular;
    vector<int> coordinate;
    for(int i=1; i<=n; i++){
        Elem elem;   
        scanf("%d%d%d", &elem.left, &elem.right, &elem.height);
        rectangular.push_back(elem);
        coordinate.push_back(elem.left);
        coordinate.push_back(elem.right);   
    }
       
    sort(coordinate.begin(), coordinate.end());
    vector<int>::iterator erase_iter = unique(coordinate.begin(), coordinate.end());
    coordinate.erase(erase_iter, coordinate.end());       
    //copy(coordinate.begin(), coordinate.end(), ostream_iterator<int>(cout, "/t"));
    //cout << endl;
   
    len = 1;
    root->Construct(0, coordinate.size()-1);   
   
    for(vector<Elem>::iterator iter=rectangular.begin(); iter!=rectangular.end(); iter++){
        int l_c = findC(iter->left, coordinate);
        int r_c = findC(iter->right, coordinate);
        assert(l_c>=0 && r_c>=0);
        //cout << "insert:" << l_c << "," << r_c << "," << iter->height << endl;
        root->Insert(l_c, r_c, iter->height);       
    }

    memset(silhouette, 0, sizeof(silhouette));   
    root->Calculate();


    long long area = 0;   
    int pre_h = silhouette[0];
    int pre_x = coordinate[0];
    int cur_h = 0;   
    int cur_x = 0;
    for(int i=1; i<=coordinate.size()-1; i++){
        cur_h = silhouette[i];   
        cur_x = coordinate[i];
        if(cur_h!=pre_h){
            if(pre_h != 0){
                //cout << pre_x << "," << cur_x << "," << pre_h << endl;
                area += ((long long)(cur_x - pre_x)) * pre_h;       
            }
            pre_h = cur_h;
            pre_x = cur_x;   
        }       
    }
   
    printf("%lld/n",area);   


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值