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

// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
using pii = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
#define all(c) begin(c), end(c)
#define rall(c) rbegin(c), rend(c)
#define fore(x, c) for(auto &&x : c)
#define rep(i, a, n) for(int i = a, i##len = (int)(n); i < i##len; ++i)
#define rrep(i, a, n) for(int i = (int)(n - 1); i >= a; --i)
#define sz(c) ((int)c.size())
#define contains(c, x) (c.find(x) != end(c))
#define inseg(l, x, r) ((l) <= (x) && (x) < (r))
#define dump(...)
#define pb push_back
#define eb emplace_back
#define _ 0
const signed INF_ = 1001001001; const long long INF = 1001001001001001001LL;
const int DX[9] = {0,1,0,-1,1,1,-1,-1,0}, DY[9] = {-1,0,1,0,-1,1,1,-1,0};
template<class T> ostream& operator<<(ostream &os, const vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++) os<<*i<<(i==end(v)-1?"":" ");return os;}
template<class T> istream& operator>>(istream &is, vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++) is>>*i;return is;}
template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) {
  is>>p.first>>p.second;return is;}
template<class T, class U> bool chmax(T &a, const U &b){return a<b?a=b,1:0;}
template<class T, class U> bool chmin(T &a, const U &b){return a>b?a=b,1:0;}
template <class T> void psum(T& c) {partial_sum(begin(c), end(c), begin(c));}
template<class T> using heap = priority_queue<T, deque<T>, greater<T>>;
struct before_main_function {
  before_main_function() {
      cin.tie(nullptr); ios::sync_with_stdio(0);
      cout << setprecision(15) << fixed;
  }
} before_main_function;
// clang-format on
//------------------8<------------------------------------8<--------------------

signed main() {
  int H, W, K;
  cin >> H >> W >> K;
  int X1, Y1, X2, Y2;
  cin >> X1 >> Y1 >> X2 >> Y2;
  X1--, Y1--, X2--, Y2--;
  vector<string> C(H);
  cin >> C;

  vvi dist(H, vi(W, INF));
  queue<pii> q;
  dist[X1][Y1] = 0;
  q.push({X1, Y1});

  vector<set<int>> row(H), col(W);
  rep(i, 0, H) {
    row[i].insert(-INF);
    row[i].insert(INF);
    rep(j, 0, W) row[i].insert(j);
  }
  rep(i, 0, W) {
    col[i].insert(-INF);
    col[i].insert(INF);
    rep(j, 0, H) col[i].insert(j);
  }
  row[X1].erase(Y1);
  col[Y1].erase(X1);

  while (sz(q)) {
    int x, y;
    tie(x, y) = q.front();
    if (x == X2 && y == Y2) {
      cout << dist[x][y] << endl;
      return 0;
    }
    q.pop();

    { // up
      auto itr = prev(col[y].lower_bound(x));
      while (!(*itr == -INF || C[*itr][y] == '@' || abs(x - *itr) > K)) {
        dist[*itr][y] = dist[x][y] + 1;
        q.push({*itr, y});
        row[*itr].erase(y);
        itr = col[y].erase(itr);
        itr--;
      }
    }
    { // down
      auto itr = col[y].lower_bound(x);
      while (!(*itr == INF || C[*itr][y] == '@' || abs(x - *itr) > K)) {
        dist[*itr][y] = dist[x][y] + 1;
        q.push({*itr, y});
        row[*itr].erase(y);
        itr = col[y].erase(itr);
      }
    }
    { // left
      auto itr = prev(row[x].lower_bound(y));
      while (!(*itr == -INF || C[x][*itr] == '@' || abs(y - *itr) > K)) {
        dist[x][*itr] = dist[x][y] + 1;
        q.push({x, *itr});
        col[*itr].erase(x);
        itr = row[x].erase(itr);
        itr--;
      }
    }
    { // right
      auto itr = row[x].lower_bound(y);
      while (!(*itr == INF || C[x][*itr] == '@' || abs(y - *itr) > K)) {
        dist[x][*itr] = dist[x][y] + 1;
        q.push({x, *itr});
        col[*itr].erase(x);
        itr = row[x].erase(itr);
      }
    }
  }
  cout << -1 << endl;
  return (0 ^ _ ^ 0);
}


Error: TLE

Fixed submission:

// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
using pii = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
#define all(c) begin(c), end(c)
#define rall(c) rbegin(c), rend(c)
#define fore(x, c) for(auto &&x : c)
#define rep(i, a, n) for(int i = a, i##len = (int)(n); i < i##len; ++i)
#define rrep(i, a, n) for(int i = (int)(n - 1); i >= a; --i)
#define sz(c) ((int)c.size())
#define contains(c, x) (c.find(x) != end(c))
#define inseg(l, x, r) ((l) <= (x) && (x) < (r))
#define dump(...)
#define pb push_back
#define eb emplace_back
#define _ 0
const signed INF_ = 1001001001; const long long INF = 1001001001001001001LL;
const int DX[9] = {0,1,0,-1,1,1,-1,-1,0}, DY[9] = {-1,0,1,0,-1,1,1,-1,0};
template<class T> ostream& operator<<(ostream &os, const vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++) os<<*i<<(i==end(v)-1?"":" ");return os;}
template<class T> istream& operator>>(istream &is, vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++) is>>*i;return is;}
template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) {
  is>>p.first>>p.second;return is;}
template<class T, class U> bool chmax(T &a, const U &b){return a<b?a=b,1:0;}
template<class T, class U> bool chmin(T &a, const U &b){return a>b?a=b,1:0;}
template <class T> void psum(T& c) {partial_sum(begin(c), end(c), begin(c));}
template<class T> using heap = priority_queue<T, deque<T>, greater<T>>;
struct before_main_function {
  before_main_function() {
      cin.tie(nullptr); ios::sync_with_stdio(0);
      cout << setprecision(15) << fixed;
  }
} before_main_function;
// clang-format on
//------------------8<------------------------------------8<--------------------

signed main() {
  int H, W, K;
  cin >> H >> W >> K;
  int X1, Y1, X2, Y2;
  cin >> X1 >> Y1 >> X2 >> Y2;
  X1--, Y1--, X2--, Y2--;
  vector<string> C(H);
  cin >> C;

  vvi dist(H, vi(W, INF));
  queue<pii> q;
  dist[X1][Y1] = 0;
  q.push({X1, Y1});
  while (sz(q)) {
    int x, y;
    tie(x, y) = q.front();
    if (x == X2 && y == Y2) {
      cout << dist[x][y] << endl;
      return 0;
    }
    q.pop();
    rep(dir, 0, 4) {
      rep(k, 1, K + 1) {
        int xx = x + DX[dir] * k;
        int yy = y + DY[dir] * k;
        if (!inseg(0, xx, H) || !inseg(0, yy, W))
          break;
        if (C[xx][yy] == '@')
          break;
        if (chmin(dist[xx][yy], dist[x][y] + 1)) {
          q.push({xx, yy});
        }
      }
    }
  }
  cout << -1 << endl;
  return (0 ^ _ ^ 0);
}