結果

問題 No.885 アマリクエリ
ユーザー tnakao0123tnakao0123
提出日時 2019-09-14 23:25:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 342 ms / 2,000 ms
コード長 1,538 bytes
コンパイル時間 794 ms
コンパイル使用メモリ 90,128 KB
実行使用メモリ 5,140 KB
最終ジャッジ日時 2023-09-21 03:02:07
合計ジャッジ時間 4,422 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 342 ms
4,924 KB
testcase_01 AC 131 ms
4,924 KB
testcase_02 AC 53 ms
5,140 KB
testcase_03 AC 53 ms
4,912 KB
testcase_04 AC 54 ms
4,912 KB
testcase_05 AC 39 ms
4,980 KB
testcase_06 AC 33 ms
4,956 KB
testcase_07 AC 19 ms
4,820 KB
testcase_08 AC 23 ms
4,376 KB
testcase_09 AC 129 ms
4,824 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 885.cc:  No.885 アマリクエリ - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;

template <typename T, const int MAX_N>
struct BIT {
  int n;
  T bits[MAX_N + 1];
  BIT() {}

  void init(int _n) {
    n = _n;
    //memset(bits, 0, sizeof(bits));
  }

  T sum(int x) {
    T s = 0;
    while (x > 0) {
      s += bits[x];
      x -= (x & -x);
    }
    return s;
  }

  void add(int x, T v) {
    while (x <= n) {
      bits[x] += v;
      x += (x & -x);
    }
  }
};

/* global variables */

BIT<ll,MAX_N> bit;

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);

  bit.init(n);
  priority_queue<pii> q;

  for (int i = 1; i <= n; i++) {
    int ai;
    scanf("%d", &ai);

    q.push(pii(ai, i));
    bit.add(i, ai);
  }

  int qn;
  scanf("%d", &qn);

  while (qn--) {
    int xi;
    scanf("%d", &xi);

    while (! q.empty() && q.top().first >= xi) {
      pii u = q.top(); q.pop();
      int &ai = u.first, &i = u.second;
      int bi = ai % xi;

      if (bi > 0) q.push(pii(bi, i));
      bit.add(i, bi - ai);
    }

    printf("%lld\n", bit.sum(n));
  }
  return 0;
}
0