「SCOI2007」降雨量

线段树

用map来记录年份是否存在

用线段树来维护 1 ~ n 内区间的最大值

设年份 x, y 之间的年份在已知年份中为 l ~ r, 其中最大值为top

讨论为 true 的条件 :
y - x - 1 = r - l + 1, map[x] = map[y] = 1, top < rain[r + 1] <= rain[l - 1]

讨论为 false 的条件:

1: map[y] = 1, top >= rain[r + 1]

2: map[x] = 1, top >= rain[l - 1]

3: map[x] = map[y], rain[l - 1] < rain[r + 1]

其它就为 maybe

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N = 1e5 + 5;
 5 
 6 int n, m, tree[N<<2], d[N], ra[N];
 7 
 8 void update(int o, int l, int r, int pos, int k) {
 9     if(l == r) {
10         tree[o] = k;
11         return;
12     }
13     int mid = (l + r)/2;
14     if(pos <= mid) update(o<<1, l, mid, pos, k);
15     else update(o<<1|1, mid + 1, r, pos, k);
16     tree[o] = max(tree[o<<1], tree[o<<1|1]);
17     return;
18 }
19 
20 int query(int o, int l, int r, int x, int y) {
21     if(x > y) return -1e9;
22     if(x <= l && r <= y) return tree[o];
23     int res = -1e9, mid = (l + r)/2;
24     if(x <= mid) res = max(res, query(o<<1, l, mid, x, y));
25     if(mid < y) res = max(res, query(o<<1|1, mid + 1, r, x, y));
26     return res;
27 }
28 
29 map <int, int> Map;
30 
31 int main() {
32     cin>>n;
33     for(int i = 1; i <= n; i++) {
34         cin>>d[i]>>ra[i];
35         update(1, 1, n, i, ra[i]);
36         Map[d[i]] = 1;
37     }
38     cin>>m;
39     for(int i = 1; i <= m; i++) {
40         int x, y;
41         cin>>x>>y;
42         int l = upper_bound(d + 1, d + n + 1, x) - d;
43         int r = lower_bound(d + 1, d + n + 1, y) - d - 1;
44         if(x >= y) {
45             cout<<false<<endl;
46             continue;
47         }
48         int top = query(1, 1, n, l, r);
49         if(Map[y] && ra[r + 1] > top && Map[x] && ra[r + 1] <= ra[l - 1] && y - x - 1 == r - l + 1) {
50             cout<<"true"<<endl;
51             continue;
52         }
53         if(Map[y] && top >= ra[r + 1]) {
54             cout<<"false"<<endl;
55             continue;
56         }
57         if(Map[x] && Map[y] && ra[l - 1] < ra[r + 1]) {
58             cout<<"false"<<endl;
59             continue;
60         }
61         if(Map[x] && top >= ra[l - 1]) {
62             cout<<"false"<<endl;
63             continue;
64         }
65         cout<<"maybe"<<endl;
66     }
67     return 0;
68 }
View Code

猜你喜欢

转载自www.cnblogs.com/ympc2005/p/12365181.html