結果
| 問題 |
No.1012 荷物収集
|
| コンテスト | |
| ユーザー |
bal4u
|
| 提出日時 | 2020-03-21 16:31:22 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 41 ms / 2,000 ms |
| コード長 | 1,847 bytes |
| コンパイル時間 | 642 ms |
| コンパイル使用メモリ | 32,000 KB |
| 実行使用メモリ | 6,272 KB |
| 最終ジャッジ日時 | 2024-12-23 02:32:38 |
| 合計ジャッジ時間 | 4,800 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 41 |
ソースコード
// 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;
}
bal4u