結果

問題 No.1012 荷物収集
ユーザー ks2mks2m
提出日時 2020-03-20 22:36:26
言語 Java21
(openjdk 21)
結果
AC  
実行時間 933 ms / 2,000 ms
コード長 1,848 bytes
コンパイル時間 2,731 ms
コンパイル使用メモリ 80,340 KB
実行使用メモリ 61,608 KB
最終ジャッジ日時 2024-05-08 22:53:20
合計ジャッジ時間 24,598 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
37,520 KB
testcase_01 AC 57 ms
37,668 KB
testcase_02 AC 59 ms
37,604 KB
testcase_03 AC 58 ms
37,448 KB
testcase_04 AC 585 ms
60,900 KB
testcase_05 AC 558 ms
60,656 KB
testcase_06 AC 565 ms
61,064 KB
testcase_07 AC 557 ms
60,040 KB
testcase_08 AC 569 ms
61,104 KB
testcase_09 AC 555 ms
60,428 KB
testcase_10 AC 573 ms
60,128 KB
testcase_11 AC 534 ms
60,480 KB
testcase_12 AC 563 ms
60,620 KB
testcase_13 AC 564 ms
61,144 KB
testcase_14 AC 71 ms
37,892 KB
testcase_15 AC 98 ms
38,700 KB
testcase_16 AC 75 ms
38,180 KB
testcase_17 AC 97 ms
38,232 KB
testcase_18 AC 81 ms
38,244 KB
testcase_19 AC 77 ms
38,072 KB
testcase_20 AC 75 ms
38,176 KB
testcase_21 AC 82 ms
38,144 KB
testcase_22 AC 80 ms
38,144 KB
testcase_23 AC 77 ms
38,188 KB
testcase_24 AC 690 ms
56,316 KB
testcase_25 AC 912 ms
61,608 KB
testcase_26 AC 481 ms
49,304 KB
testcase_27 AC 257 ms
45,552 KB
testcase_28 AC 829 ms
57,236 KB
testcase_29 AC 481 ms
49,636 KB
testcase_30 AC 863 ms
58,960 KB
testcase_31 AC 542 ms
55,496 KB
testcase_32 AC 414 ms
50,236 KB
testcase_33 AC 573 ms
53,740 KB
testcase_34 AC 630 ms
55,232 KB
testcase_35 AC 692 ms
56,356 KB
testcase_36 AC 619 ms
56,188 KB
testcase_37 AC 436 ms
49,080 KB
testcase_38 AC 686 ms
56,548 KB
testcase_39 AC 402 ms
48,800 KB
testcase_40 AC 458 ms
51,480 KB
testcase_41 AC 933 ms
59,216 KB
testcase_42 AC 520 ms
52,760 KB
testcase_43 AC 638 ms
53,488 KB
testcase_44 AC 59 ms
37,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int q = Integer.parseInt(sa[1]);
		Obj[] arr = new Obj[n];
		for (int i = 0; i < n; i++) {
			sa = br.readLine().split(" ");
			Obj o = new Obj();
			o.x = Integer.parseInt(sa[0]);
			o.w = Integer.parseInt(sa[1]);
			arr[i] = o;
		}
		sa = br.readLine().split(" ");
		Query[] qu = new Query[q];
		for (int i = 0; i < q; i++) {
			Query o = new Query();
			o.i = i;
			o.x = Integer.parseInt(sa[i]);
			qu[i] = o;
		}
		br.close();

		Arrays.parallelSort(arr, (o1, o2) -> o1.x - o2.x);
		Arrays.parallelSort(qu, (o1, o2) -> o1.x - o2.x);

		long right = 0;
		for (int i = 0; i < n; i++) {
			right += arr[i].w;
		}

		int pre = 0;
		int idx = 0;
		long left = 0;
		for (int i = 0; i < q; i++) {
			long val = left * (qu[i].x - pre);
			if (i > 0) {
				val += qu[i - 1].ans;
			}
			while (idx < n) {
				Obj o = arr[idx];
				if (o.x <= qu[i].x) {
					val += (qu[i].x - o.x) * o.w;
					if (i > 0) {
						val -= (o.x - qu[i - 1].x) * o.w;
					}
					left += o.w;
					right -= o.w;
					idx++;
				} else {
					break;
				}
			}
			if (i == 0) {
				for (int j = idx; j < n; j++) {
					val += (arr[j].x - qu[i].x) * arr[j].w;
				}
			} else {
				val -= right * (qu[i].x - pre);
			}
			qu[i].ans = val;
			pre = qu[i].x;
		}

		Arrays.parallelSort(qu, (o1, o2) -> o1.i - o2.i);
		PrintWriter pw = new PrintWriter(System.out);
		for (Query o : qu) {
			pw.println(o.ans);
		}
		pw.flush();
	}

	static class Obj {
		int x;
		long w;
	}

	static class Query {
		int i, x;
		long ans;
	}
}
0