結果

問題 No.1012 荷物収集
ユーザー 37zigen37zigen
提出日時 2020-03-20 22:52:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 800 ms / 2,000 ms
コード長 3,506 bytes
コンパイル時間 3,689 ms
コンパイル使用メモリ 75,580 KB
実行使用メモリ 69,104 KB
最終ジャッジ日時 2023-08-21 18:02:06
合計ジャッジ時間 25,344 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,428 KB
testcase_01 AC 42 ms
49,408 KB
testcase_02 AC 42 ms
49,336 KB
testcase_03 AC 42 ms
49,604 KB
testcase_04 AC 600 ms
67,388 KB
testcase_05 AC 605 ms
67,340 KB
testcase_06 AC 609 ms
67,528 KB
testcase_07 AC 596 ms
67,408 KB
testcase_08 AC 603 ms
67,260 KB
testcase_09 AC 590 ms
69,104 KB
testcase_10 AC 602 ms
67,668 KB
testcase_11 AC 609 ms
67,304 KB
testcase_12 AC 602 ms
67,472 KB
testcase_13 AC 600 ms
67,372 KB
testcase_14 AC 52 ms
49,504 KB
testcase_15 AC 86 ms
51,456 KB
testcase_16 AC 61 ms
50,320 KB
testcase_17 AC 87 ms
51,516 KB
testcase_18 AC 90 ms
49,536 KB
testcase_19 AC 53 ms
49,736 KB
testcase_20 AC 49 ms
49,452 KB
testcase_21 AC 59 ms
49,676 KB
testcase_22 AC 82 ms
51,976 KB
testcase_23 AC 64 ms
49,760 KB
testcase_24 AC 698 ms
63,140 KB
testcase_25 AC 788 ms
67,636 KB
testcase_26 AC 435 ms
61,556 KB
testcase_27 AC 191 ms
54,572 KB
testcase_28 AC 739 ms
67,620 KB
testcase_29 AC 288 ms
58,532 KB
testcase_30 AC 749 ms
68,008 KB
testcase_31 AC 546 ms
60,484 KB
testcase_32 AC 414 ms
58,484 KB
testcase_33 AC 619 ms
62,692 KB
testcase_34 AC 689 ms
63,444 KB
testcase_35 AC 691 ms
63,328 KB
testcase_36 AC 669 ms
63,288 KB
testcase_37 AC 307 ms
60,084 KB
testcase_38 AC 750 ms
65,680 KB
testcase_39 AC 373 ms
58,112 KB
testcase_40 AC 473 ms
60,068 KB
testcase_41 AC 800 ms
67,892 KB
testcase_42 AC 557 ms
60,052 KB
testcase_43 AC 617 ms
62,088 KB
testcase_44 AC 43 ms
49,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) throws FileNotFoundException {
		long t = System.currentTimeMillis();
		new Main().run();
		System.err.println(System.currentTimeMillis() - t);
	}
	
	Scanner sc = new Scanner();
	final long MOD=(long)1e9+7;
	
	void run() {
		int N=sc.nextInt();
		int Q=sc.nextInt();
		long[][] goods=new long[N][3];
		for(int i=0;i<N;++i) {
			goods[i][0]=i;
			goods[i][1]=sc.nextLong();// x
			goods[i][2]=sc.nextLong();// w
		}
		long[][] target=new long[Q][2];
		for(int i=0;i<Q;++i) {
			target[i][0]=i;
			target[i][1]=sc.nextLong();// x
		}
		Arrays.sort(target, new Comparator<long[]>() {
			@Override
			public int compare(long[] o1, long[] o2) {
				return Long.compare(o1[1], o2[1]);
			}
		});
		Arrays.sort(goods, new Comparator<long[]>() {
			@Override
			public int compare(long[] o1, long[] o2) {
				return Long.compare(o1[1], o2[1]);
			}
		});
		//最初x=0だとする。
		long leftW=0,rightW=0,cost=0;
		for(int i=0;i<N;++i) {
			rightW+=goods[i][2];
			cost+=goods[i][2]*goods[i][1];
		}
		int p=0;
		long[] ans=new long[Q];
		for(int i=0;i<Q;++i) {	
			cost+=leftW*(target[i][1]-(i>0?target[i-1][1]:0));
			cost-=rightW*(target[i][1]-(i>0?target[i-1][1]:0));
			while(p<goods.length&&goods[p][1]<=target[i][1]) {
				cost+=goods[p][2]*(target[i][1]-goods[p][1]);
				cost+=goods[p][2]*(target[i][1]-goods[p][1]);
				leftW+=goods[p][2];
				rightW-=goods[p][2];
				++p;
			}
			ans[(int)target[i][0]]=cost;
		}
		for(int i=0;i<ans.length;++i) {
			System.out.println(ans[i]);
		}
	}
		
	class Scanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;

		private boolean hasNextByte() {
			if (ptr < buflen) {
				return true;
			} else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) {
					return false;
				}
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[ptr++];
			else
				return -1;
		}

		private boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		private void skipUnprintable() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr]))
				ptr++;
		}

		public boolean hasNext() {
			skipUnprintable();
			return hasNextByte();
		}

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (isPrintableChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) {
				throw new NumberFormatException();
			}
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}

		public int nextInt() {
			return (int) nextLong();
		}
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0