結果

問題 No.2169 To Arithmetic
ユーザー 👑 emthrmemthrm
提出日時 2022-12-21 00:44:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,661 bytes
コンパイル時間 2,488 ms
コンパイル使用メモリ 217,100 KB
実行使用メモリ 17,468 KB
最終ジャッジ日時 2024-04-29 03:05:16
合計ジャッジ時間 8,580 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 50 ms
5,944 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 285 ms
17,464 KB
testcase_25 WA -
testcase_26 AC 275 ms
17,468 KB
testcase_27 AC 306 ms
17,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename Abelian>
struct FenwickTreeSupportingRangeAddQuery {
  explicit FenwickTreeSupportingRangeAddQuery(
      const int n_, const Abelian ID = 0)
      : n(n_ + 1), ID(ID) {
    data_const.assign(n, ID);
    data_linear.assign(n, ID);
  }

  void add(int left, const int right, const Abelian val) {
    if (right < ++left) return;
    for (int i = left; i < n; i += i & -i) {
      data_const[i] -= val * (left - 1);
      data_linear[i] += val;
    }
    for (int i = right + 1; i < n; i += i & -i) {
      data_const[i] += val * right;
      data_linear[i] -= val;
    }
  }

  Abelian sum(const int idx) const {
    Abelian res = ID;
    for (int i = idx; i > 0; i -= i & -i) {
      res += data_linear[i];
    }
    res *= idx;
    for (int i = idx; i > 0; i -= i & -i) {
      res += data_const[i];
    }
    return res;
  }

  Abelian sum(const int left, const int right) const {
    return left < right ? sum(right) - sum(left) : ID;
  }

  Abelian operator[](const int idx) const { return sum(idx, idx + 1); }

 private:
  const int n;
  const Abelian ID;
  std::vector<Abelian> data_const, data_linear;
};

vector<ll> solve(vector<ll> a, const vector<int>& ds) {
  const int m = ds.size();
  if (m == 0) return {};
  const int n = a.size();
  for (int i = n - 1; i >= 0; --i) {
    a[i] -= a.front();
  }

  FenwickTreeSupportingRangeAddQuery<ll> num(m), bit(m);
  FOR(i, 1, n) {
    const ll d = a[i] - a[i - 1];
    const int l = distance(ds.begin(), lower_bound(ALL(ds), d));
    num.add(l, m, 1);
    bit.add(l, m, d);
  }

  vector<ll> cnst(m, 0);
  vector<int> inc{0};
  FOR(i, 1, n) {
    if (a[inc.back()] < a[i]) inc.emplace_back(i);
  }
  REP(i, m) {
    while (inc.size() >= 2 && 1LL * ds[i] * (inc.back() - inc.end()[-2]) > a[inc.back()] - a[inc.end()[-2]]) inc.pop_back();
    cnst[i] += a[inc.back()] - 1LL * ds[i] * inc.back();
  }
  REP(i, n) a[i] -= a.back();
  inc = vector<int>{n - 1};
  for (int i = n - 2; i >= 0; --i) {
    if (a[inc.back()] < a[i]) inc.emplace_back(i);
  }

  vector<ll> ans(m, 0);
  REP(i, m) ans[i] = num[i] * ds[i] - bit[i] + cnst[i];
  return ans;
}

int main() {
  int n, q; cin >> n >> q;
  vector<ll> a(n); REP(i, n) cin >> a[i];
  vector<int> d(q); REP(i, q) cin >> d[i];
  vector<int> neg, pos;
  for (const int d_i : set<int>(ALL(d))) {
    (d_i < 0 ? neg : pos).emplace_back(d_i);
  }
  const vector<ll> ans_pos = solve(a, pos);
  reverse(ALL(a));
  reverse(ALL(neg));
  for (int& d_i : neg) d_i = -d_i;
  const vector<ll> ans_neg = solve(a, neg);
  REP(i, q) {
    if (d[i] < 0) {
      cout << ans_neg[distance(neg.begin(), lower_bound(ALL(neg), -d[i]))] << '\n';
    } else {
      cout << ans_pos[distance(pos.begin(), lower_bound(ALL(pos), d[i]))] << '\n';
    }
  }
  return 0;
}
0