您的位置:首页 > 其它

[BZOJ1067][SCOI2007]降雨量

2015-07-20 00:25 330 查看
原题地址

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

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: