結果

問題 No.1012 荷物収集
ユーザー tenten
提出日時 2022-08-30 15:07:37
言語 Java
(openjdk 23)
結果
AC  
実行時間 694 ms / 2,000 ms
コード長 2,677 bytes
コンパイル時間 3,189 ms
コンパイル使用メモリ 77,968 KB
実行使用メモリ 58,192 KB
最終ジャッジ日時 2024-11-07 09:46:24
合計ジャッジ時間 23,472 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int q = sc.nextInt();
        Point[] points = new Point[n + q + 2];
        for (int i = 1; i <= n; i++) {
            points[i] = new Point(sc.nextInt(), sc.nextInt(), true);
        }
        for (int i = 0; i < q; i++) {
            points[i + n + 1] = new Point(sc.nextInt(), i, false);
        }
        points[0] = new Point(0, 0, true);
        points[n + q + 1] = new Point(Integer.MAX_VALUE, 0, true);
        Arrays.sort(points);
        long[] ans = new long[q];
        long total = 0;
        long weight = 0;
        for (int i = 1; i <= n + q; i++) {
            total += weight * (points[i].position - points[i - 1].position);
            if (points[i].isThing) {
                weight += points[i].value;
            } else {
                ans[points[i].value] += total;
            }
        }
        total = 0;
        weight = 0;
        for (int i = n + q; i >= 1; i--) {
            total += weight * (points[i + 1].position - points[i].position);
            if (points[i].isThing) {
                weight += points[i].value;
            } else {
                ans[points[i].value] += total;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (long x : ans) {
            sb.append(x).append("\n");
        }
        System.out.print(sb);
    }
    
    static class Point implements Comparable<Point> {
        int position;
        int value;
        boolean isThing;
        
        public Point(int position, int value, boolean isThing) {
            this.position = position;
            this.value = value;
            this.isThing = isThing;
        }
        
        public int compareTo(Point another) {
            return position - another.position;
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0