1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
#include <bits/stdc++.h> #define all(vec) vec.begin(),vec.end() #define lson(o) (o<<1) #define rson(o) (o<<1|1) #define SZ(a) ((long long) a.size()) #define debug(var) cerr << #var <<":"<<var<<"\n"; #define cend cerr<<"\n-----------\n" #define fsp(x) fixed<<setprecision(x)
using namespace std;
using ll = long long; using ull = unsigned long long; using DB = double; using LD = long double;
using CD = complex<double>;
static constexpr ll MAXN = (ll)1e6+10, INF = (1ll<<61)-1; static constexpr ll mod = 998244353; static constexpr double eps = 1e-8; const double pi = acos(-1.0);
ll lT;
int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1};
void Solve() { ll N,M,K; cin >> N >> M >> K; vector maze(N+2,vector<ll>(M+2)); for (int i=1;i<=K;++i) { ll x,y; cin>>x>>y; maze[x][y]=i; } ll xl,yl,xr,yr; cin>>xl>>yl>>xr>>yr; ll mx_in=0; for (int i=xl;i<=xr;++i) { for (int j=yl;j<=yr;++j) { mx_in=max(mx_in,maze[i][j]); } } vector vis(N+2,vector<int>(M+2)); ll tim=0; auto is_valid=[&](ll threshold,ll x,ll y) { if (x<1 || x>N) { return false; } if (y<1 || y>M) { return false; } if (maze[x][y]>threshold) { return false; } if (vis[x][y]>=tim) { return false; } return true; }; auto is_in=[&](ll x,ll y) { if (x>=xl && x<=xr && y>=yl && y<=yr) { return true; } return false; }; auto bfs=[&](ll threshold,ll x,ll y) -> bool { queue<pair<ll,ll>> q; vis[x][y]=tim; q.push({x,y}); ll block_in_cnt=0,space_cnt=0; while (!q.empty()) { auto [px,py]=q.front(); q.pop(); if (is_in(px,py)) { if (maze[px][py]>0) { block_in_cnt++; } }else { if (maze[px][py]==0) { space_cnt++; } } for (int i=0;i<4;++i) { ll tox=px+dx[i],toy=py+dy[i]; if (is_valid(threshold,tox,toy)) { vis[tox][toy]=tim; q.push({tox,toy}); } } } if (space_cnt>=block_in_cnt) { return true; } return false; }; auto check=[&](ll threshold) -> bool { tim++; for (int i=1;i<=N;++i) { for (int j=1;j<=M;++j) { if (!is_valid(threshold,i,j)) { continue; } if(!bfs(threshold,i,j)) { return false; } } } return true; }; ll l=mx_in,r=K; while (l<r) { ll mid=(l+r)>>1; if (check(mid)) { r=mid; }else { l=mid+1; } }
if (check(l)) { cout<<l<<"\n"; }else { cout<<-1<<"\n"; } }
signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); Solve(); return 0; }
|