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 72 73 74 75 76 77 78 79 80 81 82 83 84
| #include <cstdio> #include <vector> #define pb push_back using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type, class ...rest> void read (Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } const int N = 1e5 + 10, B = 350; int n, w[N], v[N], tot, id[N], L[N], R[N], tr[B][N]; vector <int> h[N]; void add (int x, int y) { for (int i = x; i <= tot; i += i & -i) for (int j = y + 1; j <= n + 1; j += j & -j) ++tr[i][j]; } int ask (int x, int y) { int res = 0; for (int i = x; i; i -= i & -i) for (int j = y + 1; j; j -= j & -j) res += tr[i][j]; return res; } int query (int l, int r, int a, int b) { if (l > r) return -1; int res = 0; if (id[l] == id[r]) for (int i = l; i <= r; ++i) res += v[i] <= a && w[i] <= b; else { for (int i = l; i <= R[id[l]]; ++i) res += v[i] <= a && w[i] <= b; res += ask(id[r] - 1, a) - ask(id[l], a); for (int i = L[id[r]]; i <= r; ++i) res += v[i] <= a && w[i] <= b; } return res; } int main () { read(n); for (int i = 1; i <= n; ++i) id[i] = (i - 1) / B + 1; tot = id[n]; for (int i = 1; i <= tot; ++i) L[i] = R[i - 1] + 1, R[i] = R[i - 1] + B; R[tot] = n; for (int i = 1; i <= n + 1; ++i) h[i].pb(0); for (int i = 1; i <= n; ++i) read(w[i]), v[i] = h[w[i]].back(), h[w[i]].pb(i); for (int i = 1; i <= n + 1; ++i) h[i].pb(n + 1); for (int i = 1; i <= n + 1; ++i) { bool flag = false; for (int j = 1; j < h[i].size(); ++j) flag |= query(h[i][j - 1] + 1, h[i][j] - 1, h[i][j - 1], i - 1) == i - 1; if (!flag) return write(i), 0; for (int j = 1; j + 1 < h[i].size(); ++j) add(id[h[i][j]], h[i][j - 1]); } write(n + 2); return 0; }
|