Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

#define REP(k, n) for (ll k = 0; k < (ll)(n); ++k)
#define REPREV(k, n) for (ll k = (ll)(n)-1; k >= 0; --k)
#define FOR(k, a, b) for (ll k = (ll)(a); k < (ll)(b); ++k)
#define FORREV(k, a, b) for (ll k = (ll)(b)-1; k >= (ll)(a); --k)
#define FOREACH(e, v) for (auto &e : v)

#define TGET(t, i) get<i>(t)
#define TSET(t, i, a) get<i>(t) = a
// MinHeap - priority_queue<ll, vll, greater<ll>> pq;

#define EVEN(n) ((n)&1) == 0
#define ODD(n) ((n)&1) == 1
#define MOD(x, m) ((x) % (m) + (m)) % (m)

#define SORT(v) sort(v.begin(), v.end())
#define SORTREV(v) sort(v.rbegin(), v.rend())
#define SHUFFLE(v) random_shuffle(v.begin(), v.end())
// srand(unsigned(time(0)));

#define REMOVEAT(v, i) v.erase(v.begin() + i)
#define REVERSE(v) reverse(v.begin(), v.end())

template <typename T> void join(vector<T> &arr, string sep) {
  bool first = true;
  for (auto t : arr) {
    if (first) {
      first = false;
      cout << t;
    } else {
      cout << sep << t;
    }
  }
  cout << "\\n";
}

#define POS "POSSIBLE"
#define IMPOS "IMPOSSIBLE"

// #define INF LLONG_MAX
// #define MINF LLONG_MIN
#define INF 9e18
#define MINF -INF
vvll directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void solve() {
  ll h, w, k, x1, y1, x2, y2;
  cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vvll g(h, vll(w));
  REP(i, h) {
    REP(j, w) {
      char c;
      cin >> c;
      if (c == '.') {
        g[i][j] = 0;
      } else {
        g[i][j] = 1;
      }
    }
  }

  if (g[x1][y1] || g[x2][y2]) {
    cout << "-1\\n";
    return;
  }
  vector<map<ll, ll>> wdone(w);
  vector<map<ll, ll>> hdone(h);
  REP(i, h) {
    REP(j, w) {
      wdone[j][i] = g[i][j];
      hdone[i][j] = g[i][j];
    }
  }
  vvll dist(h, vll(w, -1));
  dist[x1][y1] = 0;
  vector<pll> stk = {{x1, y1}};
  hdone[x1].erase(y1);
  wdone[y1].erase(x1);
  while (!stk.empty()) {
    vector<pll> nxt;
    FOREACH(p, stk) {
      ll i = p.first;
      ll j = p.second;
      // auto it = hdone[i].lower_bound(j);
      auto rdown = hdone[i].lower_bound(j); // it;
      map<ll, ll>::reverse_iterator down(rdown);
      vll remove;
      while (down != hdone[i].rend()) {
        if (down->second)
          break;
        ll dj = down->first;
        if (abs(j - dj) > k)
          break;
        remove.push_back(dj);
        dist[i][dj] = dist[i][j] + 1;
        nxt.push_back({i, dj});
        ++down;
      }
      auto up = hdone[i].lower_bound(j);
      ;
      while (up != hdone[i].end()) {
        if (up->second)
          break;
        ll dj = up->first;
        if (abs(j - dj) > k)
          break;
        remove.push_back(dj);
        dist[i][dj] = dist[i][j] + 1;
        nxt.push_back({i, dj});
        up++;
      }
      FOREACH(jj, remove) {
        hdone[i].erase(jj);
        wdone[jj].erase(i);
      }
      remove.clear();
      rdown = wdone[j].lower_bound(i);
      map<ll, ll>::reverse_iterator down2(rdown);
      while (down2 != wdone[j].rend()) {
        if (down2->second)
          break;
        ll di = down2->first;
        if (abs(i - di) > k)
          break;
        remove.push_back(di);
        dist[di][j] = dist[i][j] + 1;
        nxt.push_back({di, j});
        down2++;
      }
      up = wdone[j].lower_bound(i);
      while (up != wdone[j].end()) {
        if (up->second)
          break;
        ll di = up->first;
        if (abs(i - di) > k)
          break;
        remove.push_back(di);
        dist[di][j] = dist[i][j] + 1;
        nxt.push_back({di, j});
        up++;
      }
      FOREACH(ii, remove) {
        wdone[j].erase(ii);
        hdone[ii].erase(j);
      }
    }
    stk = nxt;
  }

  // cout << fixed << setprecision(6);
  cout << dist[x2][y2] << "\\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt = 1;
  // cin >> tt;
  for (int t = 1; t <= tt; t++) {
    // cout << "Case #" << t << ": ";
    solve();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

#define REP(k, n) for (ll k = 0; k < (ll)(n); ++k)
#define REPREV(k, n) for (ll k = (ll)(n)-1; k >= 0; --k)
#define FOR(k, a, b) for (ll k = (ll)(a); k < (ll)(b); ++k)
#define FORREV(k, a, b) for (ll k = (ll)(b)-1; k >= (ll)(a); --k)
#define FOREACH(e, v) for (auto e : v)

#define TGET(t, i) get<i>(t)
#define TSET(t, i, a) get<i>(t) = a
// MinHeap - priority_queue<ll, vll, greater<ll>> pq;

#define EVEN(n) ((n)&1) == 0
#define ODD(n) ((n)&1) == 1
#define MOD(x, m) ((x) % (m) + (m)) % (m)

#define SORT(v) sort(v.begin(), v.end())
#define SORTREV(v) sort(v.rbegin(), v.rend())
#define SHUFFLE(v) random_shuffle(v.begin(), v.end())
// srand(unsigned(time(0)));

#define REMOVEAT(v, i) v.erase(v.begin() + i)
#define REVERSE(v) reverse(v.begin(), v.end())

template <typename T> void join(vector<T> &arr, string sep) {
  bool first = true;
  for (auto t : arr) {
    if (first) {
      first = false;
      cout << t;
    } else {
      cout << sep << t;
    }
  }
  cout << "\\n";
}

#define POS "POSSIBLE"
#define IMPOS "IMPOSSIBLE"

// #define INF LLONG_MAX
// #define MINF LLONG_MIN
#define INF 9e18
#define MINF -INF
vvll directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void solve() {
  ll h, w, k, x1, y1, x2, y2;
  cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vvll g(h, vll(w));
  REP(i, h) {
    REP(j, w) {
      char c;
      cin >> c;
      if (c == '.') {
        g[i][j] = false;
      } else {
        g[i][j] = true;
      }
    }
  }

  if (g[x1][y1] || g[x2][y2]) {
    cout << "-1\\n";
    return;
  }
  vvll dist(h, vll(w, -1));
  priority_queue<vll, vvll, greater<>> pq;
  dist[x1][y1] = 0;
  pq.push({0, x1, y1});
  while (!pq.empty()) {
    auto &t = pq.top();
    ll d = t[0];
    ll i = t[1];
    ll j = t[2];
    pq.pop();
    if (i == x2 && j == y2) {
      break;
    }
    if (dist[i][j] != d)
      continue;
    for (int di = 1; di <= k; di++) {
      if (i + di >= h || g[i + di][j])
        break;
      if (dist[i + di][j] != -1 && dist[i + di][j] <= d)
        break;
      dist[i + di][j] = d + 1;
      pq.push({d + 1, i + di, j});
    }
    for (int di = 1; di <= k; di++) {
      if (i - di < 0 || g[i - di][j])
        break;
      if (dist[i - di][j] != -1 && dist[i - di][j] <= d)
        break;
      dist[i - di][j] = d + 1;
      pq.push({d + 1, i - di, j});
    }
    for (int dj = 1; dj <= k; dj++) {
      if (j + dj >= w || g[i][j + dj])
        break;
      if (dist[i][j + dj] != -1 && dist[i][j + dj] <= d)
        break;
      dist[i][j + dj] = d + 1;
      pq.push({d + 1, i, j + dj});
    }
    for (int dj = 1; dj <= k; dj++) {
      if (j - dj < 0 || g[i][j - dj])
        break;
      if (dist[i][j - dj] != -1 && dist[i][j - dj] <= d)
        break;
      dist[i][j - dj] = d + 1;
      pq.push({d + 1, i, j - dj});
    }
  }

  // cout << fixed << setprecision(6);
  cout << dist[x2][y2] << "\\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt = 1;
  // cin >> tt;
  for (int t = 1; t <= tt; t++) {
    // cout << "Case #" << t << ": ";
    solve();
  }

  return 0;
}