【题目链接】
【算法】
线段树
此题细节很多,写程序时要细心!
【代码】
#include<bits/stdc++.h> using namespace std; #define MAXN 50010 struct Node { int l,r,mx; } Tree[MAXN<<2]; int i,n,l,r,posl,posr,q,mx; int sum[MAXN],a[MAXN],y[MAXN]; template <typename T> inline void read(T &x) { int f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; } for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } template <typename T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x/10); putchar(x%10+'0'); } template <typename T> inline void writeln(T x) { write(x); puts(""); } inline void update(int index) { Tree[index].mx = max(Tree[index<<1].mx,Tree[index<<1|1].mx); } inline void build(int index,int l,int r) { int mid; Tree[index].l = l; Tree[index].r = r; if (l == r) { Tree[index].mx = a[l]; return; } mid = (l + r) >> 1; build(index<<1,l,mid); build(index<<1|1,mid+1,r); update(index); } inline int query_max(int index,int l,int r) { int mid; if (Tree[index].l == l && Tree[index].r == r) return Tree[index].mx; mid = (Tree[index].l + Tree[index].r) >> 1; if (mid >= r) return query_max(index<<1,l,r); else if (mid + 1 <= l) return query_max(index<<1|1,l,r); else return max(query_max(index<<1,l,mid),query_max(index<<1|1,mid+1,r)); } int main() { read(n); for (i = 1; i <= n; i++) { read(y[i]); read(a[i]); if (i == 1) sum[i] = 1; else sum[i] = sum[i-1] + (y[i] == y[i-1] + 1); } build(1,1,n); read(q); while (q--) { read(l); read(r); posl = lower_bound(y+1,y+n+1,l) - y; posr = lower_bound(y+1,y+n+1,r) - y; if (r < l) { puts("false"); continue; } if (posl == posr) { if (l == r) puts("true"); else puts("maybe"); continue; } if (posl + 1 == posr) { if (y[posr] == r && y[posl] != l && a[posr] <= a[posl]) puts("false"); else if (y[posl] != l || y[posr] != r) puts("maybe"); else if (y[posl] != l && y[posr] != r) puts("maybe"); else { if (a[posr] > a[posl]) puts("false"); else { if (l + 1 == r) puts("true"); else puts("maybe"); } } continue; } if (y[posl] == l && y[posr] == r) { if (sum[posr] - sum[posl] == r - l) { mx = query_max(1,posl+1,posr-1); if (a[posr] > mx && a[posr] <= a[posl]) puts("true"); else puts("false"); } else { mx = query_max(1,posl+1,posr-1); if (a[posr] <= mx || a[posr] > a[posl]) puts("false"); else puts("maybe"); } continue; } if (y[posl] == l) { mx = query_max(1,posl+1,posr-1); if (mx >= a[posl]) puts("false"); else puts("maybe"); continue; } if (y[posr] == r) { mx = query_max(1,posl,posr-1); if (mx >= a[posr]) puts("false"); else puts("maybe"); continue; } puts("maybe"); } return 0; }