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

来源:互联网 发布:圣火明尊坐骑进阶数据 编辑:程序博客网 时间:2024/06/06 03:52

重新学习线段树。
好多都忘了,第一次没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;}
0 0
原创粉丝点击