結果

問題 No.1012 荷物収集
ユーザー kawara_y_kyopro
提出日時 2020-03-20 22:38:53
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,257 bytes
コンパイル時間 1,221 ms
コンパイル使用メモリ 113,792 KB
実行使用メモリ 7,936 KB
最終ジャッジ日時 2024-12-15 07:15:42
合計ジャッジ時間 5,555 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(),v.end()
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) REP(sz, vec.size()) std::cerr<<vec[sz]<<(sz==vec.size()-1?'\n':' ');

constexpr long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, q; cin >> n >> q;
    V< P<ll, ll> > xw(n);
    for (ll i = 0; i < n; i++) cin >> xw[i].first >> xw[i].second;
    sort(ALL(xw));
    vll lwacc(n + 1, 0), rwacc(n + 1, 0);
    vll lcost(n, 0), rcost(n, 0);
    for (ll i = 0; i < n; i++) {
        lwacc[i + 1] = lwacc[i] + xw[i].second;
    }
    for (ll i = n - 1; i >= 0; i--) {
        rwacc[i] = rwacc[i + 1] + xw[i].second;
    }
    for (ll i = 1; i < n; i++) {
        lcost[i] = lcost[i - 1] + lwacc[i] * (xw[i].first - xw[i - 1].first);
    }
    for (ll i = n - 2; i >= 0; i--) {
        rcost[i] = rcost[i + 1] + rwacc[i + 1] * (xw[i + 1].first - xw[i].first);
    }

    while (q-->0) {
        ll x; cin >> x;
        ll idx = lower_bound(ALL(xw), make_pair(x, 0LL)) - xw.begin();
        if (xw[idx].first == x) {
            cout << lcost[idx] + rcost[idx] << '\n';
        } else {
            ll cost = lcost[max(0LL, idx - 1)] + rcost[idx] + (x - xw[max(0LL, idx - 1)].first) * lwacc[idx] + abs(xw[min(idx, n)].first - x) * rwacc[idx];
            cout << cost << '\n';
        }
    }
    return 0;
}
0