hwangnk1004 / Algorithm

0 stars 0 forks source link

StepByStep #구간 합 #270

Open hwangnk1004 opened 4 years ago

hwangnk1004 commented 4 years ago

import java.util.Scanner;

public class Main { public static void main(String args[]) {

    Scanner scanner = new Scanner(System.in);

    int n = scanner.nextInt();
    int[] arr = new int[n];
    int k = 0;

    for (int i = 0; i < arr.length; i++) {
        k += scanner.nextInt();

        arr[i] = k;

    }

    int q = scanner.nextInt();

    for (int i = 0; i < q; i++) {
        int a = scanner.nextInt();
        int b = scanner.nextInt();

        if (a == 1) {
            System.out.println(arr[b - 1]);
        } else {
            System.out.println(arr[b - 1] - arr[a - 2]);
        }
    }

}

}