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
| #include <iostream> #define maxn 200010 #define INF 1000000000 using namespace std;
int n, m, a[maxn];
const int N = 1000000000; #define lc T[i].ch[0] #define rc T[i].ch[1] #define Lc T[j].ch[0] #define Rc T[j].ch[1] struct Zhuxi { int v, ch[2]; } T[maxn * 31]; int rt[maxn], top; void update(int &i, int j, int l, int r, int k, int v) { i = ++top; T[i] = T[j]; T[i].v += v; if (l == r) return ; int m = l + r >> 1; if (k <= m) update(lc, Lc, l, m, k, v); else update(rc, Rc, m + 1, r, k, v); }
int query(int i, int j, int l, int r, int L, int R) { if (l > R || r < L || T[j].v - T[i].v == 0) return 0; if (l == r) return T[j].v - T[i].v > 0 ? l : 0; int m = l + r >> 1, v = query(rc, Rc, m + 1, r, L, R); if (v) return v; else return query(lc, Lc, l, m, L, R); }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) update(rt[i], rt[i - 1], 1, N, a[i], 1); for (int i = 1; i <= m; ++i) { int x1, x2, y1, y2, v = INF, ans = 0; cin >> x1 >> y1 >> x2 >> y2; while (v > 1) { v = query(rt[x1 - 1], rt[y1], 1, N, 1, v); int res = query(rt[x2 - 1], rt[y2], 1, N, v / 2, v - 1); if (res) { ans = res + v; break; } v = query(rt[x2 - 1], rt[y2], 1, N, 1, v / 2 - 1); if (v) res = query(rt[x1 - 1], rt[y1], 1, N, v + 1, 2 * v + 1); if (res) { ans = res + v; break; } } cout << ans << "\n"; } return 0; }
|