結果

問題 No.1012 荷物収集
ユーザー kappybarkappybar
提出日時 2020-03-29 14:58:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 270 ms / 2,000 ms
コード長 1,720 bytes
コンパイル時間 1,674 ms
コンパイル使用メモリ 172,680 KB
実行使用メモリ 8,040 KB
最終ジャッジ日時 2023-08-30 18:43:51
合計ジャッジ時間 11,208 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 235 ms
7,896 KB
testcase_05 AC 233 ms
7,700 KB
testcase_06 AC 230 ms
7,744 KB
testcase_07 AC 230 ms
7,804 KB
testcase_08 AC 244 ms
7,892 KB
testcase_09 AC 237 ms
7,916 KB
testcase_10 AC 236 ms
7,732 KB
testcase_11 AC 236 ms
8,040 KB
testcase_12 AC 230 ms
7,856 KB
testcase_13 AC 240 ms
7,900 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 183 ms
5,376 KB
testcase_25 AC 241 ms
6,680 KB
testcase_26 AC 97 ms
6,472 KB
testcase_27 AC 22 ms
4,380 KB
testcase_28 AC 227 ms
7,224 KB
testcase_29 AC 79 ms
7,632 KB
testcase_30 AC 228 ms
7,888 KB
testcase_31 AC 120 ms
4,376 KB
testcase_32 AC 82 ms
4,376 KB
testcase_33 AC 144 ms
4,376 KB
testcase_34 AC 165 ms
4,376 KB
testcase_35 AC 187 ms
6,744 KB
testcase_36 AC 155 ms
4,380 KB
testcase_37 AC 77 ms
7,368 KB
testcase_38 AC 194 ms
5,928 KB
testcase_39 AC 75 ms
4,580 KB
testcase_40 AC 104 ms
4,736 KB
testcase_41 AC 270 ms
7,536 KB
testcase_42 AC 129 ms
4,848 KB
testcase_43 AC 158 ms
6,168 KB
testcase_44 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll INF = 1e18;
const int MOD = 1000000007;


int main() {
    int n,q;
    cin >> n >> q;
    vector<P> carry(n);
    rep(i,n) cin >> carry[i].first >> carry[i].second ;
    sort(carry.begin(),carry.end());
    vector<ll> left(n),right(n),left_w(n),right_w(n);
    ll now = 0;
    rep(i,n){
        if(i == 0){
            left[0] = 0;
            now += carry[i].second;
        }
        else{
            left[i] = left[i-1] + (carry[i].first - carry[i-1].first) * now;
            now += carry[i].second;
        }
        left_w[i] = now;
    }

    now = 0;
    for(int i=n-1;i>=0;i--){
        if(i == n-1){
            right[i] = 0;
            now += carry[i].second;
        }
        else{
            right[i] = right[i+1] + (carry[i+1].first - carry[i].first) * now;
            now += carry[i].second;
        }
        right_w[i] = now;
    }

    auto ok = [&](int i,ll j){
        if(i == n) return true;
        if(i == -1) return false;
        return carry[i].first >= j;
    };

    rep(i,q){
        ll x;
        cin >> x;
        int l = -1,r = n;
        while(r - l > 1){
            int m = (l + r)/2;
            if(ok(m,x)) r = m;
            else l = m;
        }
        ll res;
        if(r == n){
            res = left[n-1] + left_w[n-1] * (x - carry[n-1].first);
        }
        else if(l == -1){
            res = right[0] + right_w[0] * (carry[0].first - x);
        }
        else{
            res = left[l] + right[r] + left_w[l]*(x-carry[l].first) + right_w[r]*(carry[r].first-x);
        }
        cout << res << endl;
    }
    return 0;  
}
0