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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

namespace utils {
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
template <class T, class Compare>
using p_queue = priority_queue<T, vector<T>, Compare>;
template <class T> using min_queue = p_queue<T, greater<T>>;
template <class T> using max_queue = p_queue<T, less<T>>;

template <class T> inline bool UPMIN(T &X, const T &A) {
  if (X > A) {
    X = A;
    return true;
  }
  return false;
}
template <class T> inline bool UPMAX(T &X, const T &A) {
  if (X < A) {
    X = A;
    return true;
  }
  return false;
}

template <class T> vector<T> VEC(int n, T t) { return vector<T>(n, t); }
template <class... Ts> auto VEC(int n, Ts... ts) {
  return vector<decltype(VEC(ts...))>(n, VEC(ts...));
}

template <class S, class T> istream &operator>>(istream &is, tuple<S, T> &t) {
  return is >> get<0>(t) >> get<1>(t);
}

template <class S, class T, class U>
istream &operator>>(istream &is, tuple<S, T, U> &t) {
  return is >> get<0>(t) >> get<1>(t) >> get<2>(t);
}

template <class S, class T>
ostream &operator<<(ostream &os, const tuple<S, T> &t) {
  return os << get<0>(t) << ' ' << get<1>(t);
}

template <class S, class T, class U>
ostream &operator<<(ostream &os, const tuple<S, T, U> &t) {
  return os << get<0>(t) << ' ' << get<1>(t) << ' ' << get<2>(t);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &&x : v) {
    is >> x;
  }
  return is;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  auto p = v.begin();
  assert(p != v.end());
  os << *p++;
  while (p != v.end()) {
    os << ' ' << *p++;
  }
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
  auto p = v.begin();
  assert(p != v.end());
  os << *p++;
  while (p != v.end()) {
    os << '\\n' << *p++;
  }
  return os;
}

template <class... Ts>
ostream &operator<<(ostream &os, const vector<tuple<Ts...>> &v) {
  auto p = v.begin();
  assert(p != v.end());
  os << *p++;
  while (p != v.end()) {
    os << '\\n' << *p++;
  }
  return os;
}

constexpr long long INFLL = 1'000'000'000'000'000'000ll;
constexpr int INF = 1'000'000'000;
constexpr double PI = 3.14'159'265'358'973;
constexpr double EPS = 1e-10;
} // namespace utils
using namespace utils;

template <class Monoid> class SegmentTree {
  using Func = std::function<Monoid(Monoid, Monoid)>;
  int N;

  std::vector<Monoid> segment;

  const Func op;
  const Monoid id;

  bool dirty_flag;

public:
  // id : op = max なら -INT_MAX
  SegmentTree(int n, const Func biop, const Monoid &M1)
      : op(biop), id(M1), dirty_flag(false) {
    N = 1;
    while (N < n)
      N *= 2;
    segment.assign(2 * N, id);
  }

  void set(int k, const Monoid &a) {
    dirty_flag = true;
    segment[k + N] = a;
  }

  void build() {
    for (int k = N - 1; k > 0; --k) {
      segment[k] = op(segment[2 * k], segment[2 * k + 1]);
    } // end k
    dirty_flag = false;
  }

  void update(int k, const Monoid &a) {
    k += N;
    segment[k] = a;
    while (k /= 2) {
      segment[k] = op(segment[k * 2], segment[k * 2 + 1]);
    }
  }

  // -> op [a,b)
  Monoid query(unsigned a, unsigned b) {
    if (dirty_flag)
      build();
    dirty_flag = false;
    Monoid L = id, R = id;
    for (a += N, b += N; a < b; a >>= 1u, b >>= 1u) {
      if (a & 1u)
        L = op(L, segment[a++]);
      if (b & 1u)
        R = op(segment[--b], R);
    } // end i
    return op(L, R);
  }

  Monoid operator[](const int &k) const { return segment[k + N]; }
};

class solver {
  istream &is;
  ostream &os;

public:
  solver(istream &I, ostream &O) : is(I), os(O) {}

  int H, W, K;
  int x1, y1, x2, y2;
  vector<vector<char>> ike;

  vector<SegmentTree<bool>> hvs, wvs;

  bool input() {
    is >> H >> W >> K;
    is >> x1 >> y1 >> x2 >> y2;
    hvs.reserve(H);
    for (int i = 0; i < H; ++i) {
      hvs.emplace_back(
          W, [](bool a, bool b) { return a && b; }, true);
    } // end i
    wvs.reserve(W);
    for (int i = 0; i < W; ++i) {
      wvs.emplace_back(
          H, [](bool a, bool b) { return a && b; }, true);
    } // end i
    x1--;
    y1--;
    x2--;
    y2--;
    ike = VEC(H, W, '\\0');
    is >> ike;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        hvs[i].set(j, ike[i][j] == '.');
        wvs[j].set(i, ike[i][j] == '.');
      } // end j
    }   // end i
    for (int i = 0; i < H; ++i) {
      hvs[i].build();
    } // end i
    for (int i = 0; i < W; ++i) {
      wvs[i].build();
    } // end i
    return !!is;
  }

  bool inside(int x, int y) const {
    return 0 <= x and x < H and 0 <= y and y < W;
  }

  bool lotus(int x, int y) const { return ike[x][y] == '@'; }

  int maxgo(int i, int x, int y) {
    int ok = 0;
    int ng = K + 1;
    if (i % 2) {
      while (abs(ok - ng) > 1) {
        int mid = (ok + ng) / 2;
        int nx = x + dx[i] * mid;
        if (inside(nx, y) and wvs[y].query(min(nx, x), max(nx, x) + 1)) {
          ok = mid;
        } else
          ng = mid;
      }
    } else {
      while (abs(ok - ng) > 1) {
        int mid = (ok + ng) / 2;
        int ny = y + dy[i] * mid;
        if (inside(x, ny) and hvs[x].query(min(ny, y), max(ny, y) + 1)) {
          ok = mid;
        } else
          ng = mid;
      }
    }
    return ok;
  }

  vector<int> dx = {0, 1, 0, -1};
  vector<int> dy = {1, 0, -1, 0};

  void run();
};

int main(int argc, char *argv[]) {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << setprecision(16) << scientific;
#ifdef XELMH
  string test_cases = "test_F.txt";
  cerr << test_cases << " -->" << endl;
  auto fs = fstream(test_cases, fstream::in);
  int loop = 0;
  while (fs) {
    loop++;
    cout << '#' << loop << "#------\\n";
    solver(fs, cout).run();
  }
  if (loop <= 1) {
    cout << "===" << endl;
    while (cin)
      solver(cin, cout).run();
  }
#else
  solver(cin, cout).run();
#endif
  return 0;
}

void solver::run() {
  if (!input())
    return;
  auto distance = VEC(4, H, W, INF);
  // (distance, x, y, direction)
  queue<tuple<int, int, int>> pq;
  for (int i = 0; i < 4; ++i) {
    distance[i][x1][y1] = 0;
    int mx = maxgo(i, x1, y1);
    for (int k = mx; k > 0; --k) {
      int nx, ny;
      nx = x1 + dx[i] * k;
      ny = y1 + dy[i] * k;

      distance[i][nx][ny] = 1;
      pq.emplace(nx, ny, i);
    } // end k
  }   // end i

  while (!pq.empty()) {
    int x, y, direction;
    tie(x, y, direction) = pq.front();
    pq.pop();
    int need = distance[direction][x][y];
    for (int i = 0; i < 4; ++i) {
      int mx = maxgo(i, x, y);
      for (int k = mx; k > 0; --k) {
        int nx, ny;
        nx = x + dx[i] * k;
        ny = y + dy[i] * k;

        if (UPMIN(distance[i][nx][ny], need + 1)) {
          pq.emplace(nx, ny, i);
        } else
          break;
      } // end k
    }   // end i
  }
  int ans = INF;
  for (int i = 0; i < 4; ++i) {
    UPMIN(ans, distance[i][x2][y2]);
  } // end i
    //    cerr << distance << endl;
  if (ans == INF) {
    cout << -1 << endl;
  } else
    cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

namespace utils {
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
template <class T, class Compare>
using p_queue = priority_queue<T, vector<T>, Compare>;
template <class T> using min_queue = p_queue<T, greater<T>>;
template <class T> using max_queue = p_queue<T, less<T>>;

template <class T> inline bool UPMIN(T &X, const T &A) {
  if (X > A) {
    X = A;
    return true;
  }
  return false;
}
template <class T> inline bool UPMAX(T &X, const T &A) {
  if (X < A) {
    X = A;
    return true;
  }
  return false;
}

template <class T> vector<T> VEC(int n, T t) { return vector<T>(n, t); }
template <class... Ts> auto VEC(int n, Ts... ts) {
  return vector<decltype(VEC(ts...))>(n, VEC(ts...));
}

template <class S, class T> istream &operator>>(istream &is, tuple<S, T> &t) {
  return is >> get<0>(t) >> get<1>(t);
}

template <class S, class T, class U>
istream &operator>>(istream &is, tuple<S, T, U> &t) {
  return is >> get<0>(t) >> get<1>(t) >> get<2>(t);
}

template <class S, class T>
ostream &operator<<(ostream &os, const tuple<S, T> &t) {
  return os << get<0>(t) << ' ' << get<1>(t);
}

template <class S, class T, class U>
ostream &operator<<(ostream &os, const tuple<S, T, U> &t) {
  return os << get<0>(t) << ' ' << get<1>(t) << ' ' << get<2>(t);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &&x : v) {
    is >> x;
  }
  return is;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  auto p = v.begin();
  assert(p != v.end());
  os << *p++;
  while (p != v.end()) {
    os << ' ' << *p++;
  }
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
  auto p = v.begin();
  assert(p != v.end());
  os << *p++;
  while (p != v.end()) {
    os << '\\n' << *p++;
  }
  return os;
}

template <class... Ts>
ostream &operator<<(ostream &os, const vector<tuple<Ts...>> &v) {
  auto p = v.begin();
  assert(p != v.end());
  os << *p++;
  while (p != v.end()) {
    os << '\\n' << *p++;
  }
  return os;
}

constexpr long long INFLL = 1'000'000'000'000'000'000ll;
constexpr int INF = 1'000'000'000;
constexpr double PI = 3.14'159'265'358'973;
constexpr double EPS = 1e-10;
} // namespace utils
using namespace utils;

class solver {
  istream &is;
  ostream &os;

public:
  solver(istream &I, ostream &O) : is(I), os(O) {}

  int H, W, K;
  int x1, y1, x2, y2;
  vector<vector<char>> ike;
  bool input() {
    is >> H >> W >> K;
    is >> x1 >> y1 >> x2 >> y2;
    x1--;
    y1--;
    x2--;
    y2--;
    ike = VEC(H, W, '\\0');
    is >> ike;
    return !!is;
  }

  bool inside(int x, int y) const {
    return 0 <= x and x < H and 0 <= y and y < W;
  }

  bool lotus(int x, int y) const { return ike[x][y] == '@'; }
  vector<int> dx = {0, 1, 0, -1};
  vector<int> dy = {1, 0, -1, 0};

  void run();
};

int main(int argc, char *argv[]) {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << setprecision(16) << scientific;
#ifdef XELMH
  string test_cases = "test_F.txt";
  cerr << test_cases << " -->" << endl;
  auto fs = fstream(test_cases, fstream::in);
  int loop = 0;
  while (fs) {
    loop++;
    cout << '#' << loop << "#------\\n";
    solver(fs, cout).run();
  }
  if (loop <= 1) {
    cout << "===" << endl;
    while (cin)
      solver(cin, cout).run();
  }
#else
  solver(cin, cout).run();
#endif
  return 0;
}

void solver::run() {
  if (!input())
    return;
  auto distance = VEC(H, W, 4, INF);
  auto remain = VEC(H, W, 4, -1);
  // (distance, x, y, direction)
  queue<tuple<int, int, int>> pq;
  for (int i = 0; i < 4; ++i) {
    distance[x1][y1][i] = 0;
    for (int k = 1; k <= K; ++k) {
      int nx, ny;
      nx = x1 + dx[i] * k;
      ny = y1 + dy[i] * k;

      if (!inside(nx, ny))
        break;
      if (lotus(nx, ny))
        break;
      distance[nx][ny][i] = 1;
      remain[nx][ny][i] = K - k;
      pq.emplace(nx, ny, i);
    } // end k
  }   // end i

  while (!pq.empty()) {
    int x, y, direction;
    tie(x, y, direction) = pq.front();
    pq.pop();
    int need = distance[x][y][direction];
    for (int i = 0; i < 4; ++i) {
      for (int k = 1; k <= K; ++k) {
        int nx, ny;
        nx = x + dx[i] * k;
        ny = y + dy[i] * k;

        if (!inside(nx, ny))
          break;
        if (lotus(nx, ny))
          break;
        if (UPMIN(distance[nx][ny][i], need + 1)) {
          pq.emplace(nx, ny, i);
        } else if (remain[nx][ny][i] < K - k) {
          remain[nx][ny][i] = K - k;
        } else
          break;
      } // end k
    }   // end i
  }
  ll ans = *min_element(ALL(distance[x2][y2]));
  if (ans == INF) {
    cout << -1 << endl;
  } else
    cout << ans << endl;
}