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

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define xx first
#define yy second
#define sci(n) scanf("%d", &n)
#define scii(n, m) scanf("%d%d", &n, &m)
#define sciii(n, m, w) scanf("%d%d%d", &n, &m, &w)
#define scl(n) scanf("%lld", &n)
#define scll(n, m) scanf("%lld%lld", &n, &m)
#define sclll(n, m, w) scanf("%lld%lld%lld", &n, &m, &w)
#define pf(a) printf("%d\\n", a)
#define CASE(a) printf("Case %d:\\n", a)
#define dbg(i) printf("yo %lld\\n", i)
#define endl '\\n'
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout);
#define pi acos(-1)
#define mem(a, b) memset(a, b, sizeof(a))
#define SQR(a) (a) * (a)
#define all(v) v.begin(), v.end()
#define pb push_back
#define pri priority_queue<int>
#define rev_pri priority_queue<int, vector<int>, greater<int>>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define mod 1000000007
#define inf INT_MAX
#define eps 1e-9
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define snuke(c, itr)                                                          \\
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)

using namespace __gnu_pbds;
using namespace std;

int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
// ll ncr(ll n,ll r){
// if(n==r)return 1;
// if(r==1)return n;
// if(dp[n][r]!=-1)return dp[n][r];
// return dp[n][r]=ncr(n-1,r)+ncr(n-1,r-1);
//}

bool checkbit(int mask, int bit) { return mask & (1 << bit); }
int setbit(int mask, int bit) { return mask | (1 << bit); }
int clearbit(int mask, int bit) { return mask & ~(1 << bit); }
int togglebit(int mask, int bit) { return mask ^ (1 << bit); }
int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(ll t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }

// #define CAPITAL
void yes(bool ex = true) {
#ifdef CAPITAL
  cout << "YES"
       << "\\n";
#else
  cout << "Yes"
       << "\\n";
#endif
  if (ex)
    exit(0);
}
void no(bool ex = true) {
#ifdef CAPITAL
  cout << "NO"
       << "\\n";
#else
  cout << "No"
       << "\\n";
#endif
  if (ex)
    exit(0);
}
void possible(bool ex = true) {
#ifdef CAPITAL
  cout << "POSSIBLE"
       << "\\n";
#else
  cout << "Possible"
       << "\\n";
#endif
  if (ex)
    exit(0);
}
void impossible(bool ex = true) {
#ifdef CAPITAL
  cout << "IMPOSSIBLE"
       << "\\n";
#else
  cout << "Impossible"
       << "\\n";
#endif
  if (ex)
    exit(0);
}

template <typename T> T BigMod(T b, T p, T m) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {
    T s = BigMod(b, p / 2, m);
    return ((s % m) * (s % m)) % m;
  }
  return ((b % m) * (BigMod(b, p - 1, m) % m)) % m;
}
template <typename T> T ModInv(T b, T m) { return BigMod(b, m - 2, m); }
template <typename T> T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9')
      break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9')
      break;
  }
  return (ng ? -n : n);
}
template <typename T> T POW(T B, T P) {
  if (P == 0)
    return 1;
  if (P & 1)
    return B * POW(B, P - 1);
  else
    return SQR(POW(B, P / 2));
}
template <typename T> T Bigmod(T b, T p, T m) {
  if (p == 0)
    return 1;
  else if (!(p & 1))
    return SQR(Bigmod(b, p / 2, m)) % m;
  else
    return ((b % m) * Bigmod(b, p - 1, m)) % m;
}
template <typename T> T Dis(T x1, T y1, T x2, T y2) {
  return sqrt(SQR(x1 - x2) + SQR(y1 - y2));
}
template <typename T> T Angle(T x1, T y1, T x2, T y2) {
  return atan(double(y1 - y2) / double(x1 - x2));
}
template <typename T> T DIFF(T a, T b) {
  T d = a - b;
  if (d < 0)
    return -d;
  else
    return d;
}
template <typename T> T ABS(T a) {
  if (a < 0)
    return -a;
  else
    return a;
}
template <typename T> T euclide(T a, T b, T &x, T &y) {
  if (a < 0) {
    T d = euclide(-a, b, x, y);
    x = -x;
    return d;
  }
  if (b < 0) {
    T d = euclide(a, -b, x, y);
    y = -y;
    return d;
  }
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    T d = euclide(b, a % b, x, y);
    T t = x;
    x = y;
    y = t - (a / b) * y;
    return d;
  }
}
template <typename T> void ia(T a[], int n) {
  for (int i = 0; i < n; i++)
    cin >> a[i];
}
template <typename T> void pa(T a[], int n) {
  for (int i = 0; i < n - 1; i++)
    cout << a[i] << " ";
  cout << a[n - 1] << endl;
}
template <typename T> ll isLeft(T a, T b, T c) {
  return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
template <typename T> T gcd(T a, T b) {
  if (a < 0)
    return gcd(-a, b);
  if (b < 0)
    return gcd(a, -b);
  return (b == 0) ? a : gcd(b, a % b);
}
template <typename T> T lcm(T a, T b) {
  if (a < 0)
    return lcm(-a, b);
  if (b < 0)
    return lcm(a, -b);
  return a * (b / gcd(a, b));
}

template <class t> int lwb(const vector<t> &v, const t &a) {
  return lower_bound(all(v), a) - v.bg;
}

template <typename A, typename B> class Dijkstra {
public:
  vector<vector<pair<int, A>>> vertexs;
  B Cost_Function;
  Dijkstra(int n, B cost) : Cost_Function(cost) {
    vertexs = vector<vector<pair<int, A>>>(n, vector<pair<int, A>>{});
  }
  ~Dijkstra() { vertexs.clear(); }
  void add_edge(int a, int b, A c) { vertexs[a].push_back(mp(b, c)); }
  vector<ll> build_result(int StartPoint) {
    vector<ll> dist(vertexs.size(), 2e18);
    dist[StartPoint] = 0;
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
        next;
    next.push(make_pair(0, StartPoint));
    while (next.empty() == false) {
      pair<ll, int> now = next.top();
      next.pop();
      if (dist[now.second] != now.first)
        continue;
      for (auto x : vertexs[now.second]) {
        ll now_cost = now.first + Cost_Function(x.second);
        if (dist[x.first] > now_cost) {
          dist[x.first] = now_cost;
          next.push(mp(now_cost, x.first));
        }
      }
    }
    return dist;
  }
};

#define mxn 100005

// bool vis[1LL << 19];
const ll nax = 1000050;
ll n, w, m;
ll ar[nax];
ll h, k;
vector<char> v[nax];
vector<ll> ds[nax];
pair<ll, pll> pr;
void bfs(ll hn, ll hm, ll sx, ll sy) {
  queue<pair<ll, pair<ll, ll>>> pq;
  pq.push({0, {hn, hm}});
  ll crds, x, y, cn;
  while (!pq.empty()) {
    pr = pq.front();
    pq.pop();
    crds = pr.xx;
    x = pr.yy.xx;
    y = pr.yy.yy;
    for (ll i = x + 1, cn = 1; i <= h && cn <= k; i++, cn++) {
      if (v[i][y] == '@')
        break;
      if (ds[i][y] > crds + 1) {
        ds[i][y] = crds + 1;
        pq.push({ds[i][y], {i, y}});
      } else if (ds[i][y] < crds)
        break;
    }
    for (ll i = x - 1, cn = 1; i >= 1 && cn <= k; i--, cn++) {
      if (v[i][y] == '@')
        break;
      if (ds[i][y] > crds + 1) {
        ds[i][y] = crds + 1;
        pq.push({ds[i][y], {i, y}});
      } else if (ds[i][y] < crds)
        break;
    }
    for (ll i = y + 1, cn = 1; i <= w && cn <= k; i++, cn++) {
      if (v[x][i] == '@')
        break;
      if (ds[x][i] > crds + 1) {
        ds[x][i] = crds + 1;
        pq.push({ds[x][i], {x, i}});
      } else if (ds[x][i] < crds)
        break;
    }
    for (ll i = y - 1, cn = 1; i >= 1 && cn <= k; i--, cn++) {
      if (v[x][i] == '@')
        break;
      if (ds[x][i] > crds + 1) {
        ds[x][i] = crds + 1;
        pq.push({ds[x][i], {x, i}});
      } else if (ds[x][i] < crds)
        break;
    }
  }
}
void solve() {

  ll a, b, c, q, j, l, mx = 0, i;
  ll hn, hm, gc, sx, sy, ex = 1, ey = -1;
  scll(h, w);
  scl(k);
  FOR(i, 0, h + 4) {
    v[i].resize(w + 4);
    ds[i].resize(w + 4);
  }
  scll(hn, hm);
  scll(sx, sy);
  char cha;
  scanf("%c", &cha);
  FOR(i, 1, h + 1) {
    FOR(j, 1, w + 1) {
      scanf("%c", &cha);
      v[i][j] = cha;
      ds[i][j] = nax * nax;
    }
    scanf("%c", &cha);
  }
  ds[hn][hm] = 0;
  bfs(hn, hm, sx, sy);
  if (ds[sx][sy] == nax * nax)
    printf("-1\\n");
  else
    printf("%I64d\\n", ds[sx][sy]);
}
int main() {
  // IOS;
  double start_time = clock();
  ll t = 1, mex = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  double end_time = clock();
  // fprintf(stderr, "Time = %lf\\n", (end_time - start_time) / CLOCKS_PER_SEC);
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define xx first
#define yy second
#define sci(n) scanf("%d", &n)
#define scii(n, m) scanf("%d%d", &n, &m)
#define sciii(n, m, w) scanf("%d%d%d", &n, &m, &w)
#define scl(n) scanf("%lld", &n)
#define scll(n, m) scanf("%lld%lld", &n, &m)
#define sclll(n, m, w) scanf("%lld%lld%lld", &n, &m, &w)
#define pf(a) printf("%d\\n", a)
#define CASE(a) printf("Case %d:\\n", a)
#define dbg(i) printf("yo %lld\\n", i)
#define endl '\\n'
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout);
#define pi acos(-1)
#define mem(a, b) memset(a, b, sizeof(a))
#define SQR(a) (a) * (a)
#define all(v) v.begin(), v.end()
#define pb push_back
#define pri priority_queue<int>
#define rev_pri priority_queue<int, vector<int>, greater<int>>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define mod 1000000007
#define inf INT_MAX
#define eps 1e-9
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define snuke(c, itr)                                                          \\
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)

using namespace __gnu_pbds;
using namespace std;

int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
// ll ncr(ll n,ll r){
// if(n==r)return 1;
// if(r==1)return n;
// if(dp[n][r]!=-1)return dp[n][r];
// return dp[n][r]=ncr(n-1,r)+ncr(n-1,r-1);
//}

bool checkbit(int mask, int bit) { return mask & (1 << bit); }
int setbit(int mask, int bit) { return mask | (1 << bit); }
int clearbit(int mask, int bit) { return mask & ~(1 << bit); }
int togglebit(int mask, int bit) { return mask ^ (1 << bit); }
int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(ll t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }

// #define CAPITAL
void yes(bool ex = true) {
#ifdef CAPITAL
  cout << "YES"
       << "\\n";
#else
  cout << "Yes"
       << "\\n";
#endif
  if (ex)
    exit(0);
}
void no(bool ex = true) {
#ifdef CAPITAL
  cout << "NO"
       << "\\n";
#else
  cout << "No"
       << "\\n";
#endif
  if (ex)
    exit(0);
}
void possible(bool ex = true) {
#ifdef CAPITAL
  cout << "POSSIBLE"
       << "\\n";
#else
  cout << "Possible"
       << "\\n";
#endif
  if (ex)
    exit(0);
}
void impossible(bool ex = true) {
#ifdef CAPITAL
  cout << "IMPOSSIBLE"
       << "\\n";
#else
  cout << "Impossible"
       << "\\n";
#endif
  if (ex)
    exit(0);
}

template <typename T> T BigMod(T b, T p, T m) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {
    T s = BigMod(b, p / 2, m);
    return ((s % m) * (s % m)) % m;
  }
  return ((b % m) * (BigMod(b, p - 1, m) % m)) % m;
}
template <typename T> T ModInv(T b, T m) { return BigMod(b, m - 2, m); }
template <typename T> T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9')
      break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9')
      break;
  }
  return (ng ? -n : n);
}
template <typename T> T POW(T B, T P) {
  if (P == 0)
    return 1;
  if (P & 1)
    return B * POW(B, P - 1);
  else
    return SQR(POW(B, P / 2));
}
template <typename T> T Bigmod(T b, T p, T m) {
  if (p == 0)
    return 1;
  else if (!(p & 1))
    return SQR(Bigmod(b, p / 2, m)) % m;
  else
    return ((b % m) * Bigmod(b, p - 1, m)) % m;
}
template <typename T> T Dis(T x1, T y1, T x2, T y2) {
  return sqrt(SQR(x1 - x2) + SQR(y1 - y2));
}
template <typename T> T Angle(T x1, T y1, T x2, T y2) {
  return atan(double(y1 - y2) / double(x1 - x2));
}
template <typename T> T DIFF(T a, T b) {
  T d = a - b;
  if (d < 0)
    return -d;
  else
    return d;
}
template <typename T> T ABS(T a) {
  if (a < 0)
    return -a;
  else
    return a;
}
template <typename T> T euclide(T a, T b, T &x, T &y) {
  if (a < 0) {
    T d = euclide(-a, b, x, y);
    x = -x;
    return d;
  }
  if (b < 0) {
    T d = euclide(a, -b, x, y);
    y = -y;
    return d;
  }
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    T d = euclide(b, a % b, x, y);
    T t = x;
    x = y;
    y = t - (a / b) * y;
    return d;
  }
}
template <typename T> void ia(T a[], int n) {
  for (int i = 0; i < n; i++)
    cin >> a[i];
}
template <typename T> void pa(T a[], int n) {
  for (int i = 0; i < n - 1; i++)
    cout << a[i] << " ";
  cout << a[n - 1] << endl;
}
template <typename T> ll isLeft(T a, T b, T c) {
  return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
template <typename T> T gcd(T a, T b) {
  if (a < 0)
    return gcd(-a, b);
  if (b < 0)
    return gcd(a, -b);
  return (b == 0) ? a : gcd(b, a % b);
}
template <typename T> T lcm(T a, T b) {
  if (a < 0)
    return lcm(-a, b);
  if (b < 0)
    return lcm(a, -b);
  return a * (b / gcd(a, b));
}

template <class t> int lwb(const vector<t> &v, const t &a) {
  return lower_bound(all(v), a) - v.bg;
}

template <typename A, typename B> class Dijkstra {
public:
  vector<vector<pair<int, A>>> vertexs;
  B Cost_Function;
  Dijkstra(int n, B cost) : Cost_Function(cost) {
    vertexs = vector<vector<pair<int, A>>>(n, vector<pair<int, A>>{});
  }
  ~Dijkstra() { vertexs.clear(); }
  void add_edge(int a, int b, A c) { vertexs[a].push_back(mp(b, c)); }
  vector<ll> build_result(int StartPoint) {
    vector<ll> dist(vertexs.size(), 2e18);
    dist[StartPoint] = 0;
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
        next;
    next.push(make_pair(0, StartPoint));
    while (next.empty() == false) {
      pair<ll, int> now = next.top();
      next.pop();
      if (dist[now.second] != now.first)
        continue;
      for (auto x : vertexs[now.second]) {
        ll now_cost = now.first + Cost_Function(x.second);
        if (dist[x.first] > now_cost) {
          dist[x.first] = now_cost;
          next.push(mp(now_cost, x.first));
        }
      }
    }
    return dist;
  }
};

#define mxn 100005

// bool vis[1LL << 19];
const ll nax = 1000050;
ll n, w, m;
ll ar[nax];
ll h, k;
vector<char> v[nax];
vector<ll> ds[nax];
pair<ll, pll> pr;
void bfs(ll hn, ll hm, ll sx, ll sy) {
  queue<pair<ll, pair<ll, ll>>> pq;
  pq.push({0, {hn, hm}});
  ll crds, x, y, cn;
  while (!pq.empty()) {
    pr = pq.front();
    pq.pop();
    crds = pr.xx;
    x = pr.yy.xx;
    y = pr.yy.yy;
    for (ll i = x + 1, cn = 1; i <= h && cn <= k; i++, cn++) {
      if (v[i][y] == '@')
        break;
      if (ds[i][y] > crds + 1) {
        ds[i][y] = crds + 1;
        pq.push({ds[i][y], {i, y}});
      }
    }
    for (ll i = x - 1, cn = 1; i >= 1 && cn <= k; i--, cn++) {
      if (v[i][y] == '@')
        break;
      if (ds[i][y] > crds + 1) {
        ds[i][y] = crds + 1;
        pq.push({ds[i][y], {i, y}});
      }
    }
    for (ll i = y + 1, cn = 1; i <= w && cn <= k; i++, cn++) {
      if (v[x][i] == '@')
        break;
      if (ds[x][i] > crds + 1) {
        ds[x][i] = crds + 1;
        pq.push({ds[x][i], {x, i}});
      }
    }
    for (ll i = y - 1, cn = 1; i >= 1 && cn <= k; i--, cn++) {
      if (v[x][i] == '@')
        break;
      if (ds[x][i] > crds + 1) {
        ds[x][i] = crds + 1;
        pq.push({ds[x][i], {x, i}});
      }
    }
  }
}
void solve() {

  ll a, b, c, q, j, l, mx = 0, i;
  ll hn, hm, gc, sx, sy, ex = 1, ey = -1;
  scll(h, w);
  scl(k);
  FOR(i, 0, h + 4) {
    v[i].resize(w + 4);
    ds[i].resize(w + 4);
  }
  scll(hn, hm);
  scll(sx, sy);
  char cha;
  scanf("%c", &cha);
  FOR(i, 1, h + 1) {
    FOR(j, 1, w + 1) {
      scanf("%c", &cha);
      v[i][j] = cha;
      ds[i][j] = nax * nax;
    }
    scanf("%c", &cha);
  }
  ds[hn][hm] = 0;
  bfs(hn, hm, sx, sy);
  if (ds[sx][sy] == nax * nax)
    printf("-1\\n");
  else
    printf("%I64d\\n", ds[sx][sy]);
}
int main() {
  // IOS;
  double start_time = clock();
  ll t = 1, mex = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  double end_time = clock();
  // fprintf(stderr, "Time = %lf\\n", (end_time - start_time) / CLOCKS_PER_SEC);
  return 0;
}