[SCOI2007]降雨量

ST表,再大力讨论一下(因为lower_bound和upper_bound,WA了一次

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(5e4 + 10); IL ll Read(){
char c = '%'; ll x = 0, z = 1;
for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return x * z;
} int n, val[_], r[_], m, st[_][20], lg[_]; IL int Query(RG int x, RG int y){
if(x > y) return -1e9;
RG int l = lg[y - x + 1];
return max(st[x][l], st[y - (1 << l) + 1][l]);
} IL int Judge(RG int x, RG int y){
RG int fx = lower_bound(val + 1, val + n + 1, x) - val, fy = lower_bound(val + 1, val + n + 1, y) - val;
RG int bx = val[fx] == x, by = val[fy] == y, q;
if(bx){
if(by){
q = Query(fx + 1, fy - 1);
if(r[fx] < r[fy]) return 0;
if(q < r[fy]){
if(fy - fx == y - x) return 2;
return 1;
}
return 0;
}
else{
q = Query(fx + 1, fy - 1);
if(q < r[fx]) return 1;
return 0;
}
}
else{
if(by){
q = Query(fx, fy - 1);
if(q < r[fy]) return 1;
return 0;
}
else return 1;
}
} int main(RG int argc, RG char *argv[]){
n = Read();
for(RG int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
for(RG int i = 1; i <= n; i++) val[i] = Read(), r[i] = Read(), st[i][0] = r[i];
for(RG int j = 1; j <= lg[n]; j++)
for(RG int i = 1; i + (1 << j) - 1 <= n; i++)
st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
m = Read();
while(m--){
RG int y = Read(), x = Read(), flg = Judge(y, x);
if(flg == 0) puts("false");
else if(flg == 1) puts("maybe");
else puts("true");
}
return 0;
}
上一篇:初学者--bootstrap(一)----在路上(1)


下一篇:Java核心技术 卷1 基础知识-第一天