奇怪的线段树姿势get。
看了hzwer的题解,发现这个奇怪的姿势。
权值也是标记...
/* Footprints In The Blood Soaked Snow */ #include <cstdio> #include <algorithm> using namespace std; const int maxn = 200005, maxm = maxn, inf = 0x3f3f3f3f; int n, m, num[maxn], next[maxn], last[maxn], sg[maxn], ans[maxm], tr[maxn << 2]; bool vis[maxn]; 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); } int main() { n = iread(); m = iread(); for(int i = 1, k = 0; i <= n; i++) { vis[num[i] = iread()] = 1; for(; vis[k]; k++); 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[num[i]], last[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; }