|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.PriorityQueue; |
| 11 | +import java.util.StringTokenizer; |
| 12 | + |
| 13 | +public class Main { |
| 14 | + |
| 15 | + static BufferedReader br; |
| 16 | + static StringTokenizer st; |
| 17 | + static StringBuilder sb = new StringBuilder(); |
| 18 | + |
| 19 | + static PriorityQueue<Integer> minQ = new PriorityQueue<>(); |
| 20 | + static PriorityQueue<Integer> maxQ = new PriorityQueue<>(Collections.reverseOrder()); |
| 21 | + |
| 22 | + static HashMap<Integer,Integer> map = new HashMap<>(); |
| 23 | + |
| 24 | + static int size,len,qCnt; |
| 25 | + static int[][] arr,min,max; |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + public static void main(String[] args) throws IOException { |
| 30 | + init(); |
| 31 | + process(); |
| 32 | + print(); |
| 33 | + } |
| 34 | + |
| 35 | + private static void init() throws IOException{ |
| 36 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 37 | + st = new StringTokenizer(br.readLine()); |
| 38 | + size = Integer.parseInt(st.nextToken()); |
| 39 | + len = Integer.parseInt(st.nextToken()); |
| 40 | + qCnt = Integer.parseInt(st.nextToken()); |
| 41 | + |
| 42 | + arr = new int[size][size]; |
| 43 | + min = new int[size-len+1][size-len+1]; |
| 44 | + max = new int[size-len+1][size-len+1]; |
| 45 | + for (int i = 0; i < size; i++) { |
| 46 | + st = new StringTokenizer(br.readLine()); |
| 47 | + for (int j = 0; j < size; j++) { |
| 48 | + arr[i][j] = Integer.parseInt(st.nextToken()); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + private static void process() throws IOException { |
| 56 | + getBase(); |
| 57 | + |
| 58 | + for (int i = 0; i < size-len+1; i++) { |
| 59 | + |
| 60 | + |
| 61 | + if (i != 0)goUp(i); |
| 62 | + PriorityQueue<Integer> preMinQ = new PriorityQueue<>(); |
| 63 | + PriorityQueue<Integer> preMaxQ = new PriorityQueue<>(Collections.reverseOrder()); |
| 64 | + HashMap<Integer,Integer> preMap = new HashMap<>(); |
| 65 | + preMinQ.addAll(minQ); |
| 66 | + preMaxQ.addAll(maxQ); |
| 67 | + preMap.putAll(map); |
| 68 | + |
| 69 | + calculate(i,0); |
| 70 | + for (int j = 1; j < size-len+1; j++) { |
| 71 | + goRight(i,j); |
| 72 | + calculate(i,j); |
| 73 | + } |
| 74 | + |
| 75 | + minQ = preMinQ; |
| 76 | + maxQ = preMaxQ; |
| 77 | + map = preMap; |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + for (int i = 0; i < qCnt; i++) { |
| 82 | + st = new StringTokenizer(br.readLine()); |
| 83 | + int y = Integer.parseInt(st.nextToken())-1; |
| 84 | + int x = Integer.parseInt(st.nextToken())-1; |
| 85 | + sb.append(max[y][x] - min[y][x]).append("\n"); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + private static void goUp(int y) { |
| 92 | + for (int i = 0; i < len; i++) { |
| 93 | + remove(y-1,i); |
| 94 | + } |
| 95 | + |
| 96 | + for (int i = 0; i < len; i++) { |
| 97 | + put(y+len-1,i); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static void goRight(int y, int x) { |
| 102 | + for (int i = y; i < y+len; i++) { |
| 103 | + remove(i,x-1); |
| 104 | + put(i,x+len-1); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + private static void calculate(int y, int x) { |
| 112 | + int minNum = minQ.peek(); |
| 113 | + int maxNum = maxQ.peek(); |
| 114 | + while(map.get(minNum) == 0) { |
| 115 | + minQ.poll(); |
| 116 | + minNum = minQ.peek(); |
| 117 | + } |
| 118 | + while(map.get(maxNum) == 0) { |
| 119 | + maxQ.poll(); |
| 120 | + maxNum = maxQ.peek(); |
| 121 | + } |
| 122 | + min[y][x] = minNum; |
| 123 | + max[y][x] =maxNum; |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + private static void getBase() { |
| 130 | + for (int i = 0; i < len; i++) { |
| 131 | + for (int j = 0; j < len; j++) { |
| 132 | + put(i,j); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + private static void put(int y,int x) { |
| 139 | + int num = arr[y][x]; |
| 140 | + if (map.containsKey(num) && map.get(num) > 0) { |
| 141 | + map.replace(num, map.get(num)+1); |
| 142 | + } else { |
| 143 | + map.put(num, 1); |
| 144 | + minQ.add(num); |
| 145 | + maxQ.add(num); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private static void remove(int y,int x) { |
| 150 | + int num = arr[y][x]; |
| 151 | + map.replace(num, map.get(num)-1); |
| 152 | + } |
| 153 | + |
| 154 | + private static void print() { |
| 155 | + System.out.print(sb); |
| 156 | + } |
| 157 | + |
| 158 | +} |
| 159 | +``` |
0 commit comments