poj -2528 Mayor's posters -离散化线段树

重新学习线段树。
好多都忘了,第一次没A过的原因是下表没用好,总是想从point[1]开始记录,排序又是从0开始的。

以后下标还是从0开始好
附上代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<fstream>

using namespace std;

struct Post{
    int l,r;
} post[10005];
typedef struct Node{
    int l,r;
    bool f;
    Node *lson,*rson;
} node;
node Tree[10001*4];
int point[20005],nodeCount;

void build(node *p,int l,int r){
    p->l=l;
    p->r=r;
    p->f=false;
    if (l>=r) return;
    ++nodeCount;
    p->lson=Tree+nodeCount;
    ++nodeCount;
    p->rson=Tree+nodeCount;
    build(p->lson,l,(l+r)/2);
    build(p->rson,(l+r)/2+1,r);
    return;
}

bool hang(node *root,int l,int r){
    if (root->f) return false;
    bool re=false;
    int m=(root->l+root->r)/2;
    if (root->l==l && root->r==r){
        return root->f=true;
    }
    else if (r<=m){
        re=hang(root->lson,l,r);
    }
    else if (l>=m+1){
        re=hang(root->rson,l,r);
    }
    else {
        re=hang(root->lson,l,m);
        re=hang(root->rson,m+1,r) || re;
    }
    if (root->lson->f && root->rson->f){
        root->f=true;
    }
    return re;
}

int main(){
    int c,n,pointNum,l,r;
    cin>>c;
    while (c--){
        cin>>n;
        pointNum=0;
        int ans=0;
        for (int i=1; i<=n; ++i) {
            cin>>post[i].l>>post[i].r;
            point[pointNum++]=post[i].l;
            point[pointNum++]=post[i].r;
        }
        sort(point, point+pointNum);
        pointNum=(int)(unique(point, point+pointNum)-point);
        nodeCount=0;
        build(Tree,0,pointNum-1);
        for (int i=n; i>=1; --i) {
            l=int (lower_bound(point, point+pointNum, post[i].l)-point);
            r=int (lower_bound(point, point+pointNum, post[i].r)-point);
            if (hang(Tree,l,r)){
                ans++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值