結果

問題 No.1012 荷物収集
ユーザー rxoxixyxdrxoxixyxd
提出日時 2020-03-21 00:30:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 304 ms / 2,000 ms
コード長 1,732 bytes
コンパイル時間 1,917 ms
コンパイル使用メモリ 176,452 KB
実行使用メモリ 14,188 KB
最終ジャッジ日時 2023-08-22 04:29:20
合計ジャッジ時間 12,732 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 259 ms
14,008 KB
testcase_05 AC 261 ms
14,016 KB
testcase_06 AC 266 ms
13,968 KB
testcase_07 AC 262 ms
13,964 KB
testcase_08 AC 258 ms
13,968 KB
testcase_09 AC 262 ms
14,188 KB
testcase_10 AC 265 ms
14,024 KB
testcase_11 AC 261 ms
13,968 KB
testcase_12 AC 260 ms
14,128 KB
testcase_13 AC 259 ms
14,068 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 3 ms
4,384 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 3 ms
4,384 KB
testcase_18 AC 3 ms
4,384 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 202 ms
9,040 KB
testcase_25 AC 272 ms
12,192 KB
testcase_26 AC 131 ms
10,944 KB
testcase_27 AC 27 ms
5,220 KB
testcase_28 AC 267 ms
13,120 KB
testcase_29 AC 138 ms
13,832 KB
testcase_30 AC 273 ms
14,016 KB
testcase_31 AC 125 ms
4,384 KB
testcase_32 AC 87 ms
5,636 KB
testcase_33 AC 147 ms
5,332 KB
testcase_34 AC 164 ms
4,884 KB
testcase_35 AC 216 ms
11,960 KB
testcase_36 AC 155 ms
4,384 KB
testcase_37 AC 131 ms
13,128 KB
testcase_38 AC 215 ms
9,644 KB
testcase_39 AC 89 ms
7,044 KB
testcase_40 AC 109 ms
6,616 KB
testcase_41 AC 304 ms
13,752 KB
testcase_42 AC 140 ms
7,252 KB
testcase_43 AC 183 ms
10,680 KB
testcase_44 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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