#include #include using namespace std; struct S { int lmax, rmax, max, sum; S(int a = 0) { lmax = rmax = max = sum = a; } }; S op(S s, S t) { S res; res.lmax = max(s.lmax, s.sum + t.lmax); res.rmax = max(t.rmax, s.rmax + t.sum); res.max = max({s.max, t.max, s.rmax + t.lmax}); res.sum = s.sum + t.sum; return res; } S e() { return S(0); } int main() { int n, q; cin >> n >> q; vector a(n); for (int i = 0; i < n; i++) cin >> a[i], a[i]--; vector l(q), r(q); for (int i = 0; i < q; i++) cin >> l[i] >> r[i], l[i]--; vector> a_idx(n); for (int i = 0; i < n; i++) a_idx[a[i]].push_back(i); vector lo(q, -1), hi(q, n); vector> mi_num(n); for (int i = 0; i < q; i++) mi_num[(lo[i] + hi[i]) / 2].push(i); int ans_cnt = 0; while (ans_cnt < q) { atcoder::segtree seg(vector(n, 1)); for (int mi = 0; mi < n; mi++) { for (auto i : a_idx[mi]) seg.set(i, S(-1)); while (not mi_num[mi].empty()) { int i = mi_num[mi].front(); mi_num[mi].pop(); S s = seg.prod(l[i], r[i]); if (s.sum < 0 or s.sum - s.max + 1 < 0) hi[i] = mi; else lo[i] = mi; if (hi[i] - lo[i] > 1) mi_num[lo[i] + (hi[i] - lo[i]) / 2].push(i); else ans_cnt++; } } } for (int i = 0; i < q; i++) cout << hi[i] + 1 << "\n"; }