bzoj1067 [SCOI2007]降雨量

题目链接

一道比较裸的线段树

左右端点是年份,维护是否有空点、最大值即可

  1 #include<algorithm>
  2 #include<iostream>
  3 #include<cstdlib>
  4 #include<cstring>
  5 #include<cstdio>
  6 #include<string>
  7 #include<cmath>
  8 #include<ctime>
  9 #include<queue>
 10 #include<stack>
 11 #include<map>
 12 #include<set>
 13 #define rre(i,r,l) for(int i=(r);i>=(l);i--)
 14 #define re(i,l,r) for(int i=(l);i<=(r);i++)
 15 #define Clear(a,b) memset(a,b,sizeof(a))
 16 #define inout(x) printf("%d",(x))
 17 #define douin(x) scanf("%lf",&x)
 18 #define strin(x) scanf("%s",(x))
 19 #define LLin(x) scanf("%lld",&x)
 20 #define op operator
 21 #define CSC main
 22 typedef unsigned long long ULL;
 23 typedef const int cint;
 24 typedef long long LL;
 25 using namespace std;
 26 void inin(int &ret)
 27 {
 28     ret=0;int f=0;char ch=getchar();
 29     while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
 30     while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar();
 31     ret=f?-ret:ret;
 32 }
 33 struct segtree{int l,r,Max,bo;}t[200001];
 34 int n,m;
 35 void build(int k,int l,int r)
 36 {
 37     if(l==r){scanf("%d%d",&t[k].l,&t[k].Max);t[k].r=t[k].l;t[k].bo=1;return;}
 38     int mid=(l+r)>>1;
 39     build(k<<1,l,mid);build(k<<1|1,mid+1,r);
 40     t[k].bo=(t[k<<1].bo&&t[k<<1|1].bo);
 41     if(t[k<<1].r+1!=t[k<<1|1].l)t[k].bo=0;
 42     t[k].l=t[k<<1].l;t[k].r=t[k<<1|1].r;
 43     t[k].Max=max(t[k<<1].Max,t[k<<1|1].Max);
 44 }
 45 int get(int k,int x)
 46 {    
 47     if(t[k].l==t[k].r)
 48     {
 49         if(t[k].l!=x)return 0;                
 50         else return t[k].Max;
 51     }
 52     if(x<=t[k<<1].r)return get(k<<1,x);
 53     else if(x>=t[k<<1|1].l)return get(k<<1|1,x);
 54     return 0;
 55 }
 56 int query(int k,int x,int y,int num)
 57 {
 58     bool ret=0;
 59     if(x<t[k].l){ret=1;x=t[k].l;}
 60     if(t[k].l==x&&t[k].r==y)
 61     {
 62         if(t[k].Max>=num)return 0;
 63         else if(t[k].bo&&!ret)return 1;
 64         else return 2;
 65     }
 66     if(y<=t[k<<1].r)return query(k<<1,x,y,num);
 67     else if(x>=t[k<<1|1].l)return query(k<<1|1,x,y,num);
 68     else 
 69     {
 70         int t1=query(k<<1,x,t[k<<1].r,num);
 71         int t2=query(k<<1|1,t[k<<1|1].l,y,num);
 72         if(!t1||!t2)return 0;
 73         else if(t[k<<1].r+1!=t[k<<1|1].l)return 2;
 74         else return 1;
 75     } 
 76 }
 77 int next(int k,int x)
 78 {
 79     int l=t[k].l,r=t[k].r;
 80     if(l==r)return t[k].l;
 81     if(x<t[k<<1].r)return next(k<<1,x);
 82     else return next(k<<1|1,x);
 83 }
 84 int last(int k,int x)
 85 {
 86     int l=t[k].l,r=t[k].r;
 87     if(l==r)return t[k].l;
 88     if(x>t[k<<1|1].l)return last(k<<1|1,x);
 89     else return last(k<<1,x);
 90 } 
 91 int CSC()
 92 {
 93     inin(n);
 94     build(1,1,n);
 95     inin(m);
 96     re(i,1,m)
 97     {
 98         int l,r;
 99         scanf("%d%d",&l,&r);
100         if(r<l){printf("false
");continue;}
101         int ll=get(1,l),rr=get(1,r);
102         if(!ll&&!rr)printf("maybe
");
103         else 
104         {
105             int s=next(1,l),t=last(1,r);
106             if(!ll)
107             {
108                 if(s>t||r==t){printf("maybe
");continue;}
109                    int ret=query(1,s,t,rr);
110                 if(ret==0)printf("false
");
111                 else printf("maybe
");
112             }
113             else if(!rr)
114             {
115                 if(s>t||l==s){printf("maybe
");continue;}
116                    int ret=query(1,s,t,ll);
117                 if(ret==0)printf("false
");
118                 else printf("maybe
");
119             }
120             else 
121             {
122                 if(rr>ll){printf("false
");continue;}
123                 if(s>t)
124                 {
125                      if(l+1==r)printf("true
");
126                      else printf("maybe
");
127                      continue;
128                 }
129                 int ret=query(1,s,t,rr);
130                 if(ret==0)printf("false
");
131                 else if(ret==1)
132                     if(l+1==s&&r-1==t)printf("true
");
133                        else printf("maybe
");
134                 else if(query(1,s,t,rr)==2)printf("maybe
");
135                 else printf("false
");
136             }
137         }
138     }
139     return 0;
140 }
原文地址:https://www.cnblogs.com/HugeGun/p/5151084.html