結果

問題 No.1012 荷物収集
ユーザー iqueue02
提出日時 2020-03-20 23:40:17
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 212 ms / 2,000 ms
コード長 749 bytes
コンパイル時間 1,604 ms
コンパイル使用メモリ 176,220 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-15 19:53:38
合計ジャッジ時間 9,006 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;

int main(){
  int n, q;
  cin >> n >> q;
  vector<pair<ll,ll>> v(n);
  rep(i,n) cin >> v[i].first >> v[i].second;

  sort(v.begin(), v.end());

  vector<ll> cumx(n+1,0), cumw(n+1,0);

  for (int i = 0; i < n; i++) {
    cumx[i + 1] = cumx[i] + v[i].second * v[i].first;
    cumw[i + 1] = cumw[i] + v[i].second;
  }

  while (q--) {
    ll x;
    cin >> x;
    auto idx = lower_bound(v.begin(), v.end(), make_pair(x, 0LL)) - v.begin();
    ll res = (x * cumw[idx] - cumx[idx]);
    res += (cumx[n] - cumx[idx]) - x * (cumw[n] - cumw[idx]);
    cout << res << endl;
  }

  return 0;
}
0