#include #include #include #include #include using namespace std ; using ll = long long ; using ull = unsigned long long ; ull *mem ; const ull ONES = (ull)(-1LL) ; void relimit(vector &c, vector &c1, vector &c2, map &xmap) { sort(c.begin(), c.end()) ; // locate boundary wall pair: BB....F ll fc = c[0] - 6 ; ll lc = c[c.size()-1] + 5 ; vector probes = c1 ; probes.insert(probes.end(), c2.begin(), c2.end()) ; sort(probes.begin(), probes.end()) ; probes.resize(unique(probes.begin(), probes.end()) - probes.begin()) ; for (auto v: probes) { fc = min(fc, v-2) ; lc = max(lc, v+1) ; } c.insert(c.begin(), fc+1) ; c.insert(c.begin(), fc) ; c.push_back(lc) ; c.push_back(lc+1) ; ll bigc = fc ; ll smc = 0 ; int pat = 0 ; int cat = 1 ; ll nbigc, nsmc ; vector newv ; while (1) { xmap[bigc] = smc ; newv.push_back(smc) ; if (cat >= (int)c.size()) break ; nbigc = c[cat++] ; if (nbigc - bigc > 7) nsmc = smc + 5 + (nbigc - bigc + 3) % 4 ; else nsmc = smc + nbigc - bigc ; while (pat < (int)probes.size() && probes[pat] < nbigc) { xmap[probes[pat]] = smc + 1 + (probes[pat] - bigc + 3) % 4 ; pat++ ; } bigc = nbigc ; smc = nsmc ; } swap(newv, c) ; } int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2 } ; int dy[] = {-1, 1, -2, 2, -2, 2, -1, 1 } ; int nomoves(ll x, ll y, map &xb, map &yb) { for (int i=0; i<8; i++) { ll x2 = x + dx[i] ; ll y2 = y + dy[i] ; if (xb.find(x2) == xb.end() && yb.find(y2) == yb.end()) return 0 ; } return 1 ; } int main(int argc, char *argv[]) { int n, q ; cin >> n >> q ; if (n == 0) { for (int i=0; i xr(n), yr(n) ; for (int i=0; i> xr[i] >> yr[i] ; vector xs(q), ys(q), xe(q), ye(q) ; vector solved(q) ; for (int i=0; i> xs[i] >> ys[i] >> xe[i] >> ye[i] ; map xrs, yrs ; for (auto v: xr) xrs[v] = v ; for (auto v: yr) yrs[v] = v ; // mod4 only holds if we can move at all, // so check before coordinate compression for (int i=0; i> 6 ; mem = (ull *)calloc(ww * sizeof(ull), h) ; for (auto v: yr) for (int i=0; i> 6)] |= 1LL << (v & 63) ; vector> lev0, lev1, lev2 ; for (int i=0; i> 6)] |= 1LL << (x1 & 63) ; for (int d=1; ; d++) { if (lev1.size() == 0) break ; thispart += lev1.size() ; for (auto [x, y]: lev1) { for (int dd=0; dd<8; dd++) { auto x2 = x + dx[dd] ; auto y2 = y + dy[dd] ; if (0 == ((mem[y2 * ww + (x2 >> 6)] >> (x2 & 63)) & 1)) { mem[y2 * ww + (x2 >> 6)] |= 1LL << (x2 & 63) ; lev2.push_back({x2, y2}) ; } } } swap(lev0, lev1) ; swap(lev1, lev2) ; lev2.clear() ; } if (argc > 1) cerr << "Part " << thispart << endl ; for (int j=i; j> 6)] >> (x1 & 63)) & 1) ; int se = ((mem[y2 * ww + (x2 >> 6)] >> (x2 & 63)) & 1) ; if (sf || se) { if (sf && se) { solved[j] = 3 ; } else { solved[j] = 1 ; } } } } for (auto v: solved) cout << (v >> 1) << endl ; }