Home AtCoder. 010 Score Sum Queries(2)
Post
Cancel

AtCoder. 010 Score Sum Queries(2)

[Link] https://AtCoder.jp/contests/typical90/tasks/typical90_j


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.*;
import java.io.*;

public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader reader =  new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st;
    StringBuilder sb = new StringBuilder();
    int n = Integer.parseInt(reader.readLine());
    int[] classOne = new int[n + 1];
    int[] classTwo = new int[n + 1];
    for(int i = 1; i <= n; i++) {
      st = new StringTokenizer(reader.readLine());
      int classN = Integer.parseInt(st.nextToken());
      int score = Integer.parseInt(st.nextToken());
      classOne[i] = classOne[i-1];
      classTwo[i] = classTwo[i-1];
      if(classN == 1) classOne[i] += score;
      else classTwo[i] += score;
    }

    int q = Integer.parseInt(reader.readLine());
    for(int i = 1; i <= q; i++) {
      st = new StringTokenizer(reader.readLine());
      int l = Integer.parseInt(st.nextToken());
      int r = Integer.parseInt(st.nextToken());
      int sum1 = classOne[r] - classOne[l-1], sum2 = classTwo[r] - classTwo[l-1];
      sb.append(sum1 + " " + sum2).append("\n");
    }

    System.out.print(sb);
  }
}
This post is licensed under CC BY 4.0 by the author.