HYSBZ1067(RMQ)

1067: [SCOI2007]降雨量

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 6711  Solved: 1814
[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

Sample Output

false
true
false
maybe
false

HINT

 

100%的数据满足:1<=n<=50000, 1<=m<=10000, -10^9<=yi<=10^9, 1<=ri<=10^9


题意:很明显

题解:标准的RMQ模板注意判断


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
#define clr(a,b)  memset(a,b,sizeof(a))
#define il        inline
typedef long long ll;
const int maxn = 50000 + 2;
const int minn = 130 + 1;

int n, m, dp[maxn][20], y[maxn];
void RMQ(int n)
{
	for (int j = 0; j < 17; ++j)
	{
		for (int i = n; i > 0; --i)
		{
			if (i + (1 << j) <= n)
				dp[i][j + 1] = max(dp[i][j], dp[i + (1 << j)][j]);
		}
	}
}
int query(int l, int r)
{
	if (l > r) { return -1; }
	int k=(int)(log(r-l+1.0)/log(2.0));
	return max(dp[l][k], dp[r - (1 << k) + 1][k]);
}
int main()
{
	while (~scanf("%d", &n))
	{
		clr(dp, 0);
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d%d", &y[i], &dp[i][0]);
		}
		RMQ(n);
		scanf("%d", &m);
		int tx, ty;
		int l, r;
		while (m--)
		{
			scanf("%d%d", &ty, &tx);
			l = lower_bound(y + 1, y + n + 1, ty) - y;
			r = lower_bound(y + 1, y + n + 1, tx) - y;
			if (y[l] == ty && y[r] == tx && r - l == tx - ty && dp[l][0] >= dp[r][0])  //y年到x年的降水量都已知(每年都有)    y年的降水量不小于x年
			{
				int h = query(l + 1, r - 1);
				if (h < dp[r][0])             //y+1到x-1年的降水量都小于x年
					puts("true");
				else                     //不成立则为假
					puts("false");
			}
			else if (y[l] == ty && y[r] == tx && dp[l][0] >= dp[r][0] && r - l != tx - ty)//y年的降水量不小于x年    y年,x年的降水量已知(x~y至少有一年未知)
			{
				int h = query(l + 1, r - 1);
				if (h < dp[r][0])             //y+1到x-1年的降水量都小于x年
					printf("maybe\n");
				else
					printf("false\n");
			}
			else if (y[l] != ty && y[r] == tx)  //y未知
			{
				int h = query(l, r - 1);
				if (h < dp[r][0])            //y到x-1年的降水量都小于x年
					puts("maybe");
				else
					puts("false");
			}
			else if (y[l] == ty && y[r] != tx)  //x未知
			{
				int h = query(l + 1, r - 1);
				if (h < dp[l][0])
					puts("maybe");
				else
					puts("false");
			}
			else if (y[l] != ty && y[r] != tx)    //x,y都未知
				puts("maybe");
			else                         //其他情况全为假
				puts("false");
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值