#include <bits/stdc++.h>
#define rep(a,b,c) for(auto a = (b); a != (c); a++)
#define repD(a,b,c) for(auto a = (b); a != (c); a--)
#define repIn(a, b) for(auto& a : (b))
#define repIn2(a, b, c) for(auto& [a, b] : c)
#define ll long long
constexpr bool dbg = 0;
#define DEBUG if constexpr(dbg)
#define DC DEBUG std::cerr
#define eol std::endl
using namespace std;
constexpr int maxn = 1e6 + 7, maxLog = 21;
int h, w, q, n;
vector<int> graf[maxn];
int component[maxn], top[maxn], bottom[maxn];
int jp[maxn][maxLog];
int rowCosts[maxn];
void input() {
scanf("%d%d%d", &h, &w, &q);
char c;
rep(i, 0, h) rep(j, 0, w - 1) scanf(" %c", &c), (c == '1' ? graf[i * w + j].push_back(i * w + j + 1), graf[i * w + j + 1].push_back(i * w + j), 0 : 0);
rep(i, 0, h - 1) rep(j, 0, w) scanf(" %c", &c), (c == '1' ? graf[i * w + j].push_back(i * w + j + w), graf[i * w + j + w].push_back(i * w + j), 0 : 0);
rep(i, 0, h) scanf("%d", rowCosts + i);
}
void dfsComponents(int v, int c) {
component[v] = c;
top[c] = min(top[c], v / w);
bottom[c] = max(bottom[c], v / w);
repIn(u, graf[v]) if (!component[u]) dfsComponents(u, c);
}
void calcComponents() {
rep(i, 0, h) rep(j, 0, w) if (!component[i * w + j]) {
n++;
top[n] = bottom[n] = i;
dfsComponents(i * w + j, n);
DC << "Component " << n << " from " << i << ' ' << j << " -> top " << top[n] << " bottom " << bottom[n] << eol;
}
}
int lowestSon1[maxn], lowestSon2[maxn], lowestRow1[maxn], lowestRow2[maxn];
void calcLowestSon() {
// TODO: 1 or 2
priority_queue<pair<int, int>> Q1, Q2;
repD(i, h - 1, -1) {
while (!Q1.empty() && top[Q1.top().second] > i) Q1.pop();
while (!Q2.empty() && top[Q2.top().second] > i) Q2.pop();
if (!Q1.empty()) rep(j, 0, w) {
bool xd = false;
if (Q1.top().second == component[i * w + j]) Q1.pop(), xd = true;
if (!Q1.empty() && bottom[lowestSon1[component[i * w + j]]] < Q1.top().first && bottom[component[i * w + j]] < Q1.top().first) lowestSon1[component[i * w + j]] = Q1.top().second;
if (xd) Q1.push({bottom[component[i * w + j]], component[i * w + j]});
}
if (!Q2.empty()) rep(j, 0, w) {
bool xd = false;
if (Q2.top().second == component[i * w + j]) Q2.pop(), xd = true;
if (!Q2.empty() && bottom[lowestSon2[component[i * w + j]]] < Q2.top().first && bottom[component[i * w + j]] < Q2.top().first) lowestSon2[component[i * w + j]] = Q2.top().second;
if (xd) Q2.push({bottom[component[i * w + j]], component[i * w + j]});
}
rep(j, 0, w) if ((j == 0 || component[i * w + j] != component[i * w + j - 1]) && bottom[component[i * w + j]] == i) (rowCosts[i] == 1 ? Q1 : Q2).push({i, component[i * w + j]});
auto& lr = rowCosts[i] == 1 ? lowestRow1 : lowestRow2;
rep(j, 0, w) if ((j == 0 || component[i * w + j] != component[i * w + j - 1])) lr[component[i * w + j]] = max(lr[component[i * w + j]], i);
}
DEBUG rep(i, 1, n + 1) DC << "LowestSon1[" << i << "] = " << lowestSon1[i] << " lowestRow1 = " << lowestRow1[i] << eol;
DEBUG rep(i, 1, n + 1) DC << "LowestSon2[" << i << "] = " << lowestSon2[i] << " lowestRow2 = " << lowestRow2[i] << eol;
}
void calcJP() {
rep(i, 1, n + 1) jp[i][0] = lowestSon1[i];
rep(i, 1, n + 1) {
auto a = jp[jp[i][0]][0], b = lowestSon2[i];
if (a == 0) {jp[i][1] = b; continue;}
if (b == 0) {jp[i][1] = a; continue;}
if (bottom[a] > bottom[b]) jp[i][1] = a;
else jp[i][1] = b;
}
rep(k, 2, maxLog) rep(i, 1, n + 1) jp[i][k] = jp[jp[i][k - 1]][k - 1];
}
int query(int v, int u) {
if (bottom[v] > bottom[u]) swap(u, v);
if (u == v) return 0;
if (bottom[v] >= top[u]) return 1;
int ans = 0;
DC << "query " << v << ' ' << u << eol << ' ';
repD(k, maxLog - 1, -1) if (jp[v][k] && bottom[jp[v][k]] < top[u]) {
v = jp[v][k], ans += 1 << k;
DC << v << "(" << (1 << k) << ") ";
}
DC << ".";
if (lowestSon1[v] != 0 && top[u] <= lowestRow1[lowestSon1[v]]) {
DC << "." << lowestSon1[v] << ' ' << lowestRow1[lowestSon1[v]] << eol;
return ans + 2;
}
if (lowestSon1[v] != 0 && top[u] <= lowestRow2[lowestSon1[v]]) {
DC << "," << lowestSon1[v] << ' ' << lowestRow2[lowestSon1[v]] << eol;
return ans + 3;
}
if (lowestSon2[v] != 0 && top[u] <= lowestRow1[lowestSon2[v]]) {
DC << '_' << lowestSon2[v] << ' ' << lowestRow1[lowestSon2[v]] << eol;
return ans + 3;
}
if (lowestSon2[v] != 0 && top[u] <= lowestRow2[lowestSon2[v]]) {
DC << ';' << lowestSon2[v] << ' ' << lowestRow2[v] << eol;
return ans + 4;
}
DC << "chuj" << eol;
return -1;
}
void processQueries() {
int t, a, b, c, d;
rep(i, 0, q) {
// TODO: more vertices
scanf("%d%d%d%d%d", &t, &a, &b, &c, &d);
a--; b--; c--; d--;
printf("%d\n", query(component[a * w + b], component[c * w + d]));
}
}
int main() {
input();
calcComponents();
calcLowestSon();
calcJP();
processQueries();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'void input()':
Main.cpp:25:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | scanf("%d%d%d", &h, &w, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:27:40: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | rep(i, 0, h) rep(j, 0, w - 1) scanf(" %c", &c), (c == '1' ? graf[i * w + j].push_back(i * w + j + 1), graf[i * w + j + 1].push_back(i * w + j), 0 : 0);
| ~~~~~^~~~~~~~~~~
Main.cpp:28:40: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | rep(i, 0, h - 1) rep(j, 0, w) scanf(" %c", &c), (c == '1' ? graf[i * w + j].push_back(i * w + j + w), graf[i * w + j + w].push_back(i * w + j), 0 : 0);
| ~~~~~^~~~~~~~~~~
Main.cpp:29:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | rep(i, 0, h) scanf("%d", rowCosts + i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'void processQueries()':
Main.cpp:120:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | scanf("%d%d%d%d%d", &t, &a, &b, &c, &d);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |