結果

問題 No.1012 荷物収集
ユーザー bal4ubal4u
提出日時 2020-03-21 16:50:19
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,867 bytes
コンパイル時間 1,387 ms
コンパイル使用メモリ 30,372 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-24 18:31:55
合計ジャッジ時間 6,012 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 0 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 23 ms
4,380 KB
testcase_05 AC 24 ms
4,388 KB
testcase_06 AC 24 ms
4,376 KB
testcase_07 AC 24 ms
4,380 KB
testcase_08 AC 24 ms
4,380 KB
testcase_09 AC 26 ms
4,384 KB
testcase_10 AC 23 ms
4,376 KB
testcase_11 AC 24 ms
4,376 KB
testcase_12 AC 23 ms
4,380 KB
testcase_13 AC 24 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 28 ms
4,376 KB
testcase_25 AC 38 ms
4,376 KB
testcase_26 AC 15 ms
4,384 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 33 ms
4,380 KB
testcase_29 AC 10 ms
4,380 KB
testcase_30 AC 32 ms
4,380 KB
testcase_31 AC 16 ms
4,376 KB
testcase_32 AC 13 ms
4,380 KB
testcase_33 AC 20 ms
4,384 KB
testcase_34 AC 24 ms
4,380 KB
testcase_35 AC 27 ms
4,380 KB
testcase_36 AC 20 ms
4,380 KB
testcase_37 AC 10 ms
4,376 KB
testcase_38 AC 29 ms
4,376 KB
testcase_39 AC 11 ms
4,376 KB
testcase_40 AC 14 ms
4,380 KB
testcase_41 AC 40 ms
4,380 KB
testcase_42 AC 17 ms
4,376 KB
testcase_43 AC 24 ms
4,376 KB
testcase_44 AC 0 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// yuki 1012 荷物収集
// 2020.3.21 bal4u

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef long long ll;

int getchar_unlocked(void);
int putchar_unlocked(int c);
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int in() {   // 非負整数の入力
	int n = 0; int c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

void out(ll n) { // 非負整数の表示(出力)
	int i; char b[30];

	if (!n) pc('0');
	else {
		//		if (n < 0) pc('-'), n = -n;
		i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

#define MAX 100005
// ソート
typedef struct { int x, w; } T;
T t[MAX]; int N;
ll sw[MAX], sxw[MAX];

void Usort(T *a, int n) {
	int i, k;
#define B 8
#define BMSK 255 // (1<<8)-1
	int t1[1 << B], t2[1 << B];

	T *tmp = malloc(n * sizeof(T));
	for (k = 0; k < 4; ++k) {  // for 32ビット
		memset(t1, 0, sizeof(t1)), memset(t2, 0, sizeof(t2));
		for (i = 0; i < n; ++i)	t1[(a[i].x >> k*B) & BMSK]++;
		for (i = 0; i < BMSK; ++i) t1[i+1] += t1[i];
		i = n; while (i--) tmp[--t1[(a[i].x >> k*B) & BMSK]] = a[i];
		k++;
		for (i = 0; i < n; ++i) t2[(tmp[i].x >> k*B) & BMSK]++;
		for (i = 0; i < BMSK; ++i) t2[i+1] += t2[i];
		i = n; while (i--) a[--t2[(tmp[i].x >> k*B) & BMSK]] = tmp[i];
	}
	free(tmp);
#undef B
}

// バイナリサーチ
int bs(int x) {
	int m, l = 0, r = N;
    while (l < r) {
        m = (l+r) >> 1;
        if (t[m].x <= x) l = m + 1; else r = m;
    }
	return l-1;
}

int main()
{
	int i, Q, X;

	N = in(), Q = in();
	for (i = 1; i <= N; ++i) t[i].x = in(), t[i].w = in();
	Usort(t+1, N);
	for (i = 1; i <= N; ++i) {
		sw[i] = sw[i-1] + t[i].w;
		sxw[i] = sxw[i-1] + (ll)t[i].x * t[i].w;
	}
	ll SW = sw[N], SXW = sxw[N++];
	while (Q--) {
		i = bs(X = in());
		out(((sw[i] << 1) - SW) * X + SXW - (sxw[i] << 1));
	}
	return 0;
}
0