結果

問題 No.1012 荷物収集
コンテスト
ユーザー fura
提出日時 2020-07-25 15:31:14
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 859 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,414 ms
コンパイル使用メモリ 223,044 KB
実行使用メモリ 13,440 KB
最終ジャッジ日時 2026-06-11 17:11:43
合計ジャッジ時間 7,839 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

int main(){
	int n,q; scanf("%d%d",&n,&q);
	vector<lint> x(n),w(n);
	rep(i,n) scanf("%lld%lld",&x[i],&w[i]);
	vector<lint> a(q);
	rep(i,q) scanf("%lld",&a[i]);

	vector<int> p(q);
	iota(p.begin(),p.end(),0);
	sort(p.begin(),p.end(),[&](int i,int j){ return a[i]<a[j]; });

	lint cost_L=0,cost_R=0,wsum_L=0,wsum_R=0;
	set<pair<lint,lint>> L,R;
	rep(i,n){
		R.emplace(x[i],w[i]);
		cost_R+=w[i]*x[i];
		wsum_R+=w[i];
	}
	vector<lint> ans(q);
	for(int i:p){
		while(!R.empty() && R.begin()->first<a[i]){
			auto [x0,w0]=*R.begin();
			R.erase(R.begin());
			cost_R-=w0*x0;
			wsum_R-=w0;
			L.emplace(x0,w0);
			cost_L+=w0*x0;
			wsum_L+=w0;
		}
		ans[i]=(cost_R-wsum_R*a[i])+(wsum_L*a[i]-cost_L);
	}

	rep(i,q) printf("%lld\n",ans[i]);

	return 0;
}
0