結果

問題 No.1012 荷物収集
ユーザー bal4ubal4u
提出日時 2020-03-21 16:31:22
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 1,847 bytes
コンパイル時間 535 ms
コンパイル使用メモリ 30,868 KB
実行使用メモリ 6,212 KB
最終ジャッジ日時 2023-08-24 18:04:32
合計ジャッジ時間 4,617 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 0 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 24 ms
6,016 KB
testcase_05 AC 25 ms
5,924 KB
testcase_06 AC 24 ms
6,212 KB
testcase_07 AC 24 ms
5,948 KB
testcase_08 AC 24 ms
6,144 KB
testcase_09 AC 25 ms
6,052 KB
testcase_10 AC 25 ms
6,016 KB
testcase_11 AC 25 ms
5,980 KB
testcase_12 AC 24 ms
6,048 KB
testcase_13 AC 25 ms
5,924 KB
testcase_14 AC 0 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 0 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 0 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 26 ms
4,380 KB
testcase_25 AC 33 ms
5,168 KB
testcase_26 AC 14 ms
4,612 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 31 ms
5,448 KB
testcase_29 AC 11 ms
5,988 KB
testcase_30 AC 30 ms
6,044 KB
testcase_31 AC 15 ms
4,380 KB
testcase_32 AC 12 ms
4,376 KB
testcase_33 AC 19 ms
4,380 KB
testcase_34 AC 21 ms
4,380 KB
testcase_35 AC 27 ms
5,168 KB
testcase_36 AC 20 ms
4,380 KB
testcase_37 AC 11 ms
5,668 KB
testcase_38 AC 26 ms
4,380 KB
testcase_39 AC 11 ms
4,376 KB
testcase_40 AC 14 ms
4,376 KB
testcase_41 AC 36 ms
6,032 KB
testcase_42 AC 17 ms
4,376 KB
testcase_43 AC 23 ms
4,708 KB
testcase_44 AC 1 ms
4,376 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');
}

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

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+1;
    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;

	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) {
		t[i].sw = t[i-1].sw + t[i].w;
		t[i].sxw = t[i-1].sxw + (ll)t[i].x * t[i].w;
	}
	while (Q--) {
		int X = in();
		i = bs(X);
		ll ans = (2*t[i].sw - t[N].sw) * X + (t[N].sxw - 2*t[i].sxw);
		out(ans);
	}
	return 0;
}
0