This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 4005,M = 2e5+15,LIM = 4e6+5;
int px[M],py[M];
int org[N][N],T[N][N];
int par[M];
int f(int x){
if(x==par[x])return x;
return par[x]= f(par[x]);
}
int xlim ,ylim;
int check(int ny,int nx){
if(nx>=1 && ny>=1 && nx<=xlim && ny<=ylim)return 1;
return 0;
}
void color(){
for(int i=1;i<=ylim;++i)
for(int j=1;j<=xlim;++j)
T[i][j] = org[i][j];
}
void merge(int a,int b){
a= f(a),b = f(b);
if(a==b)return;
par[b]= a;
}
vector<int> ch[LIM];
int L[M],R[M];
int start[M],tar[M];
int dy[4]= {1,-1,0,0},dx[4]= {0,0,1,-1};
int main(){
int h,w,p,q;
cin>>h>>w>>p>>q;
string s;
xlim = w*2-1,ylim = h*2-1;
for(int i=1;i<=h;++i){
cin>>s;
for(int j=1;j<=w;++j){
if(s[j-1]=='#'){
org[i*2-1][j*2-1] = -1;
org[i*2-1][j*2]= -1;
org[i*2][j*2-1]= -1;
org[i*2][j*2]= -1;
}
}
}
for(int i=1;i<=p;++i){
scanf("%d%d",&py[i],&px[i]);
px[i] = px[i]*2-1,py[i]= py[i]*2-1;
org[py[i]][px[i]]= i;
}
for(int i=1;i<=q;++i){
scanf("%d%d",&start[i],&tar[i]);
L[i]= 0,R[i] = LIM-1;
}
bool is = 1;
while(is){
is=0;
color();
stack<pair<int,pair<int,int> > >st;
for(int i=1;i<=p;++i){
par[i]= i;
st.push(make_pair(i,make_pair(py[i],px[i])));
}
for(int i=1;i<=q;++i){
if(L[i]==R[i])continue;
int mid = (L[i] + R[i])/2;
is = 1;
ch[mid].push_back(i);
}
stack<pair<int,pair<int,int> > >temp;
int ny,nx,a,b,len = 0,cur;
while(!st.empty()){
++len;
while(!st.empty()){
int id = st.top().first;
a= st.top().second.first, b= st.top().second.second;
st.pop();
for(int i=0;i<4;++i){
ny = a+dy[i],nx =b+dx[i];
if(T[ny][nx]==0){
if(check(ny,nx)){
T[ny][nx]=id;
temp.push(make_pair(id,make_pair(ny,nx)));
}
}
else if(T[ny][nx]!=-1){
merge(T[ny][nx],id);
}
}
}
swap(st,temp);
cur = (len-1)/2;
cur*=2;
if(len%2 ==0)
++cur;
while(!ch[cur].empty()){
a = ch[cur].back();
ch[cur].pop_back();
if(f(start[a]) == f(tar[a])){
R[a] = cur;
}
else{
L[a]= cur+1;
}
}
}
for(int i=cur;i<LIM;++i){
while(!ch[i].empty()){
a= ch[i].back();
R[a]= i;
ch[i].pop_back();
}
}
}
for(int i=1;i<=q;++i){
printf("%d\n",L[i]);
}
}
Compilation message (stderr)
bottle.cpp: In function 'int main()':
bottle.cpp:59:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&py[i],&px[i]);
^
bottle.cpp:64:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&start[i],&tar[i]);
^
# | 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... |