提交时间:2024-08-28 19:39:37

运行 ID: 31987

#include <bits/stdc++.h> #define int long long #define pii pair<int,int> #define mkp make_pair #define push_back emplace_back using namespace std; inline int read() { int fl = 1, x; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') fl = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return fl * x; } int n, q, a[2000005], c[2][2000005], r[2000005]; inline int lb(int x) { return x & (-x); } inline void upd(int x, int y, int mod) { for (int i = x; i <= n; i += lb(i)) c[mod][i] += y; } inline int qry(int x, int mod) { int res = 0; for (int i = x; i >= 1; i -= lb(i)) res += c[mod][i]; return res; } inline int nxd() { for (int i = n; i >= 1; i--) { c[1][i] += qry(a[i] - 1, 0); r[i] += c[1][i]; upd(a[i], 1, 0); } } inline void build() { for (int i = 1; i <= n; i++) { if (i + lb(i) <= n) { c[1][i + lb(i)] += c[1][i]; } } } bool stolylorz[500005]; set <int> st; signed main() { scanf("%lld %lld", &n, &q); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); st.insert(i); } nxd(); build(); while (q--) { int x; scanf("%lld", &x); for (auto it = st.lower_bound(x); it != st.end(); it++) { if (a[*it] <= a[x]) { upd(*it, -r[*it], 1); auto it2 = it; it--; st.erase(it2); } } printf("%lld\n", qry(n, 1)); } return 0; }