結果

問題 No.2169 To Arithmetic
ユーザー 👑 emthrmemthrm
提出日時 2022-12-21 03:20:42
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 319 ms / 2,000 ms
コード長 3,506 bytes
コンパイル時間 3,329 ms
コンパイル使用メモリ 225,612 KB
実行使用メモリ 23,824 KB
最終ジャッジ日時 2023-08-11 10:39:50
合計ジャッジ時間 9,222 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 57 ms
7,296 KB
testcase_10 AC 60 ms
7,100 KB
testcase_11 AC 107 ms
9,992 KB
testcase_12 AC 36 ms
5,500 KB
testcase_13 AC 114 ms
9,504 KB
testcase_14 AC 61 ms
7,156 KB
testcase_15 AC 41 ms
6,048 KB
testcase_16 AC 60 ms
7,648 KB
testcase_17 AC 159 ms
13,128 KB
testcase_18 AC 70 ms
8,072 KB
testcase_19 AC 239 ms
15,736 KB
testcase_20 AC 232 ms
15,740 KB
testcase_21 AC 237 ms
15,736 KB
testcase_22 AC 240 ms
15,860 KB
testcase_23 AC 237 ms
15,892 KB
testcase_24 AC 198 ms
15,644 KB
testcase_25 AC 187 ms
13,992 KB
testcase_26 AC 319 ms
23,824 KB
testcase_27 AC 211 ms
15,892 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;

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();
  }

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

  vector<ll> ans(m, 0);
  REP(i, m) ans[i] = 1LL * num[i] * ds[i] - bit[i];
  vector<int> inc{0};
  FOR(i, 1, n) {
    if (a[inc.back()] < a[i]) inc.emplace_back(i);
  }
  map<int, vector<pair<int, int>>> mp;
  const auto add = [&](const int l_ind, const int r_ind) -> void {
    const int l = inc[l_ind], r = inc[r_ind];
    mp[(a[r] - a[l]) % (r - l) == 0 ? (a[r] - a[l]) / (r - l) + 1 : (a[r] - a[l] + r - l - 1) / (r - l)].emplace_back(l_ind, r_ind);
  };
  FOR(i, 1, inc.size()) add(i - 1, i);
  set<int> indices;
  REP(i, inc.size()) indices.emplace(i);
  REP(i, m) {
    while (!mp.empty() && mp.begin()->first <= ds[i]) {
      const vector<pair<int, int>> record = mp.begin()->second;
      mp.erase(mp.begin());
      for (const auto& [l_ind, r_ind] : record) {
        const auto it = indices.find(l_ind);
        if (it == indices.end()) continue;
        auto it2 = next(it);
        if (it2 == indices.end() || *it2 != r_ind) continue;
        it2 = indices.erase(it2);
        if (it2 != indices.end()) add(*it, *it2);
      }
    }
    ans[i] += a[inc[*indices.rbegin()]] - 1LL * ds[i] * inc[*indices.rbegin()];
  }
  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<ll> true_ans(q, 0);
  // REP(i, q) {
  //   vector<ll> b = a;
  //   REP(j, n) b[j] -= d[i] * j;
  //   const ll maxi = *max_element(ALL(b));
  //   REP(j, n) b[j] -= maxi;
  //   true_ans[i] += max(-b.front(), 0LL);
  //   FOR(j, 1, n) true_ans[i] += max(-(b[j] - b[j - 1]), 0LL);
  // }
  vector<int> neg, pos;
  for (const int d_i : set<int>(ALL(d))) {
    if (d_i < 0) {
      neg.emplace_back(-d_i);
    } else {
      pos.emplace_back(d_i);
    }
  }
  reverse(ALL(neg));
  const vector<ll> ans_pos = solve(a, pos);
  reverse(ALL(a));
  const vector<ll> ans_neg = solve(a, neg);
  REP(i, q) {
    const ll ans = (
        d[i] < 0 ?
        ans_neg[distance(neg.begin(), lower_bound(ALL(neg), -d[i]))] :
        ans_pos[distance(pos.begin(), lower_bound(ALL(pos), d[i]))]);
    // assert(ans == true_ans[i]);
    cout << ans << '\n';
  }
  return 0;
}
0