[BZOJ1067][SCOI2007]降雨量

原题地址

线段树.分类讨论很恶心…

AC code:

#include <cstdio>
#include <algorithm>
using namespace std;
const int N=100010;
int n,m,cnt;

struct nod{
    int l,r,mx,tot;
    nod *lc,*rc;
}*NIL,pool[N<<3];

struct Segtree{
    nod *root;

    Segtree(){
        NIL=&pool[cnt++];
        root=&pool[cnt++];
        root->lc=root->rc=NIL;
        root->l=-1000000000;root->r=1000000000;
    }

    void newnod(nod **p,int L,int R){
        *p=&pool[cnt++];
        (*p)->l=L;(*p)->r=R;
        (*p)->lc=(*p)->rc=NIL;
    }

    void insert(nod **p,int pos,int k){
        if((*p)->l==(*p)->r){
            (*p)->tot=1;(*p)->mx=k;
            return ;
        }
        int M=((*p)->l+(*p)->r)>>1;
        if(pos<=M){
            if((*p)->lc==NIL) newnod(&(*p)->lc,(*p)->l,M);
            insert(&(*p)->lc,pos,k);
        }
        else{
            if((*p)->rc==NIL) newnod(&(*p)->rc,M+1,(*p)->r);
            insert(&(*p)->rc,pos,k);
        }
        (*p)->tot=(*p)->lc->tot+(*p)->rc->tot;
        (*p)->mx=max((*p)->lc->mx,(*p)->rc->mx);
    }
    int get(nod *p,int L,int R){
        if(p==NIL) return 0;
        if(p->l==L&&p->r==R) return p->mx;
        int M=(p->l+p->r)>>1;
        if(R<=M) return get(p->lc,L,R);
        else if(L>M) return get(p->rc,L,R);
        else return max(get(p->lc,L,M),get(p->rc,M+1,R));
    }
    int total(nod *p,int L,int R){
        if(p==NIL) return 0;
        if(p->l==L&&p->r==R) return p->tot;
        int M=(p->l+p->r)>>1;
        if(R<=M) return total(p->lc,L,R);
        else if(L>M) return total(p->rc,L,R);
        else return total(p->lc,L,M)+total(p->rc,M+1,R);
    }
};

int main(){
    Segtree T;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        T.insert(&T.root,a,b);
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++){
        int y,x,a,b,c;
        scanf("%d%d",&y,&x);
        a=T.get(T.root,y,y);
        b=T.get(T.root,y+1,x-1);
        c=T.get(T.root,x,x);
        if((!a)&&(!c)) printf("maybe\n");
        else if(!c){
            if(b>=a) printf("false\n");
            else printf("maybe\n");
        }
        else if(!a){
            if(b>=c) printf("false\n");
            else printf("maybe\n");
        }
        else{
            if(a>=c&&b<c&&T.total(T.root,y+1,x-1)==x-y-1) printf("true\n");
            else if(a<c||b>=c) printf("false\n");
            else printf("maybe\n");
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值