降雨量
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 4446 Solved: 1193
[ Submit][ Status][ Discuss]
Description
我们常常会说这样的话:“X年是自Y年以来降雨量最多的”。它的含义是X年的降雨量不超过Y年,且对于任意
Y<Z<X,Z年的降雨量严格小于X年。例如2002,2003,2004和2005年的降雨量分别为4920,5901,2832和3890,
则可以说“2005年是自2003年以来最多的”,但不能说“2005年是自2002年以来最多的”由于有些年份的降雨量未
知,有的说法是可能正确也可以不正确的。
Input
输入仅一行包含一个正整数n,为已知的数据。以下n行每行两个整数yi和ri,为年份和降雨量,按照年份从小
到大排列,即yi<yi+1。下一行包含一个正整数m,为询问的次数。以下m行每行包含两个数Y和X,即询问“X年是
自Y年以来降雨量最多的。”这句话是必真、必假还是“有可能”。
Output
对于每一个询问,输出true,false或者maybe。
Sample Input
6
2002 4920
2003 5901
2004 2832
2005 3890
2007 5609
2008 3024
5
2002 2005
2003 2005
2002 2007
2003 2007
2005 2008
2002 4920
2003 5901
2004 2832
2005 3890
2007 5609
2008 3024
5
2002 2005
2003 2005
2002 2007
2003 2007
2005 2008
Sample Output
false
true
false
maybe
false
true
false
maybe
false
HINT
100%的数据满足:1<=n<=50000, 1<=m<=10000, -10^9<=yi<=10^9, 1<=ri<=10^9
Source
解题思路:RMQ
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
const int INF=0x3f3f3f3f;
#define LL long long
#define MAX 50009
int n,m,dp[MAX][50];
int a[MAX],b[MAX];
int query(int l,int r)
{
if(l>r) return -1;
int k=0;
while(1<<(k+1)<=r-l+1) k++;
return max(dp[l][k],dp[r-(1<<k)+1][k]);
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++) scanf("%d %d",&a[i],&b[i]);
for(int i=1;i<=n;i++) dp[i][0]=b[i];
for (int j=1;(1<<j)<=n;j++)
{
for (int i=1;i+(1<<j)-1<=n;i++)
dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
scanf("%d",&m);
while(m--)
{
int a1,a2,l,r;
scanf("%d %d",&a1,&a2);
l=lower_bound(a+1,a+1+n,a1)-a;
r=lower_bound(a+1,a+1+n,a2)-a;
int flag=(a2-a1+1==r-l+1);
if(a[r]!=a2&&a[l]!=a1) {printf("maybe\n");continue;}
if(a[r]!=a2)
{
int ma=query(l+1,r-1);
if(ma<b[l]) printf("maybe\n");
else printf("false\n");
continue;
}
if(a[l]!=a1)
{
int ma=query(l,r-1);
if(b[r]>ma) printf("maybe\n");
else printf("false\n");
continue;
}
if(b[r]>b[l]) {printf("false\n");continue;}
if(flag)
{
int ma=query(l+1,r-1);
if(ma<b[r]&&b[r]<=b[l]) printf("true\n");
else printf("false\n");
continue;
}
else
{
int ma=query(l+1,r-1);
if(ma<b[r]&&b[r]<=b[l]) printf("maybe\n");
else printf("false\n");
}
}
}
return 0;
}