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
| #include <bits/stdc++.h> const int IN_LEN = 1000000, OUT_LEN = 1000000; inline int nextChar() { static char buf[IN_LEN], *h, *t; if (h == t) { t = (h = buf) + fread(buf, 1, IN_LEN, stdin); if (h == t) return -1; } return *h++; } template<class T> inline bool read(T &x) { static bool iosig = 0; static char c; for (iosig = 0, c = nextChar(); !isdigit(c); c = nextChar()) { if (c == -1) return false; if (c == '-') iosig = 1; } for (x = 0; isdigit(c); c = nextChar()) x = (x << 1) + (x << 3) + (c ^ '0'); if (iosig) x = -x; return true; } char obuf[OUT_LEN], *oh = obuf; inline void writeChar(const char c) { if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf; *oh++ = c; } template<class T> inline void write(T x) { static int buf[30], cnt; if (!x) writeChar(48); else { if (x < 0) writeChar('-'), x = -x; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48; while (cnt) writeChar(buf[cnt--]); } } inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); } const static int MAXN = 100005; typedef long long ll; int n, m, x, num, pos[MAXN]; ll ans; struct Node { Node *lc, *rc; int cnt; } node[MAXN * 90], *cur = node, *root[MAXN]; inline void insert(Node *&k, int l, int r, int x) { if (!k) k = ++cur; k->cnt++; if (l == r) return; register int mid = l + r >> 1; if (x <= mid) insert(k->lc, l, mid, x); else insert(k->rc, mid + 1, r, x); } inline void remove(Node *k, int l, int r, int x) { if (!k) return; k->cnt--; if (l == r) return; register int mid = l + r >> 1; if (x <= mid) remove(k->lc, l, mid, x); else remove(k->rc, mid + 1, r, x); } inline int query(Node *k, int l, int r, int s, int t) { if (!k) return 0; if (l == s && r == t) return k->cnt; register int mid = l + r >> 1; if (t <= mid) return query(k->lc, l, mid, s, t); else if (s > mid) return query(k->rc, mid + 1, r, s, t); else return query(k->lc, l, mid, s, mid) + query(k->rc, mid + 1, r, mid + 1, t); } inline int lowbit(int k) { return k & -k; } int main() { read(n), read(m); for (register int i = 1; i <= n; i++) { read(x), pos[x] = i; for (register int j = i; j <= n; j += lowbit(j)) insert(root[j], 1, n, x); if (x != n) for (register int j = i; j; j -= lowbit(j)) ans += (ll)query(root[j], 1, n, x + 1, n); } for (register int i = 1; i <= m; i++) { read(x), write(ans), writeChar('\n'); if (x != n) for (register int j = pos[x]; j; j -= lowbit(j)) ans -= (ll)query(root[j], 1, n, x + 1, n); if (x != 1) { for (register int j = n; j; j -= lowbit(j)) ans -= (ll)query(root[j], 1, n, 1, x - 1); for (register int j = pos[x] - 1; j; j -= lowbit(j)) ans += (ll)query(root[j], 1, n, 1, x - 1); } for (register int j = pos[x]; j <= n; j += lowbit(j)) remove(root[j], 1, n, x); } flush(); return 0; }
|