#include <bits/stdc++.h>
using namespace std;
vector<vector<vector<bool>>> gr;
vector<int> cs;
vector<vector<int>> sp;
vector<pair<int,int>> spp;
int csp = 0;
vector<vector<int>> jm[21];
vector<int> mx;
vector<int> mx2;
int h,w,q;
void dfs(int i,int j,int s)
{
spp[s].first = min(spp[s].first,i);
spp[s].second = max(spp[s].second,i);
sp[i][j] = s;
if(gr[i][j][0] && sp[i-1][j] == -1)
{
dfs(i-1,j,s);
}
if(gr[i][j][1] && sp[i][j+1] == -1)
{
dfs(i,j+1,s);
}
if(gr[i][j][2] && sp[i+1][j] == -1)
{
dfs(i+1,j,s);
}
if(gr[i][j][3] && sp[i][j-1] == -1)
{
dfs(i,j-1,s);
}
}
vector<int> tr;
void add(int p,int v)
{
p+=h;
tr[p] = v;
p/=2;
while(p > 0)
{
tr[p] = min(tr[p*2],tr[p*2+1]);
p/=2;
}
}
int check(int l,int r)
{
l+=h;
r+=h;
int ans = min(tr[l],tr[r]);
while(l/2 != r/2)
{
if(l%2 == 0) ans = min(ans,tr[l+1]);
if(r%2 == 1) ans = min(ans, tr[r-1]);
l/=2;r/=2;
}
return ans;
}
int main()
{
cin>>h>>w>>q;
gr.resize(h);
sp.resize(h);
for(int i = 0;i<h;i++)
{
sp[i].resize(w);
gr[i].resize(w);
for(int j =0;j<w;j++)
{
sp[i][j] = -1;
gr[i][j].resize(4);
}
}
for(int i =0;i<h;i++)
{
for(int j = 0;j<w-1;j++)
{
char c;
cin>>c;
if(c == '1')
{
gr[i][j][1] = true;
gr[i][j+1][3] = true;
}
}
}
for(int i = 0;i<h-1;i++)
{
for(int j = 0;j<w;j++)
{
char c;
cin>>c;
if(c == '1')
{
gr[i][j][2] = true;
gr[i+1][j][0] =true;
}
}
}
cs.resize(h);
tr.resize(h*2);
for(int i =0;i<h;i++)
{
cin>>cs[i];
add(i,cs[i]);
}
for(int i = 0;i<h;i++)
{
for(int j = 0;j<w;j++)
{
if(sp[i][j] == -1)
{
spp.push_back({i,i});
dfs(i,j,csp);
csp++;
}
}
}
for(int o = 0;o<21;o++)
{
jm[o].resize(h);
for(int i = 0;i<h;i++)
{
jm[o][i].resize(3);
}
}
mx.resize(h);
mx2.resize(h);
for(int i = 0;i<csp;i++)
{
for(int j = spp[i].first;j<=spp[i].second;j++)
{
if(cs[j] == 1)
{
jm[0][j][1] = max(jm[0][j][1],spp[i].second);
}
else
{
jm[0][j][2] = max(jm[0][j][2],spp[i].second);
}
}
}
for(int i = 0;i<h;i++)
{
jm[0][i][2] = max(jm[0][i][2],jm[0][jm[0][i][1]][1]);
}
for(int i = 0;i<h;i++)
{
jm[0][i][0] = i;
}
for(int o = 1;o<21;o++)
{
for(int i = 0;i<h;i++)
{
jm[o][i][0] = max(jm[o-1][jm[o-1][i][0]][1],jm[o-1][jm[o-1][i][1]][0]);
jm[o][i][1] = max(jm[o-1][jm[o-1][i][2]][0],jm[o-1][jm[o-1][i][1]][1]);
jm[o][i][2] = max(jm[o-1][jm[o-1][i][1]][2],jm[o-1][jm[o-1][i][2]][1]);
}
}
for(int i = 0;i<q;i++)
{
int t;
cin>>t;
vector<pair<int,int>> p;
set<int> e;
int a,b;
cin>>a>>b;
a--;b--;
int x,y;
cin>>x>>y;
x--;y--;
//cout<<spp[sp[a][b]].first<<" "<<spp[sp[a][b]].second<<" "<<spp[sp[x][y]].first<<" "<<spp[sp[x][y]].second<<"\n";
if(spp[sp[a][b]] > spp[sp[x][y]])
{
swap(a,x);
swap(b,y);
}
//if(i == 1) return 0;
if(sp[a][b] == sp[x][y])
{
cout<<0<<"\n";
continue;
}
//if(i == 1) return 0;
/*if(i == 2)
{
return 0;
}*/
if(spp[sp[a][b]].second >= spp[sp[x][y]].first)
{
cout<<check(spp[sp[x][y]].first,spp[sp[a][b]].second)<<"\n";
continue;
}
//if(i == 1) return 0;
int cu0 = spp[sp[a][b]].second;
int cu = jm[0][cu0][1];
int cu2 = jm[0][cu0][2];
if(cu >= spp[sp[x][y]].first)
{
cout<<check(spp[sp[x][y]].first,cu)+1<<"\n";
continue;
}
/*for(int j = 0;j<t;j++)
{
int x,y;
cin>>x>>y;
x--;y--;
if(e.find(sp[x][y]) == e.end())
{
p.push_back({spp[sp[x][y]].first,sp[x][y]});
e.insert(sp[x][y]);
}
}
sort(p.begin(),p.end());
cu0 = spp[p[0].second].second;
cu = spp[p[0].second].second;
cu2 = spp[p[0].second].second;*/
int ans = 1;
//if(i == 1) return 0;
for(int o = 20;o>=0;o--)
{
if(max(jm[o][cu2][0],jm[o][cu][1]) < spp[sp[x][y]].first)
{
int cc = jm[o][cu2][0];
cu0 = max(jm[o][cu0][1],jm[o][cu][0]);
cu2 = max(jm[o][cu][2],jm[o][cu2][1]);
cu = max(jm[o][cu][1],cc);
ans+=(1<<o);
}
}
cu = max(jm[0][cu0][2],jm[0][cu][1]);
ans++;
cout<<ans+check(spp[sp[x][y]].first,cu)<<"\n";
}
}
# | 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... |