結果

問題 No.2169 To Arithmetic
ユーザー KudeKude
提出日時 2022-12-21 10:24:45
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 2,377 bytes
コンパイル時間 4,064 ms
コンパイル使用メモリ 240,672 KB
実行使用メモリ 36,592 KB
最終ジャッジ日時 2023-08-11 10:44:22
合計ジャッジ時間 9,186 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 38 ms
7,656 KB
testcase_10 AC 100 ms
28,864 KB
testcase_11 AC 86 ms
18,172 KB
testcase_12 AC 73 ms
27,108 KB
testcase_13 AC 138 ms
32,216 KB
testcase_14 AC 64 ms
17,380 KB
testcase_15 AC 102 ms
28,092 KB
testcase_16 AC 67 ms
18,188 KB
testcase_17 AC 98 ms
18,092 KB
testcase_18 AC 53 ms
10,836 KB
testcase_19 AC 189 ms
32,976 KB
testcase_20 AC 189 ms
33,052 KB
testcase_21 AC 189 ms
32,912 KB
testcase_22 AC 186 ms
32,976 KB
testcase_23 AC 197 ms
33,056 KB
testcase_24 AC 145 ms
33,172 KB
testcase_25 AC 108 ms
19,120 KB
testcase_26 AC 140 ms
35,844 KB
testcase_27 AC 143 ms
36,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic pop
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

struct S { ll a, b; };
S op(S x, S y) { return S{x.a + y.a, x.b + y.b}; }
S e() { return S{0, 0}; }

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  VI a(n);
  rep(i, n) cin >> a[i];
  VI qs(q);
  rep(i, q) cin >> qs[i];
  auto convex_check = [](P p1, P p2, P p3) {
    int dx1 = p2.first - p1.first;
    int dy1 = p2.second - p1.second;
    int dx2 = p3.first - p2.first;
    int dy2 = p3.second - p2.second;
    return ll(dx2) * dy1 >= ll(dx1) * dy2;
  };
  vector<P> st;
  rep(i, n) {
    P p(i, a[i]);
    while(st.size() >= 2 && !convex_check(st.end()[-2], st.end()[-1], p)) {
      st.pop_back();
    }
    st.emplace_back(p);
  }
  VI b(n - 1);
  rep(i, n - 1) b[i] = a[i + 1] - a[i];
  // d, i, type
  vector<tuple<int, int, bool>> evs;
  rep(i, n - 1) {
    int b = a[i+1] - a[i];
    evs.emplace_back(b, i, 0);
  }
  rep(i, q) evs.emplace_back(qs[i], i, 1);
  sort(all(evs));
  vector<S> init_vec(n - 1);
  rep(i, n - 1) init_vec[i] = S{-1, b[i]};
  segtree<S, op, e> seg_pos(init_vec), seg_neg(n - 1);
  VL ans(q);
  int ptr = int(st.size()) - 1;
  for(auto [d, i, type]: evs) {
    if (type) {
      while(ptr) {
        int dx = st[ptr].first - st[ptr - 1].first;
        int dy = st[ptr].second - st[ptr - 1].second;
        // dy/dx >= d
        if (dy >= ll(d) * dx) break;
        ptr--;
      }
      int p = st[ptr].first;
      auto [a1, b1] = seg_pos.prod(0, p);
      auto [a2, b2] = seg_neg.prod(p, n - 1);
      ans[i] = (a1 - a2) * d + b1 - b2;
    } else {
      seg_pos.set(i, S{0, 0});
      seg_neg.set(i, S{-1, b[i]});
    }
  }
  rep(i, q) cout << ans[i] << '\n';
}
0