Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace std;
#define ll long long int
#define ld long double
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    in_set;
#define rep(n) for (ll i = 0; i < (n); i++)
#define jrep(n) for (ll j = 0; j < (n); j++)
#define krep(n) for (ll k = 0; k < (n); k++)
#define all(a) (a).begin(), (a).end()
#define dpp(a) cout << (a).first << " " << (a).second << endl;
#define append push_back
#define mod 2000007
#define ff first
#define ss second
#define ffast                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fast ios::sync_with_stdio(0);
ll pw(ll x, ll y, ll p = mod) {
  ll r = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      r = (r * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return r;
}
vector<ll> uniq(vector<ll> arr) {
  vector<ll> brr;
  sort(all(arr));
  brr.append(arr[0]);
  for (auto i : arr)
    if (brr.back() != i)
      brr.append(i);
  return brr;
}

ll h, w;
ll comb(ll x, ll y) { return 2000 * x + y; }
vector<ll> lnb(ll a) {
  ll x = a / 2000;
  ll y = a % 2000;
  vector<ll> ret;
  if (x + 1 <= h)
    ret.append(comb(x + 1, y));
  if (y + 1 <= w)
    ret.append(comb(x, y + 1));
  if (x - 1 > 0)
    ret.append(comb(x - 1, y));
  if (y - 1 > 0)
    ret.append(comb(x, y - 1));
  return ret;
}

vector<ll> llnb(ll a) {
  ll x = a / 2000;
  ll y = a % 2000;
  vector<ll> ret;
  for (ll i = -2; i <= 2; i++)
    for (ll j = -2; j <= 2; j++)
      if (i != 0 || j != 0) {
        if (x + i <= h && x + i > 0 && y + j <= w && y + j > 0)
          ret.append(comb(x + i, y + j));
      }
  return ret;
}

ll dist[4000005];
void solve(ll ts) {
  cin >> h >> w;
  rep(4000005) dist[i] = 1000000000;
  ll ch, cw;
  cin >> ch >> cw;
  ll dh, dw;
  cin >> dh >> dw;
  vector<string> mat(h);
  rep(h) cin >> mat[i];
  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
  dist[comb(ch, cw)] = 0;
  pq.push(make_pair((ll)0, comb(ch, cw)));
  while (!pq.empty()) {
    ll a = pq.top().second;
    pq.pop();
    //        cout<<a<<endl;
    for (auto i : lnb(a)) {
      if (mat[i / 2000 - 1][i % 2000 - 1] == '.') {
        ll w = 0;
        if (dist[i] > dist[a] + w) {
          dist[i] = dist[a] + w;
          pq.push(make_pair(dist[i], i));
        }
      }
    }
    for (auto i : llnb(a)) {
      if (mat[i / 2000 - 1][i % 2000 - 1] == '.') {
        ll w = 1;
        if (dist[i] > dist[a] + w) {
          dist[i] = dist[a] + w;
          pq.push(make_pair(dist[i], i));
        }
      }
    }
  }
  //    rep(h){ jrep(w) cout<<dist[comb(i+1,j+1)]<<" "; cout<<endl; }
  if (dist[comb(dh, dw)] < 1000000000) {
    cout << dist[comb(dh, dw)] << endl;
  } else
    cout << -1 << endl;
}

int main() {
  ffast ll t = 1;
  //    cin>>t;
  rep(t) solve(i + 1);
}

// *p.find_by_order(n) : nth value (0 indexed)
// p.order_of_key(7): index of 7 (0 indexed)


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace std;
#define ll long long int
#define ld long double
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    in_set;
#define rep(n) for (ll i = 0; i < (n); i++)
#define jrep(n) for (ll j = 0; j < (n); j++)
#define krep(n) for (ll k = 0; k < (n); k++)
#define all(a) (a).begin(), (a).end()
#define dpp(a) cout << (a).first << " " << (a).second << endl;
#define append push_back
#define mod 2000007
#define ff first
#define ss second
#define ffast                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fast ios::sync_with_stdio(0);
ll pw(ll x, ll y, ll p = mod) {
  ll r = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      r = (r * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return r;
}
vector<ll> uniq(vector<ll> arr) {
  vector<ll> brr;
  sort(all(arr));
  brr.append(arr[0]);
  for (auto i : arr)
    if (brr.back() != i)
      brr.append(i);
  return brr;
}

ll h, w;
ll comb(ll x, ll y) { return 2000 * x + y; }
vector<ll> lnb(ll a) {
  ll x = a / 2000;
  ll y = a % 2000;
  vector<ll> ret;
  if (x + 1 <= h)
    ret.append(comb(x + 1, y));
  if (y + 1 <= w)
    ret.append(comb(x, y + 1));
  if (x - 1 > 0)
    ret.append(comb(x - 1, y));
  if (y - 1 > 0)
    ret.append(comb(x, y - 1));
  return ret;
}

vector<ll> llnb(ll a) {
  ll x = a / 2000;
  ll y = a % 2000;
  vector<ll> ret;
  for (ll i = -2; i <= 2; i++)
    for (ll j = -2; j <= 2; j++)
      if (i != 0 || j != 0) {
        if (x + i <= h && x + i > 0 && y + j <= w && y + j > 0)
          ret.append(comb(x + i, y + j));
      }
  return ret;
}

ll dist[2000005];
void solve(ll ts) {
  cin >> h >> w;
  rep(2000005) dist[i] = 1000000000;
  ll ch, cw;
  cin >> ch >> cw;
  ll dh, dw;
  cin >> dh >> dw;
  vector<string> mat(h);
  rep(h) cin >> mat[i];
  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
  dist[comb(ch, cw)] = 0;
  pq.push(make_pair((ll)0, comb(ch, cw)));
  while (!pq.empty()) {
    ll a = pq.top().second;
    pq.pop();
    //        cout<<a<<endl;
    for (auto i : lnb(a)) {
      if (mat[i / 2000 - 1][i % 2000 - 1] == '.') {
        ll w = 0;
        if (dist[i] > dist[a] + w) {
          dist[i] = dist[a] + w;
          pq.push(make_pair(dist[i], i));
        }
      }
    }
    for (auto i : llnb(a)) {
      //            cout<<i<<endl;
      if (mat[i / 2000 - 1][i % 2000 - 1] == '.') {
        ll w = 1;
        if (dist[i] > dist[a] + w) {
          dist[i] = dist[a] + w;
          pq.push(make_pair(dist[i], i));
        }
      }
    }
  }
  //    rep(h){ jrep(w) cout<<dist[comb(i+1,j+1)]<<" "; cout<<endl; }
  if (dist[comb(dh, dw)] < 1000000000) {
    cout << dist[comb(dh, dw)] << endl;
  } else
    cout << -1 << endl;
}

int main() {
  ffast ll t = 1;
  //    cin>>t;
  rep(t) solve(i + 1);
}

// *p.find_by_order(n) : nth value (0 indexed)
// p.order_of_key(7): index of 7 (0 indexed)