BZOJ 1067 降雨量 (线段树)

14 篇文章 0 订阅

1067: [SCOI2007]降雨量

Time Limit: 1 Sec Memory Limit: 162 MB

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

思路:
线段树维护最大值,二分查找年份位置。
方法很简单,但是细节很恶心。。。
(1)i,j均未知输出mayde。
(2)i知j未知,因为j最大可以和i相等,所以如果i到j之间有大于i的需输出false,否则输出mayde。
(3)i未知j知,同上
(4)i知j知,如果i到j中已知的大于j或j的信息大于i,则输出false,如果所有年份已知输出true,有未知的输出mayde。
具体的就看代码吧。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream> 
#define INF 0x3fffffff
using namespace std;

const int N = 100010;

int n, m, mn;
int year[N], rain[N], mx[N*2];

void build(int x, int l, int r){
    if(l == r){
        mx[x] = rain[l]; 
        return;
    } 
    int mid = (l + r) >> 1;
    build(x<<1, l, mid); 
    build(x<<1|1, mid+1, r); 
    mx[x] = max(mx[x<<1], mx[x<<1|1]);
}

int query(int x, int l, int r, int lf, int rg){
    if(lf<=l && r<=rg) return mx[x]; 
    int mid = (l + r) >> 1;
    int q = 0;
    if(lf <= mid) q = max(q, query(x<<1, l, mid, lf, rg)); 
    if(rg > mid) q = max(q, query(x<<1|1, mid+1, r, lf, rg));
    return q;
}

int find(int p, bool a){
    int l=1, r=n;
    if( a ){//第一个大于等于的数 
        l = upper_bound( year+1, year+n+1, p) - year;
        if(year[l-1] == p) l--; 
    }
    else{//第一个小于等于的数 
        l = lower_bound( year+1, year+n+1, p) - year - 1;
    }
    return l;
}

int main(){
    scanf("%d", &n); mn=INF; 
    for(int i=1; i<=n; i++){
        scanf("%d%d", &year[i], &rain[i]);
        mn = min(mn, year[i]);
    }
    build(1, 1, n); 
    scanf("%d", &m);
    while( m-- ){
        int a, b; scanf("%d%d", &a, &b);//b年是自a年以来降雨量最多的
        int c = find(a, 1), d = find(b, 1);
        if(a >= b){ puts("false"); continue;}
        if(b != year[d]){//b未知 
            if(a != year[c]){ puts("maybe"); continue;}//都未知 
            if(c == n){ puts("maybe"); continue;}//之间没有其他的已知年份 
            if(query(1, 1, n, c+1, find(b,0)) >= rain[c]) puts("false");
            else puts("maybe"); 
            continue;
        }
        if(a != year[c]){//a未知
            if(d == 1){ puts("maybe"); continue;}//之间没有其他的已知年份
            if(a < mn){
                if(query(1, 1, n, 1, d-1) >= rain[d]) puts("false");
                else puts("maybe");
            }
            else{
                if(query(1, 1, n, find(a,1), d-1) >= rain[d]) puts("false");
                else puts("maybe");
            }
            continue;
        }
        if(rain[d] > rain[c] || query(1, 1, n, c+1, d-1) >= rain[d]) puts("false");
        else{
            if(d-c != b-a) puts("maybe");
            else puts("true");//中间年份全部已知 
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值