結果

問題 No.1012 荷物収集
ユーザー bal4u
提出日時 2020-03-21 16:50:19
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,867 bytes
コンパイル時間 527 ms
コンパイル使用メモリ 31,872 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-23 03:09:46
合計ジャッジ時間 4,815 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

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