結果

問題 No.1012 荷物収集
ユーザー Mister
提出日時 2020-05-02 02:02:31
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 1,235 bytes
コンパイル時間 1,415 ms
コンパイル使用メモリ 86,592 KB
最終ジャッジ日時 2025-01-10 05:42:53
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>

using lint = long long;

void solve() {
    int n, q;
    std::cin >> n >> q;

    std::vector<std::pair<lint, lint>> ps(n);
    for (auto& p : ps) std::cin >> p.first >> p.second;
    std::sort(ps.begin(), ps.end());

    std::vector<lint> xs(q);
    for (auto& x : xs) std::cin >> x;

    std::vector<int> qs(q);
    std::iota(qs.begin(), qs.end(), 0);
    std::sort(qs.begin(), qs.end(),
              [&](int i, int j) { return xs[i] < xs[j]; });

    lint psum = 0, bsum = 0,
         pw = 0, bw = 0;
    for (const auto& p : ps) {
        bsum += p.first * p.second;
        bw += p.second;
    }

    std::vector<lint> ans(q);
    int pi = 0;
    for (auto qi : qs) {
        lint x = xs[qi];

        while (pi < n && ps[pi].first < x) {
            auto sum = ps[pi].first * ps[pi].second;
            auto w = ps[pi].second;
            psum += sum, bsum -= sum;
            pw += w, bw -= w;
            ++pi;
        }

        ans[qi] = (pw * x - psum) + (bsum - bw * x);
    }

    for (auto a : ans) std::cout << a << std::endl;
}

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

    solve();

    return 0;
}
0