結果
| 問題 | 
                            No.2501 Maximum Inversion Number
                             | 
                    
| コンテスト | |
| ユーザー | 
                             emthrm
                         | 
                    
| 提出日時 | 2023-07-13 14:08:20 | 
| 言語 | C++23  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 570 ms / 2,000 ms | 
| コード長 | 2,986 bytes | 
| コンパイル時間 | 1,442 ms | 
| コンパイル使用メモリ | 127,176 KB | 
| 実行使用メモリ | 7,296 KB | 
| 最終ジャッジ日時 | 2024-09-15 13:55:39 | 
| 合計ジャッジ時間 | 5,165 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 17 | 
ソースコード
#include <cassert>
#include <cstdint>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <utility>
#include <vector>
std::int64_t NChoose2(const int n) { return std::int64_t{n} * (n - 1) / 2; }
// <AC>
// アルゴリズムのシミュレーション
std::int64_t Solve(
    int m, const std::vector<int>& l, const std::vector<int>& r) {
  const int n = l.size();
  assert(std::ssize(r) == n);
  std::int64_t ans = NChoose2(m);
  // (現在の c_x の値, 多重集合 {r_x | c_x = キーの値})
  using PriorityQueue =
      std::priority_queue<int, std::vector<int>, std::greater<int>>;
  std::map<int, PriorityQueue> c;
  for (int i = 0; i < n; ++i) {
    m -= l[i];
    if (m < 0) return -1;
    ans -= NChoose2(l[i]);
    if (l[i] < r[i]) c[l[i]].emplace(r[i]);
  }
  if (m == 0) return ans;
  // アルゴリズムをシミュレートする
  while (m > 0 && !c.empty()) {
    PriorityQueue que;
    que.swap(c.begin()->second);
    const int current_c = c.begin()->first;
    c.erase(c.begin());
    ans += NChoose2(current_c) * std::ssize(que);
    const int next_v = (c.empty() || que.top() < c.begin()->first ?
                        que.top() : c.begin()->first);
    const std::int64_t next_len = std::ssize(que) * (next_v - current_c);
    if (next_len >= m) {  // 現在見ている数の近くに t0 がある
      const int t0 =
          current_c + (m + std::ssize(que) - 1) / std::ssize(que) - 1;
      assert(current_c <= t0 && t0 < next_v);
      const int num_of_t0 = (t0 + 1 - current_c) * std::ssize(que) - m;
      m -= (t0 - current_c) * num_of_t0;
      m -= (t0 + 1 - current_c) * (std::ssize(que) - num_of_t0);
      assert(m == 0);
      ans -= NChoose2(t0) * num_of_t0;
      ans -= NChoose2(t0 + 1) * (std::ssize(que) - num_of_t0);
      return ans;
    }
    // 全体を持ち上げる
    m -= next_len;
    ans -= NChoose2(next_v) * std::ssize(que);
    while (!que.empty() && que.top() == next_v) que.pop();
    if (!que.empty()) {
      if (c.empty() || next_v < c.begin()->first) {
        assert(c.emplace(next_v, que).second);  // c の先頭に加える
      } else {
        if (que.size() > c.begin()->second.size()) {
          std::swap(que, c.begin()->second);
        }
        // マージ
        for (; !que.empty(); que.pop()) {
          c.begin()->second.emplace(que.top());
        }
      }
    }
  }
  return -1;
}
int main() {
  constexpr int kMaxT = 200000, kMaxN = 200000, kMaxM = 1000000000;
  int t;
  std::cin >> t;
  assert(1 <= t && t <= kMaxT);
  while (t--) {
    int n, m;
    std::cin >> n >> m;
    assert(1 <= n && n <= kMaxN && 1 <= m && m <= kMaxM);
    std::vector<int> l(n);
    for (int i = 0; i < n; ++i) {
      std::cin >> l[i];
    }
    std::vector<int> r(n);
    for (int i = 0; i < n; ++i) {
      std::cin >> r[i];
      assert(0 <= l[i] && l[i] <= r[i] && r[i] <= kMaxM);
    }
    std::cout << Solve(m, l, r) << '\n';
  }
  return 0;
}
            
            
            
        
            
emthrm