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

#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <random>

using namespace std;
using namespace __gnu_pbds;

typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    orderedSet;

#define pb push_back
#define F first
#define S second
#define all(a) (a).begin(), (a).end()

#define for1(i0, l0, r0) for (int i0 = l0; i0 < r0; ++i0)
#define for2(i0, l0, r0) for (int i0 = l0; i0 <= r0; ++i0)
#define forn(i0, n0) for (int i0 = 0; i0 < n0; ++i0)
#define forn1(i0, n0) for (int i0 = 1; i0 < n0; ++i0)
#define forr(i0, n0) for (int i0 = n0; i0 >= 0; --i0)
#define forr1(i0, r0, l0) for (int i0 = r0; i0 > l0; --i0)
#define forr2(i0, r0, l0) for (int i0 = r0; i0 >= l0; --i0)

#define Sort(a) sort(all(a))
#define Reverse(a) reverse(all(a))
#define relaxMax(a, b) a = max(a, b)
#define relaxMin(a, b) a = min(a, b)

typedef long long ll;
typedef long double ld;
#define ui unsigned int
#define ull unsigned long long

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef pair<string, string> pss;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;

typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pdd> vpdd;

typedef vector<vi> vvi;
typedef vector<vd> vvd;
typedef vector<vll> vvll;
typedef vector<vb> vvb;

#define vpss vector<pss>
#define vvs vector<vs>
#define vvpii vector<vpii>
#define vvpll vector<vpll>
#define vpt vector<pt>
#define vvvi vector<vvi>
#define vsi vector<set<int>>
#define ss second
#define ff first

#define printvi(arr)                                                           \\
  for (int x0 : arr)                                                           \\
    cout << x0 << ' ';                                                         \\
  cout << '\\n';
#define printvll(arr)                                                          \\
  for (ll x0 : arr)                                                            \\
    cout << x0 << ' ';                                                         \\
  cout << '\\n';
#define printpair(pair0) cout << pair0.F << ' ' << pair0.S << '\\n';
#define printvp(arr)                                                           \\
  for (auto pair0 : arr)                                                       \\
    printpair(pair0);
#define initArray(arr, N0, X0) memset(arr, X0, N0 * sizeof(int))
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // cout.precision(16);
  // freopen("amusing.in", "r", stdin);
  // freopen("amusing.out", "w", stdout);
}

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vs a(n);
  int X1, Y1, X2, Y2;
  cin >> X1 >> Y1 >> X2 >> Y2;
  X1--, Y1--, X2--, Y2--;
  forn(i, n) cin >> a[i];
  queue<pii> q;
  q.push({X1, Y1});
  vvi d(n, vi(m, -1));
  d[X1][Y1] = 0;
  vvi lr(n), lc(m);
  vector<set<int>> r(n), c(m);
  forn(i, n) {
    forn(j, m) {
      if (a[i][j] == '@') {
        lr[i].pb(j);
        lc[j].pb(i);
      } else {
        r[i].insert(j);
        c[j].insert(i);
      }
    }
  }
  forn(i, n) r[i].insert(1e9);
  forn(i, m) c[i].insert(1e9);
  r[X1].erase(Y1);
  c[Y1].erase(X1);
  while (!q.empty()) {
    int x = q.front().F, y = q.front().S;
    q.pop();
    int y1 = max(0, y - k), y2 = min(m - 1, y + k);
    if (!lr[x].empty()) {
      auto ub = upper_bound(all(lr[x]), y);
      if (lr[x].back() >= y)
        y2 = min(y2, *ub - 1);
      if (lr[x][0] <= y)
        y1 = max(y1, 1 + *(--ub));
    }
    while (!r[x].empty()) {
      int Y = *r[x].lower_bound(y1);
      if (Y > y2)
        break;
      r[x].erase(Y);
      c[Y].erase(x);
      q.push({x, Y});
      d[x][Y] = 1 + d[x][y];
    }
    int x1 = max(0, x - k), x2 = min(n - 1, x + k);
    if (!lc[y].empty()) {
      auto ub = upper_bound(all(lc[y]), x);
      if (lc[y].back() >= x)
        x2 = min(x2, *ub - 1);
      if (lc[y][0] <= x)
        x1 = max(x1, 1 + *(--ub));
    }
    while (!c[y].empty()) {
      int X = *c[y].lower_bound(x1);
      if (X > x2)
        break;
      c[y].erase(X);
      r[X].erase(y);
      q.push({X, y});
      d[X][y] = 1 + d[x][y];
    }
    if (d[X2][Y2] != -1)
      break;
  }
  cout << d[X2][Y2] << '\\n';
}

bool multitest = false;

int main() {
  init();
  int t = 1;
  if (multitest)
    cin >> t;
  forn(i, t) solve();
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <random>

using namespace std;
using namespace __gnu_pbds;

typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    orderedSet;

#define pb push_back
#define F first
#define S second
#define all(a) (a).begin(), (a).end()

#define for1(i0, l0, r0) for (int i0 = l0; i0 < r0; ++i0)
#define for2(i0, l0, r0) for (int i0 = l0; i0 <= r0; ++i0)
#define forn(i0, n0) for (int i0 = 0; i0 < n0; ++i0)
#define forn1(i0, n0) for (int i0 = 1; i0 < n0; ++i0)
#define forr(i0, n0) for (int i0 = n0; i0 >= 0; --i0)
#define forr1(i0, r0, l0) for (int i0 = r0; i0 > l0; --i0)
#define forr2(i0, r0, l0) for (int i0 = r0; i0 >= l0; --i0)

#define Sort(a) sort(all(a))
#define Reverse(a) reverse(all(a))
#define relaxMax(a, b) a = max(a, b)
#define relaxMin(a, b) a = min(a, b)

typedef long long ll;
typedef long double ld;
#define ui unsigned int
#define ull unsigned long long

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef pair<string, string> pss;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;

typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pdd> vpdd;

typedef vector<vi> vvi;
typedef vector<vd> vvd;
typedef vector<vll> vvll;
typedef vector<vb> vvb;

#define vpss vector<pss>
#define vvs vector<vs>
#define vvpii vector<vpii>
#define vvpll vector<vpll>
#define vpt vector<pt>
#define vvvi vector<vvi>
#define vsi vector<set<int>>
#define ss second
#define ff first

#define printvi(arr)                                                           \\
  for (int x0 : arr)                                                           \\
    cout << x0 << ' ';                                                         \\
  cout << '\\n';
#define printvll(arr)                                                          \\
  for (ll x0 : arr)                                                            \\
    cout << x0 << ' ';                                                         \\
  cout << '\\n';
#define printpair(pair0) cout << pair0.F << ' ' << pair0.S << '\\n';
#define printvp(arr)                                                           \\
  for (auto pair0 : arr)                                                       \\
    printpair(pair0);
#define initArray(arr, N0, X0) memset(arr, X0, N0 * sizeof(int))
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // cout.precision(16);
  // freopen("amusing.in", "r", stdin);
  // freopen("amusing.out", "w", stdout);
}

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vs a(n);
  int X1, Y1, X2, Y2;
  cin >> X1 >> Y1 >> X2 >> Y2;
  X1--, Y1--, X2--, Y2--;
  forn(i, n) cin >> a[i];
  queue<pii> q;
  q.push({X1, Y1});
  vvi d(n, vi(m, -1));
  d[X1][Y1] = 0;
  vvi lr(n), lc(m);
  vector<set<int>> r(n), c(m);
  forn(i, n) {
    forn(j, m) {
      if (a[i][j] == '@') {
        lr[i].pb(j);
        lc[j].pb(i);
      } else {
        r[i].insert(j);
        c[j].insert(i);
      }
    }
  }
  forn(i, n) r[i].insert(1e9);
  forn(i, m) c[i].insert(1e9);
  r[X1].erase(Y1);
  c[Y1].erase(X1);
  while (!q.empty()) {
    int x = q.front().F, y = q.front().S;
    q.pop();
    int y1 = max(0, y - k), y2 = min(m - 1, y + k);
    if (!lr[x].empty()) {
      auto ub = upper_bound(all(lr[x]), y);
      if (lr[x].back() >= y)
        y2 = min(y2, *ub - 1);
      if (lr[x][0] <= y)
        y1 = max(y1, 1 + *(--ub));
    }
    while (!r[x].empty()) {
      int Y = *lower_bound(all(r[x]), y1);
      if (Y > y2)
        break;
      r[x].erase(Y);
      c[Y].erase(x);
      q.push({x, Y});
      d[x][Y] = 1 + d[x][y];
    }
    int x1 = max(0, x - k), x2 = min(n - 1, x + k);
    if (!lc[y].empty()) {
      auto ub = upper_bound(all(lc[y]), x);
      if (lc[y].back() >= x)
        x2 = min(x2, *ub - 1);
      if (lc[y][0] <= x)
        x1 = max(x1, 1 + *(--ub));
    }
    while (!c[y].empty()) {
      int X = *lower_bound(all(c[y]), x1);
      if (X > x2)
        break;
      c[y].erase(X);
      r[X].erase(y);
      q.push({X, y});
      d[X][y] = 1 + d[x][y];
    }
    if (d[X2][Y2] != -1)
      break;
  }
  cout << d[X2][Y2] << '\\n';
}

bool multitest = false;

int main() {
  init();
  int t = 1;
  if (multitest)
    cin >> t;
  forn(i, t) solve();
}