結果

問題 No.2546 Many Arithmetic Sequences
ユーザー KudeKude
提出日時 2023-11-24 23:03:59
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 4,116 bytes
コンパイル時間 4,053 ms
コンパイル使用メモリ 288,656 KB
実行使用メモリ 9,568 KB
最終ジャッジ日時 2023-11-25 20:45:24
合計ジャッジ時間 9,378 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 62 ms
6,676 KB
testcase_04 AC 139 ms
7,132 KB
testcase_05 AC 113 ms
6,676 KB
testcase_06 AC 21 ms
6,676 KB
testcase_07 AC 49 ms
6,676 KB
testcase_08 AC 15 ms
6,676 KB
testcase_09 AC 47 ms
6,676 KB
testcase_10 AC 86 ms
7,256 KB
testcase_11 AC 94 ms
8,356 KB
testcase_12 AC 39 ms
6,676 KB
testcase_13 AC 89 ms
6,676 KB
testcase_14 AC 84 ms
8,632 KB
testcase_15 AC 24 ms
6,676 KB
testcase_16 AC 80 ms
7,920 KB
testcase_17 AC 124 ms
6,676 KB
testcase_18 AC 67 ms
6,676 KB
testcase_19 AC 79 ms
6,676 KB
testcase_20 AC 45 ms
6,676 KB
testcase_21 AC 52 ms
6,676 KB
testcase_22 AC 98 ms
6,676 KB
testcase_23 AC 196 ms
8,504 KB
testcase_24 AC 197 ms
8,504 KB
testcase_25 AC 189 ms
8,524 KB
testcase_26 AC 192 ms
8,520 KB
testcase_27 AC 196 ms
8,516 KB
testcase_28 AC 152 ms
9,500 KB
testcase_29 AC 149 ms
9,072 KB
testcase_30 AC 151 ms
9,560 KB
testcase_31 AC 150 ms
9,496 KB
testcase_32 AC 148 ms
9,556 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 120 ms
9,568 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 123 ms
9,252 KB
testcase_37 AC 2 ms
6,676 KB
testcase_38 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
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>;

/**
 * @brief Convex Hull Trick Add Monotone
 * @docs docs/convex-hull-trick-add-monotone.md
*/
template< typename T, bool isMin >
struct ConvexHullTrickAddMonotone {
#define F first
#define S second
  using P = pair< T, T >;
  deque< P > H;

  ConvexHullTrickAddMonotone() = default;

  bool empty() const { return H.empty(); }

  void clear() { H.clear(); }

  inline int sgn(T x) { return x == 0 ? 0 : (x < 0 ? -1 : 1); }

  inline bool check(const P &a, const P &b, const P &c) {
    if(b.S == a.S || c.S == b.S)
      return sgn(b.F - a.F) * sgn(c.S - b.S) >= sgn(c.F - b.F) * sgn(b.S - a.S);
    //return (b.F-a.F)*(c.S-b.S) >= (b.S-a.S)*(c.F-b.F);
    if(is_integral< T >::value) {
      return (b.S - a.S) / (a.F - b.F) >= (c.S - b.S) / (b.F - c.F);
    } else {
      return
          (b.F - a.F) * sgn(c.S - b.S) / abs(b.S - a.S) >=
          (c.F - b.F) * sgn(b.S - a.S) / abs(c.S - b.S);
    }
  }

  void add(T a, T b) {
    if(!isMin) a *= -1, b *= -1;
    P line(a, b);
    if(empty()) {
      H.emplace_front(line);
      return;
    }
    if(H.front().F <= a) {
      if(H.front().F == a) {
        if(H.front().S <= b) return;
        H.pop_front();
      }
      while(H.size() >= 2 && check(line, H.front(), H[1])) H.pop_front();
      H.emplace_front(line);
    } else {
      assert(a <= H.back().F);
      if(H.back().F == a) {
        if(H.back().S <= b) return;
        H.pop_back();
      }
      while(H.size() >= 2 && check(H[H.size() - 2], H.back(), line)) H.pop_back();
      H.emplace_back(line);
    }
  }

  inline T get_y(const P &a, const T &x) {
    return a.F * x + a.S;
  }

  T query(T x) {
    assert(!empty());
    int l = -1, r = H.size() - 1;
    while(l + 1 < r) {
      int m = (l + r) >> 1;
      if(get_y(H[m], x) >= get_y(H[m + 1], x)) l = m;
      else r = m;
    }
    if(isMin) return get_y(H[r], x);
    return -get_y(H[r], x);
  }

  T query_monotone_inc(T x) {
    assert(!empty());
    while(H.size() >= 2 && get_y(H.front(), x) >= get_y(H[1], x)) H.pop_front();
    if(isMin) return get_y(H.front(), x);
    return -get_y(H.front(), x);
  }

  T query_monotone_dec(T x) {
    assert(!empty());
    while(H.size() >= 2 && get_y(H.back(), x) >= get_y(H[H.size() - 2], x)) H.pop_back();
    if(isMin) return get_y(H.back(), x);
    return -get_y(H.back(), x);
  }

#undef F
#undef S
};

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<P> dapos, daneg;
  rep(i, n) {
    int a, d;
    cin >> a >> d;
    (d >= 0 ? dapos : daneg).emplace_back(d, a);
  }
  sort(all(dapos));
  ConvexHullTrickAddMonotone<ll, false> cht;
  for (auto [d, a] : dapos) {
    cht.add(d, 2 * a - d);
  }
  int sz = daneg.size();
  VL nxt(sz);
  for (int i = 0; auto [d, a] : daneg) {
    nxt[i] = a;
    i++;
  }
  auto cmp = [&](int i, int j) {
    return nxt[i] < nxt[j];
  };
  priority_queue<int, VI, decltype(cmp)> q(cmp);
  rep(i, sz) q.emplace(i);
  ll ans = -8e18;
  if (!cht.empty()) chmax(ans, cht.query_monotone_dec(m) * m / 2);
  if (sz) {
    ll now = 0;
    for (int i = 1; i <= m; i++) {
      int j = q.top();
      q.pop();
      now += nxt[j];
      nxt[j] += daneg[j].first;
      q.emplace(j);
      if (i == m) {
        chmax(ans, now);
      } else {
        if (!cht.empty()) chmax(ans, now + cht.query_monotone_dec(m - i) * (m - i) / 2);
      }
    }
  }
  cout << ans << '\n';
}
0