結果

問題 No.1012 荷物収集
ユーザー rxoxixyxd
提出日時 2020-03-21 00:30:55
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 285 ms / 2,000 ms
コード長 1,732 bytes
コンパイル時間 2,011 ms
コンパイル使用メモリ 177,672 KB
実行使用メモリ 14,484 KB
最終ジャッジ日時 2024-12-15 22:29:18
合計ジャッジ時間 10,980 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

typedef long long ll;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
const int INF = 1e9;
const long long INFL = 1LL<<60;


int main()
{
  int n, q;
  cin >> n >> q;

  map<ll, ll> xw;
  rep(i, n) {
    ll x, w;
    cin >> x >> w;
    xw[x] += w;
  }

  int N = xw.size();

  vector<ll> x;
  vector<ll> w;
  for (auto& v : xw) {
    x.push_back(v.first);
    w.push_back(v.second);
  }

  vector<ll> lc(N, 0), lw(N, 0);
  vector<ll> rc(N, 0), rw(N, 0);
  rep(i, N) {
    lw[i] += w[i];
    if (i > 0) {
      lw[i] += lw[i-1];
      lc[i] = lc[i-1] + (x[i] - x[i-1]) * lw[i-1];
    }
  }
  for (int i = N-1; i >= 0; i--) {
    rw[i] += w[i];
    if (i < N-1) {
      rw[i] += rw[i+1];
      rc[i] = rc[i+1] + (x[i+1] - x[i]) * rw[i+1];
    }
  }

  rep(i, q) {
    ll ans = 0;
    ll X;
    cin >> X;
    auto itr = lower_bound(ALL(x), X);
    int idx = itr - x.begin();
    if (x[N-1] <= X) {
      ans = lc[N-1] + (X - x[N-1]) * lw[N-1];
    } else if (X <= x[0]) {
      ans = rc[0] + (x[0] - X) * rw[0];
    } else if (x[idx] == X) {
      ans = lc[idx] + rc[idx];
    } else {
      ans += lc[idx-1] + (X - x[idx-1]) * lw[idx-1];
      ans += rc[idx] + (x[idx] - X) * rw[idx];
    }
    cout << ans << endl;
  }

  return 0;
}
0