結果

問題 No.1012 荷物収集
ユーザー ks2mks2m
提出日時 2020-03-20 22:36:26
言語 Java21
(openjdk 21)
結果
AC  
実行時間 993 ms / 2,000 ms
コード長 1,848 bytes
コンパイル時間 2,927 ms
コンパイル使用メモリ 76,764 KB
実行使用メモリ 61,308 KB
最終ジャッジ日時 2023-08-21 17:24:20
合計ジャッジ時間 25,940 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
34,128 KB
testcase_01 AC 51 ms
34,524 KB
testcase_02 AC 52 ms
34,276 KB
testcase_03 AC 52 ms
34,160 KB
testcase_04 AC 596 ms
58,188 KB
testcase_05 AC 593 ms
57,756 KB
testcase_06 AC 592 ms
58,316 KB
testcase_07 AC 587 ms
57,292 KB
testcase_08 AC 582 ms
57,804 KB
testcase_09 AC 564 ms
57,868 KB
testcase_10 AC 579 ms
58,104 KB
testcase_11 AC 586 ms
57,596 KB
testcase_12 AC 586 ms
58,012 KB
testcase_13 AC 593 ms
57,780 KB
testcase_14 AC 68 ms
34,928 KB
testcase_15 AC 86 ms
35,776 KB
testcase_16 AC 83 ms
34,768 KB
testcase_17 AC 81 ms
35,560 KB
testcase_18 AC 79 ms
35,108 KB
testcase_19 AC 68 ms
34,648 KB
testcase_20 AC 71 ms
34,776 KB
testcase_21 AC 75 ms
34,880 KB
testcase_22 AC 79 ms
35,120 KB
testcase_23 AC 71 ms
34,756 KB
testcase_24 AC 698 ms
54,744 KB
testcase_25 AC 957 ms
59,488 KB
testcase_26 AC 503 ms
46,764 KB
testcase_27 AC 256 ms
43,188 KB
testcase_28 AC 897 ms
54,928 KB
testcase_29 AC 505 ms
46,572 KB
testcase_30 AC 908 ms
57,256 KB
testcase_31 AC 545 ms
53,416 KB
testcase_32 AC 430 ms
47,528 KB
testcase_33 AC 592 ms
51,496 KB
testcase_34 AC 684 ms
52,728 KB
testcase_35 AC 730 ms
53,516 KB
testcase_36 AC 648 ms
54,008 KB
testcase_37 AC 487 ms
46,500 KB
testcase_38 AC 756 ms
54,988 KB
testcase_39 AC 423 ms
47,724 KB
testcase_40 AC 472 ms
48,936 KB
testcase_41 AC 993 ms
61,308 KB
testcase_42 AC 542 ms
49,876 KB
testcase_43 AC 636 ms
51,136 KB
testcase_44 AC 53 ms
34,192 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