就是BZOJ3339的加强版。
加个离散化就行了。
(mex的离散化有点不一样
/* Footprints In The Blood Soaked Snow */ #include <cstdio> #include <map> #include <utility> #include <algorithm> using namespace std; const int maxn = 200005, maxm = maxn, inf = 0x3f3f3f3f; int n, m, A[maxn], cnt, num[maxn], next[maxn], last[maxn], sg[maxn], ans[maxm], tr[maxn << 2]; bool vis[maxn]; map<int, int> rank; struct _query { int id, l, r; bool operator < (const _query &a) const { return l < a.l; } } q[maxm]; inline int iread() { int f = 1, x = 0; char ch = getchar(); for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1; for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; return f * x; } inline void build(int p, int l, int r) { tr[p] = inf; if(l == r) { tr[p] = sg[l]; return; } int mid = l + r >> 1; build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r); } inline void pushdown(int p) { if(tr[p] != inf) { tr[p << 1] = min(tr[p], tr[p << 1]); tr[p << 1 | 1] = min(tr[p], tr[p << 1 | 1]); tr[p] = inf; } } inline void change(int p, int l, int r, int x, int y, int c) { if(x <= l && r <= y) { tr[p] = min(tr[p], c); return; } int mid = l + r >> 1; pushdown(p); if(x <= mid) change(p << 1, l, mid, x, y, c); if(y > mid) change(p << 1 | 1, mid + 1, r, x, y, c); } inline int query(int p, int l, int r, int x) { if(l == r && r == x) return tr[p]; int mid = l + r >> 1; pushdown(p); if(x <= mid) return query(p << 1, l, mid, x); else return query(p << 1 | 1, mid + 1, r, x); } inline int find(int x) { int l = 1, r = cnt; while(l <= r) { int mid = l + r >> 1; if(A[mid] < x) l = mid + 1; else r = mid - 1; } return l; } int main() { n = iread(); m = iread(); for(int i = 1; i <= n; i++) A[i] = num[i] = iread(); sort(A + 1, A + 1 + n); cnt = unique(A + 1, A + 1 + n) - (A + 1); for(int i = 1, k = 0; i <= n; i++) { int x = find(num[i]); vis[x] = 1; if(num[i] == k) while(vis[x]) { k++; if(A[++x] != k) break; } sg[i] = k; } for(int i = 1; i <= m; i++) { int l = iread(), r = iread(); q[i] = (_query){i, l, r}; } sort(q + 1, q + 1 + m); build(1, 1, n); for(int i = n; i >= 1; i--) next[i] = last[find(num[i])], last[find(num[i])] = i; for(int i = 1; i <= n; i++) if(!next[i]) next[i] = n + 1; for(int i = 1, pos = 1; i <= m; i++) { for(; pos < q[i].l; pos++) if(pos + 1 <= next[pos] - 1) change(1, 1, n, pos + 1, next[pos] - 1, num[pos]); ans[q[i].id] = query(1, 1, n, q[i].r); } for(int i = 1; i <= m; i++) printf("%d\n", ans[i]); return 0; }