Advertisement
tiom4eg

some n^2

Jul 8th, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. signed main() {
  2.     // avx2 bless rng
  3.     fastIO;
  4.     int q, d; cin >> q >> d;
  5.     int ans = 0;
  6.     FOR(rq, 0, q) {
  7.         int x; cin >> x, --x;
  8.         if (is[x]) {
  9.             ans -= cnt[x] * 1LL * (cnt[x] - 1) / 2;
  10.             is[x] = 0, cnt[x] = 0;
  11.             FOR(i, x + 1, min(MAX, x + d + 1)) {
  12.                 ans -= cnt[i] * 1LL * (cnt[i] - 1) / 2;
  13.                 --cnt[i];
  14.                 ans += cnt[i] * 1LL * (cnt[i] - 1) / 2;
  15.             }
  16.         }
  17.         else {
  18.             is[x] = 1;
  19.             FOR(i, max(0, x - d), x) cnt[x] += is[i];
  20.             ans += cnt[x] * 1LL * (cnt[x] - 1) / 2;
  21.             FOR(i, x + 1, min(MAX, x + d + 1)) {
  22.                 ans -= cnt[i] * 1LL * (cnt[i] - 1) / 2;
  23.                 ++cnt[i];
  24.                 ans += cnt[i] * 1LL * (cnt[i] - 1) / 2;
  25.             }
  26.         }
  27.         cout << ans << endl;
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement