【线段树】洛谷 P2471 降雨量
2021-03-16 11:02:00 # ACM

题链

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#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;
}
Prev
2021-03-16 11:02:00 # ACM
Next