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

#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<pair<int, int>> vii;
typedef stringstream ss;
const int mii = numeric_limits<int>::min();
const int mai = numeric_limits<int>::max();
const ll mil = numeric_limits<long long>::min();
const ll mal = numeric_limits<long long>::max();

#define REP(i, a, b) for (int i = a; i <= b; ++i)
#define RREP(i, a, b) for (int i = a; i >= b; --i)
#define PB(x) push_back(x)
#define MP(x, y) make_pair(x, y)

int main(int argc, char **argv) {
  int n, x, y;
  cin >> n >> x >> y;

  vector<vector<int>> dist(n + 1, vector<int>(n + 1, mai));

  /*REP(i, 1, n) {
      queue<int> next;
      next.push(i);
      set<int> visited;
      visited.insert(i);
      int currDist = 0;

      while(!next.empty()) {
          int lvlSize = next.size();

          while(lvlSize != 0) {
              int node = next.front();
              if(node != i) {
                  dist[i][node] = currDist;
                  dist[node][i] = currDist;
              }
              next.pop();
              if(node < n && visited.count(node + 1) == 0) {
                  next.push(node + 1);
                  visited.insert(node + 1);
              }
              if(node > 1 && visited.count(node - 1) == 0) {
                  next.push(node - 1);
                  visited.insert(node - 1);
              }
              if(node == x && visited.count(y) == 0) {
                  next.push(y);
                  visited.insert(y);
              }
              if(node == y && visited.count(x) == 0) {
                  next.push(x);
                  visited.insert(x);
              }
              --lvlSize;
          }

          ++currDist;
      }
  }*/

  vector<vector<int>> dists(n + 1, vector<int>(n + 1, mai));

  REP(i, 1, n) {
    REP(j, 1, n) {
      int distIJ = min(abs(i - j), min(abs(x - i) + 1 + abs(y - j),
                                       abs(y - i) + 1 + abs(x - j)));
      dists[i][j] = distIJ;
      dists[j][i] = distIJ;
    }
  }

  map<int, set<pair<int, int>>> dist1;

  REP(i, 1, n) {
    REP(j, 1, n) {
      if (i == j)
        continue;

      dist1[dists[i][j]].insert(MP(min(i, j), max(i, j)));
    }
  }

  REP(i, 1, n - 1) {
    cout << (dist1[i].size() > 0 ? dist1[i].size() : 0) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<pair<int, int>> vii;
typedef stringstream ss;
const int mii = numeric_limits<int>::min();
const int mai = numeric_limits<int>::max();
const ll mil = numeric_limits<long long>::min();
const ll mal = numeric_limits<long long>::max();

#define REP(i, a, b) for (int i = a; i <= b; ++i)
#define RREP(i, a, b) for (int i = a; i >= b; --i)
#define PB(x) push_back(x)
#define MP(x, y) make_pair(x, y)

int main(int argc, char **argv) {
  int n, x, y;
  cin >> n >> x >> y;

  vector<vector<int>> dist(n + 1, vector<int>(n + 1, mai));

  REP(i, 1, n) {
    queue<int> next;
    next.push(i);
    set<int> visited;
    visited.insert(i);
    int currDist = 0;

    while (!next.empty()) {
      int lvlSize = next.size();

      while (lvlSize != 0) {
        int node = next.front();
        if (node != i) {
          dist[i][node] = currDist;
          dist[node][i] = currDist;
        }
        next.pop();
        if (node < n && visited.count(node + 1) == 0) {
          next.push(node + 1);
          visited.insert(node + 1);
        }
        if (node > 1 && visited.count(node - 1) == 0) {
          next.push(node - 1);
          visited.insert(node - 1);
        }
        if (node == x && visited.count(y) == 0) {
          next.push(y);
          visited.insert(y);
        }
        if (node == y && visited.count(x) == 0) {
          next.push(x);
          visited.insert(x);
        }
        --lvlSize;
      }

      ++currDist;
    }
  }

  map<int, set<pair<int, int>>> dist1;

  REP(i, 1, n) {
    REP(j, 1, n) {
      if (i == j)
        continue;

      dist1[dist[i][j]].insert(MP(min(i, j), max(i, j)));
    }
  }

  REP(i, 1, n - 1) {
    cout << (dist1[i].size() > 0 ? dist1[i].size() : 0) << endl;
  }

  return 0;
}