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
| #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> using namespace std;
const int CN = 4e5 + 5;
int read(){ int s = 0,ne = 1; char c = getchar(); while(c < '0' || c > '9') ne = c == '-' ? -1 : 1, c = getchar(); while(c >= '0' && c <= '9') s = (s << 1) + (s << 3) + c - '0', c = getchar(); return s * ne; }
class SGT { public: int d[CN * 50], rt[CN], ch[CN * 50][2], idx; SGT() {idx = 0;} #define lc ch[u][0] #define rc ch[u][1] int make() {return ++idx;} void ins(int &u, int v, int l, int r, int p){ if(!u) u = make(); if(l == r) return (void)(d[u] = d[v] + 1); int m = (l + r) >> 1; if(p <= m) rc = ch[v][1], ins(lc, ch[v][0], l, m, p); else lc = ch[v][0], ins(rc, ch[v][1], m + 1, r, p); d[u] = d[lc] + d[rc]; } int qu(int u, int v, int l, int r){ if(l == r) return l; int m = (l + r) >> 1, s = d[lc] - d[ ch[v][0] ]; if(s < m - l + 1) return qu(lc, ch[v][0], l, m); else return qu(rc, ch[v][1], m + 1, r); } } D;
int n, q, ai;
int main() { freopen("_in.in", "r", stdin);
n = read(), q = read(); for(int i = 1; i <= n; i++) ai = read(), D.ins(D.rt[i], D.rt[i - 1], 1, n, ai + 1); int x, y; while(q--) x = read(), y = read(), printf("%d", y - x + 1 < n ? D.qu(D.rt[y], D.rt[x - 1], 1, n) - 1 : n), puts(""); }
|