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 85 86 87 88 89 90 91 92 93 94
| #include <bits/stdc++.h> #define int long long using namespace std;
const int N = 2e5 + 666, inf = 214748360000;
int n, m, num, res = inf; int a[N], b[N], pos[N], vis[N];
struct SEGMENTTREE { int minv; }tree[N << 2];
inline void up(int p) { int l = p << 1, r = (p << 1) | 1; return (void)(tree[p].minv = min(tree[l].minv, tree[r].minv)); }
inline void build(int p, int l, int r) { if (l == r) return (void)(tree[p].minv = pos[l]); int mid = (l + r) >> 1; build(p << 1, l, mid), build((p << 1) | 1, mid + 1, r); return (void)(up(p)); }
inline void updata(int p, int l, int r, int k, int val) { if (l == r) return (void)(tree[p].minv = val); int mid = (l + r) >> 1; if (k <= mid) updata(p << 1, l, mid, k, val); if (k > mid) updata((p << 1) | 1, mid + 1, r, k, val); return (void)(up(p)); }
inline int query(int p, int l, int r, int k) { if (l == r) return l; int mid = (l + r) >> 1; if (tree[p << 1].minv < k) return query(p << 1, l, mid, k); return query((p << 1) | 1, mid + 1, r, k); }
signed main() { int i; n = read(), m = read(), num = n; for (i = 1; i <= n; ++ i) { a[i] = read(); b[i] = a[i]; } sort(b + 1, b + n + 1); for (i = 1; i <= n; ++ i) { a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b; pos[a[i]] = i; } build(1, 1, 1e5);
for (i = 1; i <= m; ++ i) { char opt[66]; int x; scanf ("%s%lld", opt, &x); x = lower_bound(b + 1, b + n + 1, x) - b; if (opt[0] == 'A') { int y = query(1, 1, 1e5, pos[x]); if (b[y] == 0) put(-1); else put(b[y]); } else { pos[x] = num + 1; ++ num; updata(1, 1, 1e5, x, pos[x]); } } for (i = 1; i <= n; ++ i) vis[pos[a[i]]] = 1;
int ret = 0; for (i = 1; i <= 2e5; ++ i) { if (! vis[i]) ++ ret; if (i > n) if (! vis[i - n]) -- ret; if (i >= n) res = min(res, ret); } put(res); return 0; }
|