P2471 [SCOI2007] 降雨量 - 洛谷 洛谷上是这题,WA30pts
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=50005;
int rain[N],year[N],st[N][20],lg[N],n;
int find(int x){
int l=1,r=n;
while(l<=r){
int mid=(l+r)/2;
if(year[mid]==x)
return mid;
if(year[mid]<x)
l=mid+1;
else
r=mid-1;
}
return 0;
}
int findy(int x){
int l=1,r=n;
while(l<r){
int mid=(l+r)/2;
if(year[mid]<=x)
l=mid+1;
else
r=mid;
}
return l;
}
int findx(int x){
int l=1,r=n;
while(l<r){
int mid=(l+r+1)/2;
if(year[mid]<=x)
l=mid;
else
r=mid-1;
}
return l;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n;
map<int,int> mp;
for(int i=1;i<=n;i++){
cin>>year[i]>>rain[i];
st[i][0]=rain[i];
mp[year[i]]=i;
}
for(int i=2;i<=n;i++)
lg[i]=lg[i/2]+1;
for(int j=1;j<=lg[n];j++){
for(int i=1;i<=n-(1<<j)+1;i++)
st[i][j]=max(st[i][j-1],st[i+(1<<(j-1))][j-1]);
}
int q;
cin>>q;
while(q--){
int y,x;
cin>>y>>x;
if(x-y==mp[x]-mp[y]){
int yw=find(y)+1,xw=find(x)-1;
int l=lg[xw-yw+1];
if(max(st[yw][l],st[xw-(1<<l)+1][l])<rain[xw+1]){
cout<<"true\n";
continue;
}
}
if(find(x)&&find(y)){
int yw=find(y)+1,xw=find(x)-1;
int l=lg[xw-yw+1];
if(max(st[yw][l],st[xw-(1<<l)+1][l])>=rain[xw+1]){
cout<<"false\n";
continue;
}
}
if(find(x)){
int yw=findy(y),xw=find(x)-1;
int l=lg[xw-yw+1];
if(max(st[yw][l],st[xw-(1<<l)+1][l])>=rain[xw+1]){
cout<<"false\n";
continue;
}
}
if(find(y)){
int yw=find(y)+1,xw=findx(x);
int l=lg[xw-yw+1];
if(max(st[yw][l],st[xw-(1<<l)+1][l])>=rain[yw-1]){
cout<<"false\n";
continue;
}
}
cout<<"maybe\n";
}
return 0;
}
大佬们帮忙看看吧