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 79 80 81 82 83 84
| #include <bits/stdc++.h> using namespace std; const int N = 2000; int n, m; int q[4]; struct Node { int d, id; } a[N]; inline bool cmp(Node x, Node y) { return x.d < y.d; } struct node { int left, right; int sm, lm, rm; node *ch[2]; inline void upd() { sm = ch[0]->sm + ch[1]->sm; lm = max(ch[0]->lm, ch[0]->sm + ch[1]->lm); rm = max(ch[1]->rm, ch[1]->sm + ch[0]->rm); } } *rt[N], pool[N * 50], *cur = pool, *ans; inline void B (node *r, int left, int right) { r->left = left, r->right = right; if(left == right) { r->sm = r->lm = r->rm = 1; return ; } node *lson = cur++, *rson = cur++; int mid = (left + right) >> 1; r->ch[0] = lson, r->ch[1] = rson; B(lson, left, mid), B(rson, mid + 1, right); r->upd(); } inline void I (node *pre, node *now, int pos) { now->left = pre->left, now->right = pre->right; if(now->left == now->right) { now->sm = now->lm = now->rm = -1; return ; } int mid = (pre->left + pre->right) >> 1; if(pos <= mid) now->ch[1] = pre->ch[1], I(pre->ch[0], now->ch[0] = cur++, pos); if(pos > mid) now->ch[0] = pre->ch[0], I(pre->ch[1], now->ch[1] = cur++, pos); now->upd(); } inline node* Q (node *now, int l, int r) { if(now->left == l && now->right == r) return now; if(now->ch[0]->right >= r) return Q(now->ch[0], l, r); else if(now->ch[1]->left <= l) return Q(now->ch[1], l, r); else { node *ret = cur++, *L, *R; L = Q(now->ch[0], l, now->ch[0]->right); R = Q(now->ch[1], now->ch[1]->left, r); ret->sm = L->sm + R->sm; ret->lm = max(L->lm, L->sm + R->lm); ret->rm = max(R->rm, R->sm + L->rm); return ret; } } inline bool check(int id) { int sum = 0; if(q[2] + 1 <= q[3] - 1) sum += Q (rt[id - 1], q[2] + 1, q[3] - 1)->sm; sum += Q (rt[id - 1], q[1], q[2])->rm; sum += Q (rt[id - 1], q[3], q[4])->lm; return sum >= 0; } int main() { scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &a[i].d); a[i].id = i; } sort(a + 1, a + n + 1, cmp); B(rt[0] = cur++, 1, n); for(int i = 1; i <= n; i++) { rt[i] = cur++; I(rt[i - 1], rt[i], a[i].id); } int ans = 0; scanf("%d", &m); for(int i = 1; i <= m; i++) { for(int j = 1; j <= 4; j++) { scanf("%d", &q[j]), q[j] += ans, q[j] %= n; q[j]++; } sort(q + 1, q + 4 + 1); int l = 1, r = n; while(l <= r) { int mid = (l + r) / 2; if(check(mid)) l = mid + 1, ans = a[mid].d; else r = mid - 1; } printf("%d\n", ans); } return 0; }
|