結果

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

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <typeinfo>
#include <numeric>
#include <functional>
#include <unordered_map>
#include <bitset>
#include <stack>
#include <assert.h>
#include <unordered_set>
#include <random>


using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

#define REP(i, n) for(ll i = 0; i < n; i++)














int main(){
    ll n, q;
    cin >> n >> q;
    vector<pair<ll, ll>> v(n);
    REP(i, n){
        cin >> v[i].first >> v[i].second;
    }
    sort(v.begin(), v.end());
    vector<ll> x(n), w(n);
    REP(i, n){
        x[i] = v[i].first;
        w[i] = v[i].second;
    }
    vector<ll> X(q);
    vector<pair<ll, ll>> Q;
    REP(i, q){
        cin >> X[i];
        Q.push_back({X[i], i});
    }
    REP(i, n){
        Q.push_back({x[i], -1 - i});
    }
    sort(Q.begin(), Q.end());
    /*
    cerr << "Q:";
    REP(i, Q.size()){
        cerr << Q[i].first << " " << Q[i].second << endl;
    }
    cerr << ":Q" << endl;
    */
    ll wl = 0, wr = 0, sum = 0;
    REP(i, n){
        wr += w[i];
        sum += x[i] * w[i];
    }
    // cerr << "wr: " << wr << " sum: " << sum << endl;
    ll pos = 0;
    vector<ll> ans(q);
    REP(i, Q.size()){
        ll next_pos = Q[i].first, idx = Q[i].second;
        ll d = next_pos - pos;
        sum -= d * wr;
        sum += d * wl;
        // cerr << "next_pos: " << next_pos << " d: " << d << " sum: " << sum << endl;
        if(idx >= 0){
            ans[idx] = sum;
        }
        else{
            wr -= w[-idx - 1];
            wl += w[-idx - 1];
        }
        pos = next_pos;
    }
    REP(i, q){
        cout << ans[i] << endl;
    }
}
0