Skip to content

Commit 033c836

Browse files
authored
[20250809] BOJ / P4 / 다오의 행사 계획하기 / 권혁준
1 parent db965ce commit 033c836

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static int N, M, T;
57+
static int[][] row, col;
58+
static List<Integer>[] graph;
59+
static int[][] par;
60+
static int[] dep;
61+
static long[] arr;
62+
63+
public static void main(String[] args) throws Exception {
64+
65+
io = new IOController();
66+
67+
init();
68+
solve();
69+
70+
io.close();
71+
72+
}
73+
74+
static void init() throws Exception {
75+
76+
N = io.nextInt();
77+
M = io.nextInt();
78+
T = io.nextInt();
79+
row = new int[N][M];
80+
col = new int[N][M];
81+
for(int i=0;i<N-1;i++) for(int j=0;j<M;j++) row[i][j] = io.nextInt();
82+
for(int i=0;i<N;i++) for(int j=0;j<M-1;j++) col[i][j] = io.nextInt();
83+
graph = new List[N*M];
84+
for(int i=0;i<N*M;i++) graph[i] = new ArrayList<>();
85+
par = new int[N*M+1][17];
86+
dep = new int[N*M];
87+
arr = new long[T+2];
88+
89+
}
90+
91+
static void solve() throws Exception {
92+
93+
for(int i=0;i<N;i++) for(int j=0;j<M;j++) {
94+
if(i<N-1 && row[i][j] == 0) {
95+
graph[i*M+j].add((i+1)*M+j);
96+
graph[(i+1)*M+j].add(i*M+j);
97+
}
98+
if(j<M-1 && col[i][j] == 0) {
99+
graph[i*M+j].add(i*M+j+1);
100+
graph[i*M+j+1].add(i*M+j);
101+
}
102+
}
103+
104+
dfs(0,N*M,0);
105+
for(int k=1;k<17;k++) for(int i=0;i<N*M;i++) par[i][k] = par[par[i][k-1]][k-1];
106+
107+
for(int K=io.nextInt();K-->0;) {
108+
int s = io.nextInt();
109+
int e = io.nextInt();
110+
int a = io.nextInt();
111+
int b = io.nextInt();
112+
int c = io.nextInt();
113+
int d = io.nextInt();
114+
int v = io.nextInt();
115+
116+
int x = a*M+b, y = c*M+d;
117+
118+
int dist = 1, diff = Math.abs(dep[x]-dep[y]);
119+
for(int k=0;k<17;k++) if((diff & (1<<k)) != 0) {
120+
if(dep[x] > dep[y]) {
121+
dist += (1<<k);
122+
x = par[x][k];
123+
}
124+
else {
125+
dist += (1<<k);
126+
y = par[y][k];
127+
}
128+
}
129+
130+
for(int k=16;k>=0;k--) if(par[x][k] != par[y][k]) {
131+
dist += (1<<(k+1));
132+
x = par[x][k];
133+
y = par[y][k];
134+
}
135+
if(x != y) dist += 2;
136+
137+
arr[s] += v*dist;
138+
arr[e+1] -= v*dist;
139+
140+
}
141+
142+
long s = 0;
143+
for(int i=1;i<=T;i++) {
144+
s += arr[i];
145+
io.write(s + "\n");
146+
}
147+
148+
}
149+
150+
static void dfs(int n, int p, int d) {
151+
dep[n] = d;
152+
par[n][0] = p;
153+
for(int i:graph[n]) if(i != p) dfs(i,n,d+1);
154+
}
155+
156+
}
157+
```

0 commit comments

Comments
 (0)