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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; typedef long long ll; const int N = 2e5 + 5; int q, d, cnt[N]; inline int lowbit(int x) { return x & -x; } inline void add(int x, int v) { while(x <= N - 5) { cnt[x] += v; x += lowbit(x); } } inline int sum(int x) { int ans = 0; while(x) ans += cnt[x], x -= lowbit(x); return ans; } #define ls (u << 1) #define rs ((u << 1) | 1) ll tr[N << 2], tag[N << 2], siz[N << 2]; inline void maintain(int u) { tr[u] = tr[ls] + tr[rs]; siz[u] = siz[ls] + siz[rs]; } inline void upd(int u, ll d) { tr[u] += d * siz[u]; tag[u] += d; } inline void pushdown(int u) { if(tag[u]) { upd(ls, tag[u]), upd(rs, tag[u]); tag[u] = 0; } } void update(int u, int l, int r, int ql, int qr, ll d) { if(r < ql || l > qr) return; if(ql <= l && r <= qr) return upd(u, d); int mid = (l + r) >> 1; pushdown(u); update(ls, l, mid, ql, qr, d), update(rs, mid + 1, r, ql, qr, d); maintain(u); } void edit(int u, int l, int r, int x) { if(r < x || l > x) return; if(l == r) { siz[u] ^= 1; tr[u] = siz[u] * sum(min(x + d, N - 5)); return; } int mid = (l + r) >> 1; pushdown(u); edit(ls, l, mid, x), edit(rs, mid + 1, r, x); maintain(u); } ll query(int u, int l, int r, int ql, int qr) { if(r < ql || l > qr) return 0; if(ql <= l && r <= qr) return tr[u]; int mid = (l + r) >> 1; pushdown(u); return query(ls, l, mid, ql, qr) + query(rs, mid + 1, r, ql, qr); } void debug(int u, int l, int r) { if(l == r) { printf("i=%d s[i+d]=%lld siz[i]=%lld\n", l, tr[u], siz[u]); return; } int mid = (l + r) >> 1; pushdown(u); debug(ls, l, mid), debug(rs, mid + 1, r); } int vis[N]; int main() { memset(vis, -1, sizeof(vis)); scanf("%d%d", &q, &d); ll res = 0; for(int i = 1; i <= q; i++) { int x; scanf("%d", &x); vis[x] *= -1; ll t = sum(min(x + d, N - 5)) - sum(x), t2; res += vis[x] * t * (t - 1) / 2; t = sum(x - 1) - sum(max(0, x - d - 1)); res += vis[x] * t * (t - 1) / 2; t = query(1, 1, N - 5, max(1, x - d + 1), x - 1); t2 = (ll)(sum(x - 1) - sum(max(x - d, 0))) * sum(x); res += vis[x] * (t - t2); printf("%lld\n", res); add(x, vis[x]); update(1, 1, N - 5, max(x - d, 1), N - 5, vis[x]); edit(1, 1, N - 5, x);
} return 0; }
|