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
| #include <iostream> #include <cstdio> #include <algorithm> #include <vector> #define maxn 200010 #define pb push_back #define INF 1000000000 using namespace std;
inline int min(int a, int b, int c) { return min(a, min(b, c)); }
int n, m, a[maxn];
struct node { int id, p;
node() {} node(int _id, int _p) { id = _id; p = _p; } }; vector<node> A[maxn];
bool vis[maxn]; int nxt[maxn], b[maxn];
#define lc i << 1 #define rc i << 1 | 1 struct Seg { int v, tag; } T[maxn * 4]; int num[maxn]; void build(int i, int l, int r) { T[i].tag = INF; if (l == r) return (void) (T[i].v = num[l]); int m = l + r >> 1; build(lc, l, m); build(rc, m + 1, r); }
void update(int i, int l, int r, int L, int R, int v) { if (l > R || r < L) return ; if (L <= l && r <= R) return (void) (T[i].tag = min(T[i].tag, v)); int m = l + r >> 1; update(lc, l, m, L, R, v); update(rc, m + 1, r, L, R, v); }
int query(int i, int l, int r, int k, int tag) { if (l == r) return min(tag, T[i].tag, T[i].v); int m = l + r >> 1; if (k <= m) return query(lc, l, m, k, min(T[i].tag, tag)); else return query(rc, m + 1, r, k, min(T[i].tag, tag)); }
int ans[maxn]; int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), a[i] = min(n + 1, a[i]), b[a[i]] = n + 1; for (int i = n; i; --i) nxt[i] = b[a[i]], b[a[i]] = i; for (int i = 1; i <= m; ++i) { int x, y; scanf("%d%d", &x, &y); A[x].pb(node(i, y)); } for (int i = 1, now = 0; i <= n; ++i) { vis[a[i]] = 1; while (vis[now]) ++now; num[i] = now; } build(1, 1, n); for (int i = 1; i <= n; ++i) { for (auto u : A[i]) ans[u.id] = query(1, 1, n, u.p, INF); update(1, 1, n, i, nxt[i] - 1, a[i]); } for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); return 0; }
|