把年份离散化后记区间最大值,特判区间内有位置年份的情况。
#include<algorithm> #include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> #include<iomanip> #include<iostream> #include<map> #include<queue> #include<stack> #include<vector> #define rep(i,x,y) for(register int i=(x);i<=(y);i++) #define dwn(i,x,y) for(register int i=(x);i>=(y);i--) #define maxn 700010 #define ls (u<<1) #define rs (u<<1|1) #define mi (l+r>>1) using namespace std; int read() { int x=0,f=1;char ch=getchar(); while(!isdigit(ch)&&ch!=‘-‘)ch=getchar(); if(ch==‘-‘)f=-1,ch=getchar(); while(isdigit(ch))x=(x<<1)+(x<<3)+ch-‘0‘,ch=getchar(); return x*f; } void write(int x) { int f=0;char ch[20]; if(!x){putchar(‘0‘),putchar(‘\n‘);return;} if(x<0)x=-x,putchar(‘-‘); while(x)ch[++f]=x%10+‘0‘,x/=10; while(f)putchar(ch[f--]); putchar(‘\n‘); } map<int,int>to; int n,m,year[maxn],rain[maxn],qx[maxn],qy[maxn],num[maxn],a[maxn],cnt,rnk; int st[maxn][18],lg[maxn]; int MAX(int x,int y) { if(x>y)return 0; int lim=y-x+1; return max(st[x][lg[lim]],st[y-(1<<lg[lim])+1][lg[lim]]); } int main() { memset(st,-1,sizeof(st)); n=read(); rep(i,1,n)year[i]=read(),rain[i]=read(),num[++cnt]=year[i]; m=read(); rep(i,1,m)qy[i]=read(),qx[i]=read(),num[++cnt]=qx[i],num[++cnt]=qy[i]; sort(num+1,num+cnt+1);lg[0]=-1; rep(i,1,cnt) { if(i==1||num[i]!=num[i-1])rnk++; to[num[i]]=rnk; } rep(i,1,n)a[to[year[i]]]=1; rep(i,1,rnk)a[i]+=a[i-1],lg[i]=lg[i>>1]+1; rep(i,1,n)st[to[year[i]]][0]=rain[i]; rep(k,1,lg[rnk]){for(int i=1;i+(1<<k)-1<=rnk;i++) st[i][k]=max(st[i][k-1],st[i+(1<<k-1)][k-1]);} rep(i,1,m) { int rx=st[to[qx[i]]][0],ry=st[to[qy[i]]][0],maxr=MAX(to[qy[i]]+1,to[qx[i]]-1),no=qx[i]-qy[i]+1-(a[to[qx[i]]]-a[to[qy[i]]-1])+(ry==-1?1:0); //cout<<rx<<" "<<ry<<" "<<no<<" "<<maxr<<endl; if((rx!=-1&&((ry<rx&&ry!=-1)||maxr>=rx))||(rx==-1&&maxr>=ry&&ry!=-1))puts("false"); else if(rx!=-1&&(ry>=rx&&maxr<rx&&!no))puts("true"); else puts("maybe"); } return 0; }
原文:https://www.cnblogs.com/xzyf/p/9671406.html