結果

問題 No.885 アマリクエリ
ユーザー risujirohrisujiroh
提出日時 2019-09-13 22:55:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 856 ms / 2,000 ms
コード長 4,697 bytes
コンパイル時間 1,715 ms
コンパイル使用メモリ 174,816 KB
実行使用メモリ 44,556 KB
最終ジャッジ日時 2023-09-17 15:04:16
合計ジャッジ時間 5,395 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
44,336 KB
testcase_01 AC 772 ms
44,392 KB
testcase_02 AC 113 ms
44,316 KB
testcase_03 AC 45 ms
44,340 KB
testcase_04 AC 45 ms
44,392 KB
testcase_05 AC 39 ms
44,316 KB
testcase_06 AC 33 ms
44,396 KB
testcase_07 AC 67 ms
44,352 KB
testcase_08 AC 29 ms
44,348 KB
testcase_09 AC 856 ms
44,328 KB
testcase_10 AC 13 ms
44,380 KB
testcase_11 AC 13 ms
44,280 KB
testcase_12 AC 13 ms
44,352 KB
testcase_13 AC 18 ms
44,536 KB
testcase_14 AC 17 ms
44,392 KB
testcase_15 AC 18 ms
44,256 KB
testcase_16 AC 14 ms
44,556 KB
testcase_17 AC 14 ms
44,404 KB
testcase_18 AC 14 ms
44,340 KB
testcase_19 AC 14 ms
44,384 KB
testcase_20 AC 14 ms
44,488 KB
testcase_21 AC 14 ms
44,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

template<class Z> Z rng(Z a, Z b) {
  static mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
  return uniform_int_distribution<Z>(a, b - 1)(mt);
}
namespace RBSTMap {
  constexpr int lim = 1 << 20;
  using Key = int;
  auto comp = greater<Key>();
  using T = int;
  constexpr T e = 0;
  T op(const T& a, const T& b) { return a + b; }
  T op(const T& a, const T& b, const T& c) { return op(op(a, b), c); }

  struct Node;
  using Tree = Node*;
  struct Node {
    Tree lch, rch;
    int sz, h;
    Key key;
    T val, acc = e;
  };
  Tree nil = new Node();

  Tree update(Tree t) {
    t->sz = t->lch->sz + 1 + t->rch->sz;
    t->h = max(t->lch->h, t->rch->h) + 1;
    t->acc = op(t->lch->acc, t->val, t->rch->acc);
    return t;
  }
  Node pool[lim];
  Tree make(const Key& k, const T& v, Tree l, Tree r) {
    static int p = 0;
    assert(p < lim);
    Tree t = pool + p++;
    t->lch = l, t->rch = r;
    t->key = k, t->val = v;
    return update(t);
  }

  Tree merge(Tree l, Tree r) {
    if (l == nil) return r;
    if (r == nil) return l;
    if (rng(0, l->sz + r->sz) < l->sz) {
      l->rch = merge(l->rch, r);
      return update(l);
    } else {
      r->lch = merge(l, r->lch);
      return update(r);
    }
  }
  Tree merge(Tree l, Tree m, Tree r) { return merge(merge(l, m), r); }
  pair<Tree, Tree> lower_split(Tree t, const Key& k) {
    if (t == nil) return {nil, nil};
    if (comp(t->key, k)) {
      Tree r;
      tie(t->rch, r) = lower_split(t->rch, k);
      return {update(t), r};
    } else {
      Tree l;
      tie(l, t->lch) = lower_split(t->lch, k);
      return {l, update(t)};
    }
  }
  pair<Tree, Tree> upper_split(Tree t, const Key& k) {
    if (t == nil) return {nil, nil};
    if (comp(k, t->key)) {
      Tree l;
      tie(l, t->lch) = upper_split(t->lch, k);
      return {l, update(t)};
    } else {
      Tree r;
      tie(t->rch, r) = upper_split(t->rch, k);
      return {update(t), r};
    }
  }
  tuple<Tree, Tree, Tree> split(Tree t, const Key& k) {
    Tree l, m, r;
    tie(l, r) = lower_split(t, k);
    tie(m, r) = upper_split(r, k);
    return make_tuple(l, m, r);
  }

  void insert(Tree& t, const Key& k, const T& v) {
    if (!rng(0, t->sz + 1)) {
      Tree l, r;
      tie(l, ignore, r) = split(t, k);
      t = make(k, v, l, r);
      return;
    }
    if (comp(k, t->key)) {
      insert(t->lch, k, v);
    } else if (comp(t->key, k)) {
      insert(t->rch, k, v);
    } else {
      t->key = k, t->val = v;
    }
    t = update(t);
  }
  void erase(Tree& t, const Key& k) {
    if (t == nil) return;
    if (comp(k, t->key)) {
      erase(t->lch, k);
      t = update(t);
    } else if (comp(t->key, k)) {
      erase(t->rch, k);
      t = update(t);
    } else {
      t = merge(t->lch, t->rch);
    }
  }
  int lower_bound(Tree t, const Key& k) {
    if (t == nil) return 0;
    if (comp(t->key, k)) return t->lch->sz + 1 + lower_bound(t->rch, k);
    return lower_bound(t->lch, k);
  }
  int upper_bound(Tree t, const Key& k) {
    if (t == nil) return 0;
    if (comp(k, t->key)) return upper_bound(t->lch, k);
    return t->lch->sz + 1 + upper_bound(t->rch, k);
  }
  int count(Tree t, const Key& k) { return upper_bound(t, k) - lower_bound(t, k); }

  pair<Key, T> get(Tree t, int i) {
    assert(0 <= i and i < t->sz);
    if (i == t->lch->sz) return {t->key, t->val};
    if (i < t->lch->sz) return get(t->lch, i);
    return get(t->rch, i - t->lch->sz - 1);
  }
  T at(Tree& t, const Key& k) {
    if (!count(t, k)) {
      insert(t, k, e);
      return e;
    }
    return get(t, lower_bound(t, k)).second;
  }
  T acc(Tree t, int l, int r) {
    if (l <= 0 and t->sz <= r) return t->acc;
    T resl = l < t->lch->sz ? acc(t->lch, l, r) : e;
    T resr = t->lch->sz + 1 < r ? acc(t->rch, l - t->lch->sz - 1, r - t->lch->sz - 1) : e;
    T resm = l <= t->lch->sz and t->lch->sz < r ? t->val : e;
    return op(resl, resm, resr);
  }
}

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n; cin >> n;
  auto mp = RBSTMap::nil;
  while (n--) {
    int a; cin >> a;
    insert(mp, a, at(mp, a) + 1);
  }
  lint res = 0;
  for (int i = 0; i < mp->sz; ++i) {
    auto e = get(mp, i);
    res += (lint)e.first * e.second;
  }
  int m; cin >> m;
  while (m--) {
    int x; cin >> x;
    while (true) {
      auto e = get(mp, 0);
      if (e.first < x) break;
      erase(mp, e.first);
      insert(mp, e.first % x, at(mp, e.first % x) + e.second);
      res -= (lint)(e.first - e.first % x) * e.second;
    }
    cout << res << '\n';
  }
}
0