たいちっち

競技プログラミングをしています

【Java】鉄則本_A06

読んでいる本

問題と提出した回答A06

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int Q = sc.nextInt();
        Map sum = new HashMap<>();
        sum.put(0, 0);//初期値
        for (int i = 1; i <= N; i++) {
            int tmp = sc.nextInt();
            sum.put(i, (int) sum.get(i-1)+tmp);
        }
        for (int i = 0; i < Q; i++) {
            int Lq = sc.nextInt();
            int Rq = sc.nextInt();
            System.out.println((int) sum.get(Rq) - (int) sum.get(Lq-1));
        }
        sc.close();
    }
}

累積和の問題。何度も同じ計算をするのは計算量が多くなるから、累積和を計算しておいて、都度回答に必要な値を取り出す。

 

公式の回答:kyopro-tessoku/codes/java/chap02/answer_A06.java

 

問題と提出した回答B06

考え中

 

TODO

  • 本の続きを読む
  • 全bit探索を理解する

 

おわり。。