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

#include <bits/stdc++.h>
using namespace std;

#define loop(n) for (int i = 0; i < (int)(n); ++i)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(vec) (vec).begin(), (vec).end()
#define Sort(vec) sort(all(vec))
#define Rev(vec) reverse(all(vec))

using Graph = vector<vector<int>>;

namespace Input {

void input() { return; }
template <typename First, typename... Rest>
void input(First &first, Rest &...rest) {
  std::cin >> first;
  input(rest...);
}

void inputVec() { return; }
template <typename First, typename... Rest>
void inputVec(vector<First> &first, Rest &...rest) {
  int N = first.size();
  for (int i = 0; i < N; i++) {
    cin >> first[i];
  }
  inputVec(rest...);
}

template <typename First, typename Second>
void inputTwoVec(vector<First> &first, vector<Second> &second) {
  int N = first.size();
  for (int i = 0; i < N; i++) {
    cin >> first[i] >> second[i];
  }
}

void inputGraph(Graph &g, int path_num, bool isdirected) {
  for (int i = 0; i < path_num; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (isdirected) {
      g[a].push_back(b);
    } else {
      g[a].push_back(b);
      g[b].push_back(a);
    }
  }
}
} // namespace Input

void calculate() {
  int N, M;
  cin >> N >> M;
  if (N % 2 == 1) {
    int count = M;
    for (int r = 1, l = N; r < l; r++, l--) {
      cout << r << " " << l << '\\n';
      count--;
      if (count == 0) {
        break;
      }
    }
  } else {
    int count = M;
    bool one_time = true;
    for (int l = 1, r = N; l < r; l++, r--) {
      if (one_time and r - l <= N / 2) {
        r--;
        one_time = false;
      }
      cout << l << " " << r << '\\n';
      count--;
      if (count == 0) {
        break;
      }
    }
  }
}

int main(int, char **) { calculate(); }


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define loop(n) for (int i = 0; i < (int)(n); ++i)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(vec) (vec).begin(), (vec).end()
#define Sort(vec) sort(all(vec))
#define Rev(vec) reverse(all(vec))

using Graph = vector<vector<int>>;

namespace Input {

void input() { return; }
template <typename First, typename... Rest>
void input(First &first, Rest &...rest) {
  std::cin >> first;
  input(rest...);
}

void inputVec() { return; }
template <typename First, typename... Rest>
void inputVec(vector<First> &first, Rest &...rest) {
  int N = first.size();
  for (int i = 0; i < N; i++) {
    cin >> first[i];
  }
  inputVec(rest...);
}

template <typename First, typename Second>
void inputTwoVec(vector<First> &first, vector<Second> &second) {
  int N = first.size();
  for (int i = 0; i < N; i++) {
    cin >> first[i] >> second[i];
  }
}

void inputGraph(Graph &g, int path_num, bool isdirected) {
  for (int i = 0; i < path_num; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (isdirected) {
      g[a].push_back(b);
    } else {
      g[a].push_back(b);
      g[b].push_back(a);
    }
  }
}
} // namespace Input

void calculate() {
  int N, M;
  cin >> N >> M;
  if (N % 2 == 1) {
    int count = M;
    for (int l = 1, r = N; l < r; l++, r--) {
      cout << l << " " << r << '\\n';
      count--;
      if (count == 0) {
        break;
      }
    }
  } else {
    int count = M;
    set<int> s;
    for (int l = 1, r = N; l < r; l++, r--) {
    re:
      auto i = s.insert(abs(l - r));
      if (!i.second) {
        r--;
      }
      goto re;
      cout << l << " " << r << '\\n';
      count--;
      if (count == 0) {
        break;
      }
    }
  }
}

int main(int, char **) { calculate(); }