[SCOI2007]降雨量

嘟嘟嘟

这道题我第一眼看出就是RMQ,不过因为细节挺多的调了挺长时间。

st表正常预处理自不必说,主要是有没出现的年份怎么处理。

首先我们考虑false的情况:

1.左右端点年份都已知,且y的降水量小于x或者这中间最大的降水量大于x。

2.左端点年份已知,且中间的最大降水量大于等于左端点降水量。

3.右端点年份已知,且中间的最大降水量大于等于右端点降水量。

在出了上述false的情况考虑maybe:

1.左右端点年份只有一个已知。

2.两端点年份都已知,中间有的年份都已知(这时候只用判断,年分之差是否等于左右端点之差)。

那剩下的都是true啦。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<stack>
 8 #include<queue>
 9 #include<vector>
10 #include<cctype>
11 using namespace std;
12 #define space putchar(' ')
13 #define enter puts("")
14 #define Mem(a) memset(a, 0, sizeof(a))
15 typedef long long ll;
16 typedef double db;
17 const int INF = 0x3f3f3f3f;
18 const  db eps = 1e-8;
19 const int maxn = 5e4 + 5;
20 inline ll read()
21 {
22     ll ans = 0;
23     char ch = getchar(), last = ' ';
24     while(!isdigit(ch)) {last = ch; ch = getchar();}
25     while(isdigit(ch)) {ans = (ans << 3) + (ans << 1) + ch - '0'; ch = getchar();}
26     if(last == '-') ans = -ans;
27     return ans;
28 }
29 inline void write(ll x)
30 {
31     if(x < 0) putchar('-'), x = -x;
32     if(x >= 10) write(x / 10);
33     putchar(x % 10 + '0');
34 }
35 
36 int n, m, a[maxn], yr[maxn];
37 
38 int dp[maxn][25], b[maxn];
39 void rmq()
40 {
41     for(int i = 1; i <= n; ++i) dp[i][0] = a[i];
42     for(int j = 1; (1 << j) <= n; ++j)
43         for(int i = 1; i + (1 << j) - 1 <= n; ++i)
44             dp[i][j] = max(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
45     int x = 0;
46     for(int i = 1; i <= n; ++i)
47     {
48         b[i] = x;
49         if((1 << (x + 1)) <= i + 1) x++;
50     }
51 }
52 int query(int L, int R)
53 {
54     int k = b[R - L + 1];
55     return max(dp[L][k], dp[R - (1 << k) + 1][k]);
56 }
57 
58 int main()
59 {
60     n = read();
61     for(int i = 1; i <= n; ++i) yr[i] = read(), a[i] = read();
62     rmq();
63     m = read();
64     for(int i = 1; i <= m; ++i)
65     {
66         int y = read(), x = read();
67         if(x <= y) printf("false
");
68         else
69         {
70             int L = lower_bound(yr + 1, yr + n + 1, y) - yr;
71             int R = lower_bound(yr + 1, yr + n + 1, x) - yr;
72             bool fl = yr[L] == y, fr = yr[R] == x;
73             int ans = 0;
74             if(L + (fl ? 1 : 0) <= R - 1) ans = query(L + (fl ? 1 : 0), R - 1);    //别忘了这个判断啊…… 
75             if((fr && ans >= a[R]) || (fl && ans >= a[L]) || (fl && fr && (a[L] < a[R] || ans >= a[R]))) printf("false
");
76             else if(R - L != yr[R] - yr[L] || !fl || !fr) printf("maybe
");
77             else printf("true
");
78         }
79     }
80     return 0;
81 } 
View Code
原文地址:https://www.cnblogs.com/mrclr/p/9522701.html