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 74 75 76 77 78
| ##include "bits/stdc++.h" using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
struct node { int x, id; }a[N];
bool cmp(node a, node b) { return a.x == b.x ? a.id > b.id : a.x > b.x; }
struct HJTTree { int l, r; int suml, sumr, sum; }t[N * 40]; int root[N], cnt;
inline void push_up(int u, int l, int r) { int mid = (l + r) / 2; t[u].suml = t[t[u].l].suml; if(t[t[u].l].suml == mid - l + 1) t[u].suml += t[t[u].r].suml; t[u].sumr = t[t[u].r].sumr; if(t[t[u].r].sumr == r - mid) t[u].sumr += t[t[u].l].sumr; t[u].sum = max(max(t[t[u].l].sum, t[t[u].r].sum), t[t[u].l].sumr + t[t[u].r].suml); }
void insert(int pre, int &now, int l, int r, int p) { t[now = ++cnt] = t[pre]; if(l == r) { t[now].sum = t[now].suml = t[now].sumr = 1; return ; } int m = (l + r) / 2; if(p <= m) insert(t[pre].l, t[now].l, l, m, p); else insert(t[pre].r, t[now].r, m + 1, r, p); push_up(now, l, r); }
int query(int u, int ql, int qr, int l, int r) { if(l > r) return 0; if(ql == l && r == qr) return t[u].sum; int ans = 0; int m = (l + r) / 2; if(qr <= m) ans = max(ans, query(t[u].l, ql, qr, l, m)); else if(ql > m) ans = max(ans, query(t[u].r, ql, qr, m + 1, r)); else { ans = max(query(t[u].l, ql, m, l, m), query(t[u].r, m + 1, qr, m + 1, r)); ans = max(ans, min(m - ql + 1, t[t[u].l].sumr) + min(qr - m, t[t[u].r].suml)); } return ans; }
void solve() { int n; cin >> n; for(int i = 1;i <= n; i++) cin >> a[i].x, a[i].id = i; sort(a + 1, a + n + 1, cmp); for(int i = 1;i <= n; i++) insert(root[i - 1], root[i], 1, n, a[i].id); int m; cin >> m; while(m--) { int ql, qr, w; cin >> ql >> qr >> w; int l = 1, r = n, ans = n; while(l <= r) { int mid = (l + r) / 2; if(query(root[mid], ql, qr, 1, n) >= w) ans = mid, r = mid - 1; else l = mid + 1; } cout << a[ans].x << endl; } }
signed main() { solve(); }
|