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

////////////////////////////////////////////
///                                      ///
///        Template ver. 1.2. rapel      ///
/// Fear is Temporary, Regret is Forever ///
///          Must Try and Get AC         ///
///                                      ///
////////////////////////////////////////////
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdio.h>

#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>

#include <complex>
#include <math.h>

#include <assert.h>
#include <chrono>
#include <random>
#include <time.h>

#include <bitset>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define F first
#define S second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void desperate_optimization(int precision) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(precision);
}

set<int> st;
#define endl '\\n'

void hora(int tc) {
  int n, m;
  cin >> n >> m;
  int lhs = 1, rhs = n;
  if (n % 2 == 0)
    st.insert(n / 2);
  for (int i = 1; i <= m; i++) {
    while ((st.find(rhs - lhs) != st.end() ||
            st.find(lhs + n - rhs) != st.end()) &&
           lhs < rhs)
      rhs--;
    if (lhs == rhs)
      while (1)
        ;
    cout << lhs << " " << rhs << endl;
    st.insert(rhs - lhs);
    st.insert(lhs + n - rhs);
    lhs++, rhs--;
  }
}

int main() {
  desperate_optimization(10);
  int ntc = 1;
  // cin>>ntc;
  for (int tc = 1; tc <= ntc; tc++)
    hora(tc);
  return 0;
}


Error: TLE

Fixed submission:

////////////////////////////////////////////
///                                      ///
///        Template ver. 1.2. rapel      ///
/// Fear is Temporary, Regret is Forever ///
///          Must Try and Get AC         ///
///                                      ///
////////////////////////////////////////////
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdio.h>

#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>

#include <complex>
#include <math.h>

#include <assert.h>
#include <chrono>
#include <random>
#include <time.h>

#include <bitset>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define F first
#define S second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void desperate_optimization(int precision) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(precision);
}

set<int> st;
#define endl '\\n'

void hora(int tc) {
  int n, m;
  cin >> n >> m;
  int cur = 0;
  for (int i = m; i >= 1; i--) {
    while (1) {
      if (st.find(cur) == st.end() && cur != (cur + i) % n &&
          st.find((cur + i) % n) == st.end()) {
        cout << cur + 1 << " " << (cur + i) % n + 1 << endl;
        st.insert(cur), st.insert((cur + i) % n);
        cur = (cur + i + 1) % n;
        break;
      }
      cur = (cur + 1) % n;
    }
  }
}

int main() {
  desperate_optimization(10);
  int ntc = 1;
  // cin>>ntc;
  for (int tc = 1; tc <= ntc; tc++)
    hora(tc);
  return 0;
}