結果

問題 No.1012 荷物収集
ユーザー merom686
提出日時 2020-03-20 22:18:42
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 954 bytes
コンパイル時間 967 ms
コンパイル使用メモリ 81,388 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-15 06:27:53
合計ジャッジ時間 5,239 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct P {
    bool operator<(const P& p) const {
        return x < p.x;
    }
    int x, w;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, q;
    cin >> n >> q;

    vector<P> p(n);
    for (int i = 0; i < n; i++) {
        int x, w;
        cin >> x >> w;
        p[i] = { x, w };
    }
    sort(p.begin(), p.end());

    vector<ll> s0(n + 1), w0(n + 1);
    for (int i = 0; i < n; i++) {
        s0[i + 1] = s0[i] + (ll)p[i].x * p[i].w;
        w0[i + 1] = w0[i] + p[i].w;
    }

    for (int _ = 0; _ < q; _++) {
        int x;
        cin >> x;

        int k = lower_bound(p.begin(), p.end(), P{ x, 0 }) - p.begin();
        ll r = -s0[k] + x * w0[k] + (s0[n] - s0[k]) - x * (w0[n] - w0[k]);
        cout << r << '\n';
    }

    return 0;
}
0