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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef long long ll;
#define N 200020
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
struct Rook {
  int x, y, id;
  bool operator<(const Rook b) const { return x < b.x; }
} a[N];
int n;
vector<int> ys;
pair<ll, ll> ans[N];

inline int Abs(int x) { return x >= 0 ? x : -x; }
inline int Dis(int i, int j) {
  return Abs(a[i].x - a[j].x) + Abs(a[i].y - a[j].y);
}

int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    a[i].id = i;
    a[i].x = read(), a[i].y = read();
    ys.push_back(a[i].y);
  }
  sort(a + 1, a + n + 1);
  ys.push_back(-inf), ys.push_back(inf);
  sort(ys.begin(), ys.end());
  memset(ans, -1, sizeof(ans));
  for (int S = 1; S <= n; ++S) {
    if (~ans[a[S].id].first)
      continue;
    int by = lower_bound(ys.begin(), ys.end(), a[S].y) - ys.begin();
    int ly = by - 1, ry = by + 1, L = S, R = S;
    int las = -1;
    bool ok;
    vector<pair<int, int>> it;
    do {
      ok = false;
      for (int i : {L - 1, R + 1}) {
        if (i < 1 || i > n)
          continue;
        bool pre = ys[ly] == a[i].y, suf = ys[ry] == a[i].y;
        if (pre || suf) {
          int dir = (pre == (i == L - 1));
          if (las ^ dir) {
            if (~las)
              it.emplace_back(L, R);
            las = dir;
          }
          if (pre)
            --ly;
          else
            ++ry;
          if (i == L - 1)
            --L;
          else
            ++R;
          ok = true;
          break;
        }
      }
    } while (ok);
    if (it.empty() || it.back() != make_pair(L, R)) {
      it.emplace_back(L, R);
    }
    static ll dp[N][2];
    int m = it.size();
    dp[m - 1][0] = dp[m - 1][1] = 0;
    for (int u = m - 1; u >= 1; --u) {
      int L1 = it[u].first, R1 = it[u].second;
      int L2 = it[u - 1].first, R2 = it[u - 1].second;
      dp[u - 1][0] =
          min(dp[u][0] + ((R1 == R2) ? Dis(L1, L2) : Dis(L1, R1) + Dis(R1, L2)),
              dp[u][1] + Dis(R1, L1) + Dis(L1, L2));
      dp[u - 1][1] =
          min(dp[u][1] + ((L1 == L2) ? Dis(R1, R2) : Dis(R1, L1) + Dis(L1, R2)),
              dp[u][0] + Dis(L1, R1) + Dis(R1, R2));
    }
    int eL = it[0].first, eR = it[0].second;
    for (int i = eL; i <= eR; ++i) {
      ans[a[i].id] = make_pair(R - L, min(dp[0][0] + Dis(eL, eR) + Dis(eR, i),
                                          dp[0][1] + Dis(eR, eL) + Dis(eL, i)));
    }
  }
  for (int i = 1; i <= n; ++i) {
    printf("%lld\\n", ans[i].second - ans[i].first);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef long long ll;
#define N 200020
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
struct Rook {
  int x, y, id;
  bool operator<(const Rook b) const { return x < b.x; }
} a[N];
int n;
vector<int> ys;
pair<ll, ll> ans[N];

inline int Abs(int x) { return x >= 0 ? x : -x; }
inline int Dis(int i, int j) {
  return Abs(a[i].x - a[j].x) + Abs(a[i].y - a[j].y);
}

int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    a[i].id = i;
    a[i].x = read(), a[i].y = read();
    ys.push_back(a[i].y);
  }
  sort(a + 1, a + n + 1);
  ys.push_back(-inf), ys.push_back(inf);
  sort(ys.begin(), ys.end());
  memset(ans, ~0x3f, sizeof(ans));
  for (int S = 1; S <= n; ++S) {
    if (ans[a[S].id].first >= 0)
      continue;
    int by = lower_bound(ys.begin(), ys.end(), a[S].y) - ys.begin();
    int ly = by - 1, ry = by + 1, L = S, R = S;
    int las = -1;
    bool ok;
    vector<pair<int, int>> it;
    do {
      ok = false;
      for (int i : {L - 1, R + 1}) {
        if (i < 1 || i > n)
          continue;
        bool pre = ys[ly] == a[i].y, suf = ys[ry] == a[i].y;
        if (pre || suf) {
          int dir = (pre == (i == L - 1));
          if (las ^ dir) {
            if (~pre)
              it.emplace_back(L, R);
            las = dir;
          }
          if (pre)
            --ly;
          else
            ++ry;
          if (i == L - 1)
            --L;
          else
            ++R;
          ok = true;
          break;
        }
      }
    } while (ok);
    if (it.empty() || it.back() != make_pair(L, R)) {
      it.emplace_back(L, R);
    }
    static ll dp[N][2];
    int m = it.size();
    dp[m - 1][0] = dp[m - 1][1] = 0;
    for (int u = m - 1; u >= 1; --u) {
      int L1 = it[u].first, R1 = it[u].second;
      int L2 = it[u - 1].first, R2 = it[u - 1].second;
      dp[u - 1][0] =
          min(dp[u][0] + ((R1 == R2) ? Dis(L1, L2) : Dis(L1, R1) + Dis(R1, L2)),
              dp[u][1] + Dis(R1, L1) + Dis(L1, L2));
      dp[u - 1][1] =
          min(dp[u][1] + ((L1 == L2) ? Dis(R1, R2) : Dis(R1, L1) + Dis(L1, R2)),
              dp[u][0] + Dis(L1, R1) + Dis(R1, R2));
    }
    int eL = it[0].first, eR = it[0].second;
    for (int i = eL; i <= eR; ++i) {
      ans[a[i].id] = make_pair(R - L, min(dp[0][0] + Dis(eL, eR) + Dis(eR, i),
                                          dp[0][1] + Dis(eR, eL) + Dis(eL, i)));
    }
  }
  for (int i = 1; i <= n; ++i) {
    printf("%lld\\n", ans[i].second - ans[i].first);
  }
  return 0;
}