Skip to content

Commit 3c5cc26

Browse files
authored
[20260317] BOJ / P1 / 지형 평탄화 탐색기 / 권혁준
1 parent 808e2cb commit 3c5cc26

1 file changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class BOJ33908 {
6+
7+
static class Count {
8+
int[] count;
9+
Count() {
10+
count = new int[1953125];
11+
}
12+
13+
void add(int value) {
14+
if(value < 0) return;
15+
count[value]++;
16+
}
17+
18+
void remove(int value) {
19+
if(value < 0) return;
20+
count[value]--;
21+
}
22+
23+
int get(int value) {
24+
return count[value];
25+
}
26+
}
27+
28+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
29+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
30+
static StringTokenizer st;
31+
32+
static int N, M, Q;
33+
static int[][] arr;
34+
35+
static Count[][] type;
36+
37+
public static void main(String[] args) throws Exception {
38+
// 0. Input
39+
40+
st = new StringTokenizer(br.readLine());
41+
N = Integer.parseInt(st.nextToken());
42+
M = Integer.parseInt(st.nextToken());
43+
arr = new int[N][M];
44+
for(int i=0;i<N;i++) {
45+
st = new StringTokenizer(br.readLine());
46+
for(int j=0;j<M;j++) {
47+
arr[i][j] = Integer.parseInt(st.nextToken()) - 1;
48+
}
49+
}
50+
51+
// 1. Find All Types
52+
53+
type = new Count[10][];
54+
for(int r=1;r<=9;r++) {
55+
type[r] = new Count[9/r + 1];
56+
}
57+
58+
for(int r=1;r<=9;r++) for(int c=1;r*c<=9;c++) {
59+
type[r][c] = new Count();
60+
for(int i=0;i+r<=N;i++) for(int j=0;j+c<=M;j++) {
61+
type[r][c].add(compute(i, j, r, c));
62+
}
63+
}
64+
65+
// 2. Solve queries
66+
67+
Q = Integer.parseInt(br.readLine());
68+
while(Q-- > 0) {
69+
st = new StringTokenizer(br.readLine());
70+
int q = Integer.parseInt(st.nextToken());
71+
if(q == 1) {
72+
int x = Integer.parseInt(st.nextToken()) - 1;
73+
int y = Integer.parseInt(st.nextToken()) - 1;
74+
int h = Integer.parseInt(st.nextToken()) - 1;
75+
for(int r=1;r<=9;r++) for(int c=1;r*c<=9;c++) {
76+
for(int i=Math.max(x-r+1, 0);i<=Math.min(x, N-r);i++) for(int j=Math.max(y-c+1, 0);j<=Math.min(y, M-c);j++) {
77+
type[r][c].remove(compute(i, j, r, c));
78+
}
79+
}
80+
arr[x][y] = h;
81+
for(int r=1;r<=9;r++) for(int c=1;r*c<=9;c++) {
82+
for(int i=Math.max(x-r+1, 0);i<=Math.min(x, N-r);i++) for(int j=Math.max(y-c+1, 0);j<=Math.min(y, M-c);j++) {
83+
type[r][c].add(compute(i, j, r, c));
84+
}
85+
}
86+
}
87+
else {
88+
int r = Integer.parseInt(st.nextToken());
89+
int c = Integer.parseInt(st.nextToken());
90+
int[][] arr2 = new int[r][c];
91+
int min = 4, max = -1;
92+
for(int i=0;i<r;i++) for(int j=0;j<c;j++) {
93+
arr2[i][j] = Integer.parseInt(st.nextToken()) - 1;
94+
min = Math.min(min, arr2[i][j]);
95+
max = Math.max(max, arr2[i][j]);
96+
}
97+
98+
int answer = 0;
99+
for(int z=-min;z<=4-max;z++) {
100+
int k = 1, value = 0;
101+
for(int i=0;i<r;i++) for(int j=0;j<c;j++) {
102+
value += k * (arr2[i][j] + z);
103+
k *= 5;
104+
}
105+
answer += type[r][c].get(value);
106+
}
107+
108+
bw.write(answer + "\n");
109+
}
110+
}
111+
bw.close();
112+
}
113+
114+
static int compute(int x, int y, int r, int c) {
115+
if(0 <= x && x+r <= N && 0 <= y && y+c <= M) {
116+
int k = 1, value = 0;
117+
for(int i=x;i<x+r;i++) for(int j=y;j<y+c;j++) {
118+
value += k * arr[i][j];
119+
k *= 5;
120+
}
121+
return value;
122+
}
123+
return -1;
124+
}
125+
126+
}
127+
```

0 commit comments

Comments
 (0)