降雨量 HYSBZ - 1067

我们常常会说这样的话:“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

细节题:
因为只需维护区间的最大值,不带修改,所以我们用st表查询;
当然线段树也可以;
我们每次判断时,从 false ----> maybe ----> true ;
由于我们每次询问的是年份,而存储的时候是下标递增存储的,所以用 lower_bound 二分查找,
细节颇多,

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 10000007;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-7
typedef pair<int, int> pii;


inline int rd() {
    int x = 0;
    char c = getchar();
    bool f = false;
    while (!isdigit(c)) {
        if (c == '-') f = true;
        c = getchar();
    }
    while (isdigit(c)) {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return f ? -x : x;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

int n, q, Y[maxn], val[maxn];
int stTable[maxn][20];

void ST() {
    for (int i = 1; i <= n; i++)stTable[i][0] = val[i];
    int k = log2(n);
    for (int i = 1; i <= k; i++) {
        for (int j = 1; j + (1 << i) - 1 <= n; j++) {
            stTable[j][i] = max(stTable[j][i - 1], stTable[j + (1 << (i - 1))][i - 1]);
        }
    }
}

int query(int l, int r) {
    int k = log2(r - l + 1);
    return max(stTable[l][k], stTable[r - (1 << k) + 1][k]);
}

void sol() {
    rdint(q);
    int x, y, posx, posy;
    while (q--) {
        rdint(y); rdint(x);
        if (y >= x) { cout << "false" << endl; continue; }
        posx = lower_bound(Y + 1, Y + 1 + n, x) - Y;
        posy = lower_bound(Y + 1, Y + 1 + n, y) - Y;
        if (y == Y[posy] && x == Y[posx]) {
            if (val[posy] < val[posx]) {
                cout << "false" << endl;
            }
            else if (posx == posy + 1) {
                if (x == y + 1)cout << "true" << endl;
                else cout << "maybe" << endl;
            }
            else {
                int maxx = query(posy + 1, posx - 1);
                if (maxx >= val[posx]) { cout << "false" << endl; }
                else {
                    if (x - y == posx - posy)cout << "true" << endl;
                    else cout << "maybe" << endl;
                }
            }
        }
        //-------------
        else if (y != Y[posy] && x == Y[posx]) {
            if (posy == posx) { cout << "maybe" << endl; }
            else {
                int Max = query(posy, posx - 1);
                if (Max >= val[posx]) { cout << "false" << endl; }
                else cout << "maybe" << endl;
            }
        }
        //---------------
        else if (y == Y[posy] && x != Y[posx]) {
            if (posx == posy + 1) {
                cout << "maybe" << endl;
            }
            else {
                int Max = query(posy + 1, posx - 1);
                if (Max >= val[posy]) { cout << "false" << endl; }
                else cout << "maybe" << endl;
            }
        }
        //------------------
        else {
            cout << "maybe" << endl;
        }
    }
}

int main()
{
    //ios::sync_with_stdio(false);
    rdint(n);
    for (int i = 1; i <= n; i++) {
        rdint(Y[i]); rdint(val[i]);
    }
    val[n + 1] = INF;
    ST();
    sol();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值