#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pi pair<int,int>
const int MAXN = 1e6 + 5;
struct d{
int mi = 1e9;
int ma = 0;
};
vector<d> przedzialy;
int koszt[MAXN];
void dfs(int a, int b, int nr, vector<vector<int>> &kolorek, vector<vector<vector<pair<int,int>>>> &graf){
if(kolorek[a][b] != -1) return;
kolorek[a][b] = nr;
przedzialy[nr].mi = min(przedzialy.back().mi, a);
przedzialy[nr].ma = max(przedzialy.back().ma, a);
for(auto u : graf[a][b]){
if(kolorek[u.first][u.second] == -1) dfs(u.first, u.second, nr, kolorek, graf);
}
return;
}
int nxt[MAXN][22][3];
vector<int> jed;
vector<int> dwa;
int p1[MAXN];
int p2[MAXN];
int cl1(int a, int b){
if(p1[b] <= a) return a;
else return p1[b];
}
int cl2(int a, int b){
if(p2[b] <= a) return a;
else return p2[b];
}
bool comp(const pair<int,int> &p1, const pair<int,int> &p2){
if(p1.second != p2.second) return p1.second < p2.second;
if(p1.first != p2.first) return p1.first < p2.first;
return false;
}
ll jmp(int s, int a, int b){
int w = 0;
ll curr = s;
for(int l = 21; l >= 0; --l){
if(nxt[curr][l][2] < a){
w += (1 << l) + 1;
curr = nxt[curr][l][2];
}
else if(nxt[curr][l][1] < a){
w += (1 << l);
curr = nxt[curr][l][1];
}
else if(nxt[curr][l][0] < a){
w += (1 << l) - 1;
curr = nxt[curr][l][0];
}
}
if(nxt[curr][0][1] < a){
cout << "HUHH\n";
}
return w + 1;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> kolorek;
vector<vector<vector<pair<int,int>>>> graf;
kolorek.resize(n + 1);
graf.resize(n + 1);
for(int i = 0; i <= n; ++i){
kolorek[i].resize(m + 1);
graf[i].resize(m + 1);
}
for(int i = 0; i <= n; ++i){
for(int j = 0; j <= m; ++j){
kolorek[i][j] = -1;
graf[i][j].clear();
}
}
char x;
string s;
for(int i = 1; i <= n; ++i){
cin >> s;
for(int j = 1; j < m; ++j){
x = s[j-1];
//cout << x << " x\n";
if(x == '1') {
graf[i][j].push_back({i,j+1});
graf[i][j+1].push_back({i,j});
}
}
}
//cout << "#\n";
for(int i = 1; i < n; ++i){
cin >> s;
for(int j = 1; j <= m; ++j){
x = s[j-1];
if(x == '1') {
graf[i][j].push_back({i + 1, j});
graf[i+1][j].push_back({i,j});
}
}
}
int c1 = -1, c2 = -1;
for(int i = 1; i <= n; ++i){
cin >> koszt[i];
if(koszt[i] == 1){
jed.push_back(i);
c1 = i;
}
else{
dwa.push_back(i);
c2 = i;
}
p1[i] = c1;
p2[i] = c2;
}
//cout << "$\n";
int nr = 1;
przedzialy.push_back((d){1000000000, 0});
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= m; ++j){
if(kolorek[i][j] == -1){
przedzialy.push_back((d){i, i});
dfs(i, j, nr, kolorek, graf);
nr++;
}
}
}
for(int l = 1; l <= 21; ++l){
for(int i = 1; i <= n; ++i){
nxt[i][l][0] = 1e9;
nxt[i][l][1] = 1e9;
nxt[i][l][2] = 1e9;
}
}
for(int i = n; i > 0; --i){
nxt[i][0][0] = i;
int reach = i;
for(int j = 1; j <= m; ++j){
reach = max(reach, przedzialy[kolorek[i][j]].ma);
}
int w1 = cl1(i, reach);
nxt[i][0][1] = w1;
int w2 = cl2(i, reach);
nxt[i][0][2] = w2;
nxt[i][0][2] = max(nxt[i][0][2], nxt[w1][0][1]);
}
for(int l = 1; l <= 21; ++l){
for(int i = 1; i <= n; ++i){
nxt[i][l][0] = max(nxt[nxt[i][l-1][1]][l-1][0], nxt[nxt[i][l-1][0]][l-1][1]);
nxt[i][l][1] = max({nxt[nxt[i][l-1][1]][l-1][1], nxt[nxt[i][l-1][2]][l-1][0], nxt[nxt[i][l-1][0]][l-1][2]});
nxt[i][l][2] = max(nxt[nxt[i][l-1][1]][l-1][2], nxt[nxt[i][l-1][2]][l-1][1]);
}
}
pair<int,int> a;
for(int i = 1; i <= q; ++i){
int r;
cin >> r;
if(r != 2) exit(0);
set<int> num;
for(int i = 0; i < r; ++i){
cin >> a.first >> a.second;
num.insert(kolorek[a.first][a.second]);
}
if(num.size() == 1){
cout << 0 << "\n";
continue;
}
vector<pair<int,int>> v;
for(auto u : num){
v.push_back({przedzialy[u].mi, przedzialy[u].ma});
}
sort(v.begin(),v.end(), comp);
pair<int,int> para1 = v[0];
pair<int,int> para2 = v[1];
//cout << para1.first << " " << para1.second << " " << para2.first << " " << para2.second << " pary\n";
if(para1.second >= para2.first){
if(p1[para1.second] >= para2.first) cout << 1 << "\n";
else cout << 2 << "\n";
continue;
}
ll w = 1e9;
if(p1[para1.second] >= para1.first){
ll w1 = jmp(p1[para1.second], para2.first, para2.second);
w = min(w, w1 + 1);
}
if(p2[para1.second] >= para1.first){
ll w2 = jmp(p2[para1.second], para2.first, para2.second);
w = min(w, w2 + 1);
}
if(w == 1e9){
cout << -1 << "\n";
}
else{
cout << w << "\n";
}
}
return 0;
}
# | 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... |