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
| #include <iostream> #include <vector> #include <queue> #define maxn 100010 using namespace std;
#define lc i << 1 #define rc i << 1 | 1 struct Seg { int v, add; } T[maxn * 4]; inline void maintain(int i) { T[i].v = max(T[lc].v, T[rc].v); }
inline void Update(int i, int v) { T[i].v += v; T[i].add += v; }
inline void pushdown(int i) { int &add = T[i].add; if (!add) return ; Update(lc, add); Update(rc, add); add = 0; }
void update(int i, int l, int r, int L, int R, int v) { if (l > R || r < L) return ; if (L <= l && r <= R) return Update(i, v); int m = l + r >> 1; pushdown(i); update(lc, l, m, L, R, v); update(rc, m + 1, r, L, R, v); maintain(i); }
void update(int i, int l, int r, int k, int v) { if (l == r) return T[i].v = v, void(); int m = l + r >> 1; pushdown(i); if (k <= m) update(lc, l, m, k, v); else update(rc, m + 1, r, k, v); maintain(i); }
int query(int i, int l, int r, int L, int R, int v) { if (l > R || r < L || T[i].v < v) return 0; if (l == r) return T[i].v == v ? l : 0; pushdown(i); int m = l + r >> 1, t = query(rc, m + 1, r, L, R, v); if (t) return t; else return query(lc, l, m, L, R, v); }
int n, m, a[maxn], p[maxn]; vector<pair<int, int>> A[maxn];
pair<int, int> ans[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n; priority_queue<pair<int, int>> Q; for (int i = 1; i <= n; ++i) cin >> a[i], p[a[i]] = i; cin >> m; for (int i = 1, x, y; i <= m; ++i) cin >> x >> y, A[y].emplace_back(x, i); for (int i = 1; i <= n; ++i) { update(1, 1, n, i, i); if (a[i] > 1 && p[a[i] - 1] < i) update(1, 1, n, 1, p[a[i] - 1], 1); if (a[i] < n && p[a[i] + 1] < i) update(1, 1, n, 1, p[a[i] + 1], 1); for (auto t : A[i]) Q.push(t); while (!Q.empty()) { pair<int, int> t = Q.top(); int v = query(1, 1, n, 1, t.first, i); if (v == 0) break; Q.pop(); ans[t.second] = make_pair(v, i); } } for (int i = 1; i <= m; ++i) cout << ans[i].first << " " << ans[i].second << "\n"; return 0; }
|