洛谷P2471 [SCOI2007]降雨量

题链

该题洛谷题解区讲的都很好;

当询问时, l == r时,答案有可能不是true... (奇怪的坑)

#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#pragma GCC optimize("O2")
using namespace std;
#define LL long long
#define ll long long
#define ULL unsigned long long
#define ls rt<<1
#define rs rt<<1|1
#define one first
#define two second
#define MS 50009
#define INF 1e18
#define mod 99999997
#define Pi acos(-1.0)
#define Pair pair<LL,LL>
#define eps 1e-9

LL n,m,k;
LL p[MS][31];
LL ha[MS];
LL lg2[MS];
map<LL,LL> mp;

// "X 年是自 Y 年以来降雨量最多的"  
// 它的含义是 X 年的降雨量不超过 Y 年,且对于任意 Y<Z<X,Z 年的降雨量严格小于 X 年。
// 即有一个序列,最左边x的位置降雨量>=最右边y的降雨量,且对于 x < i < y,所有i位置降雨量<位置y 

void init_lg2(){
	lg2[1] = 0;
	for(int i=2;i<=n;i++) lg2[i] = lg2[i/2]+1;
}

void init_st(){
	for(int i=1;i<=lg2[n];i++){
		for(int j=1;j+(1<<(i-1))<=n;j++){
			p[j][i] = max(p[j][i-1],p[j+(1<<(i-1))][i-1]);
		}
	}
}

LL get_max(LL l,LL r){
	LL t = lg2[r-l+1];
	return max(p[l][t],p[r-(1<<t)+1][t]);
}

int main(){
	ios::sync_with_stdio(false);
	cin >> n;
	init_lg2();
	for(int i=1;i<=n;i++){
		cin >> ha[i] >> p[i][0];
		mp[ha[i]] = 1;
	}
	init_st(); // 预处理 st 表 
	cin >> m;
	while(m--){
		LL x,y;
		cin >> x >> y;
		LL l = lower_bound(ha+1,ha+n+1,x) - ha;
		LL r = lower_bound(ha+1,ha+n+1,y) - ha;
		LL cl = get_max(l,l); // 最左边降雨量 
		LL cr = get_max(r,r); // 最右边降雨量 
		//if(l == r) cout << "true\n"; // l == r 时竟然不是 true 
		if(l > r) cout << "false\n";
		else if(!mp[x] && !mp[y]){ // 当两个年份降雨量都未知 
			cout << "maybe\n";
		}
		else if(mp[x] && !mp[y]){ // 已知左边的降雨量 
			LL t = get_max(l+1,r-1);
			if(t >= cl && r-1>=l+1) cout << "false\n";
			else cout << "maybe\n";
		}
		else if(!mp[x] && mp[y]){  // 已知右边的降雨量 
			LL t = get_max(l,r-1);
			if(t >= cr && r-1>=l) cout << "false\n";
			else cout << "maybe\n";
		}
		else if(mp[x] && mp[y]){ // 两边都知 
			if(cr > cl) cout << "false\n";
			else{
				if(y-x == r-l){ // 从左到右每一年都已知 
					if(r == l+1) cout << "true\n";
					else{
						LL t = get_max(l+1,r-1);
						if(t >= cr) cout << "false\n";
						else cout << "true\n";
					}
				}
				else{ // 从左到右有未知 
					if(r == l+1) cout << "maybe\n";
					else{
						LL t = get_max(l+1,r-1);
						if(t >= cr) cout << "false\n";
						else cout << "maybe\n";
					}
				}
			}
		}
		else{
			printf("----\n");
		}
	}
	
	

	return 0;
}
posted @ 2021-03-16 11:02  棉被sunlie  阅读(61)  评论(0)    收藏  举报