|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static int[][] map; |
| 9 | + private static int[][][] sum; |
| 10 | + private static int N, Q; |
| 11 | + public static void main(String[] args) throws IOException { |
| 12 | + init(); |
| 13 | +
|
| 14 | + bw.flush(); |
| 15 | + bw.close(); |
| 16 | + br.close(); |
| 17 | + } |
| 18 | +
|
| 19 | + private static void init() throws IOException { |
| 20 | + N = Integer.parseInt(br.readLine()); |
| 21 | +
|
| 22 | + map = new int[N+1][N+1]; |
| 23 | + sum = new int[N+1][N+1][11]; |
| 24 | +
|
| 25 | + for (int i = 1; i <= N; i++) { |
| 26 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 27 | + for (int j = 1; j <= N; j++) { |
| 28 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 29 | + } |
| 30 | + } |
| 31 | +
|
| 32 | + for (int i = 1; i <= N; i++) { |
| 33 | + for (int j = 1; j <= N; j++) { |
| 34 | + for (int k = 1; k <= 10; k++) { |
| 35 | + sum[i][j][k] += sum[i-1][j][k] + sum[i][j-1][k] - sum[i-1][j-1][k]; |
| 36 | + } |
| 37 | + sum[i][j][map[i][j]]++; |
| 38 | + } |
| 39 | + } |
| 40 | +
|
| 41 | + Q = Integer.parseInt(br.readLine()); |
| 42 | + int[] temp = new int[11]; |
| 43 | +
|
| 44 | + while (Q-->0) { |
| 45 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 46 | + int x1 = Integer.parseInt(st.nextToken()); |
| 47 | + int y1 = Integer.parseInt(st.nextToken()); |
| 48 | + int x2 = Integer.parseInt(st.nextToken()); |
| 49 | + int y2 = Integer.parseInt(st.nextToken()); |
| 50 | + int count = 0; |
| 51 | +
|
| 52 | + Arrays.fill(temp, 0); |
| 53 | + for (int i = 1; i <= 10; i++) { |
| 54 | + temp[i] += sum[x2][y2][i] - sum[x1-1][y2][i] - sum[x2][y1-1][i] + sum[x1-1][y1-1][i]; |
| 55 | + if (temp[i] > 0) count++; |
| 56 | + } |
| 57 | +
|
| 58 | + bw.write(count + "\n"); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
0 commit comments