poj2528 贴海报 区间树离散化

  1. 利用离散化大大优化速度。
  2. 离散化:排序,去重,映射。
  3. 区间树:判断区间,上下更新值。

题目链接:http://acm.hust.edu.cn/vjudge/problem/14608

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
int n;
struct CPost
{
    int L,R;
};
CPost posters[10100];
int x[20200]; //存放所有海报的端点瓷砖编号
int hashs[10000010]; //hash[i]表示瓷砖i所处的离散化后的区间编号
struct CNode
{
    int L,R;
    bool bCovered; //区间[L,R]是否已经被完全覆盖
    CNode * pLeft, * pRight;
};
CNode Tree[1000000];
int nNodeCount = 0;
int Mid( CNode * pRoot)
{
    return (pRoot->L + pRoot->R)/2;
}
void BuildTree( CNode * pRoot, int L, int R)
{
    pRoot->L = L;
    pRoot->R = R;
    pRoot->bCovered = false;
    if( L == R )
        return;
    nNodeCount ++;
    pRoot->pLeft = Tree + nNodeCount;
    nNodeCount ++;
    pRoot->pRight = Tree + nNodeCount;
    BuildTree( pRoot->pLeft,L,(L+R)/2);
    BuildTree( pRoot->pRight,(L+R)/2 + 1,R);
}
bool Post( CNode *pRoot, int L, int R)
{
    if( pRoot->bCovered ) return false;
    if( pRoot->L == L && pRoot->R == R)
    {
        pRoot->bCovered = true;
        return true;
    }
    bool bResult ;
    if( R <= Mid(pRoot) ) bResult = Post( pRoot->pLeft,L,R);
    else if( L >= Mid(pRoot) + 1) bResult = Post( pRoot->pRight,L,R);
    else
    {
        bool b1 = Post(pRoot->pLeft,L,Mid(pRoot));
        bool b2 = Post(pRoot->pRight,Mid(pRoot) + 1,R);
        bResult = b1 || b2;
    }
    if( pRoot->pLeft->bCovered && pRoot->pRight->bCovered ) pRoot->bCovered = true;
    return bResult;
}
int main()
{
    int t;
    int i;
    scanf("%d",&t);
    int nCaseNo = 0;
    while(t--)
    {
        nCaseNo ++;
        scanf("%d",&n);
        int nCount = 0;
        for( i = 0; i < n; i ++ )
        {
            scanf("%d%d", & posters[i].L,
                  & posters[i].R );
            x[nCount++] = posters[i].L;
            x[nCount++] = posters[i].R;
        }
        sort(x,x+nCount);
        nCount = unique(x,x+nCount) - x;
//下面离散化
        int nIntervalNo = 0;
        for( i = 0; i < nCount; i ++ )
        {
            hashs[x[i]] = nIntervalNo;
            if( i < nCount - 1)
            {
                nIntervalNo++;
            }
        }
        BuildTree( Tree,0,nIntervalNo );
        int nSum = 0;
        for( i = n - 1; i >= 0; i -- )
        {
            if( Post(Tree,hashs[posters[i].L],hashs[posters[i].R]))
                nSum ++;
        }
        printf("%d\n",nSum);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值