結果

問題 No.1012 荷物収集
ユーザー finefine
提出日時 2020-03-20 21:33:45
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 1,320 bytes
コンパイル時間 1,833 ms
コンパイル使用メモリ 178,244 KB
実行使用メモリ 11,412 KB
最終ジャッジ日時 2024-12-15 04:25:28
合計ジャッジ時間 6,128 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;

    vector<ll> x(n), w(n);
    vector<P> v;
    for (int i = 0; i < n; ++i) {
        cin >> x[i] >> w[i];
        v.emplace_back(x[i], i);
    }

    vector<ll> X(q);
    for (int i = 0; i < q; ++i) {
        cin >> X[i];
        v.emplace_back(X[i], i + n);
    }

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

    vector<ll> ans(q, 0);
    ll sum = 0, ws = 0, px = 0;
    for (P& p : v) {
        if (p.second >= n) {
            if (px > 0) sum += (p.first - px) * ws;
            px = p.first;
            ans[p.second - n] += sum;
        } else {
            if (px > 0) sum += (p.first - px) * ws;
            ws += w[p.second];
            px = p.first;
        }
    }

    sum = 0, ws = 0, px = 0;
    reverse(v.begin(), v.end());
    for (P& p : v) {
        if (p.second >= n) {
            if (px > 0) sum += (px - p.first) * ws;
            px = p.first;
            ans[p.second - n] += sum;
        } else {
            if (px > 0) sum += (px - p.first) * ws;
            ws += w[p.second];
            px = p.first;
        }
    }

    for (int i = 0; i < q; ++i) {
        cout << ans[i] << "\n";
    }
    return 0;
}
0