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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 30;
const double EPS = 1e-9;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for (int i = 0; i < n; i++)

double cross(const xy_t &a, const xy_t &b) { return imag(conj(a) * b); }

double dot(const xy_t &a, const xy_t &b) { return real(conj(a) * b); }

xy_t projection(const line &l, const xy_t &p) {
  double t = dot(p - l.first, l.first - l.second) / norm(l.first - l.second);
  return l.first + t * (l.first - l.second);
}

bool intersectSP(const line &l, const xy_t &p) {
  return abs(l.first - p) + abs(l.second - p) - abs(l.first - l.second) < EPS;
}

double distanceSP(line s, xy_t p) {
  const xy_t r = projection(s, p);
  if (intersectSP(s, r))
    return abs(r - p);
  return min(abs(s.first - p), abs(s.second - p));
}

int next1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 3,
               2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int next2[] = {0, 1, 2, 3, 4, 5};

int main() {
  int n;
  cin >> n;
  rep(t, n) {
    int a, b;
    cin >> a >> b;
    if (a > 5 || (a <= 5 && a < b)) {
      while (next1[a] != b) {
        cout << next1[a] << " ";
        a++;
      }
      cout << b << endl;
    } else {
      while (next2[a] != b) {
        cout << next2[a] << " ";
        a--;
      }
      cout << b << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 30;
const double EPS = 1e-9;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for (int i = 0; i < n; i++)

double cross(const xy_t &a, const xy_t &b) { return imag(conj(a) * b); }

double dot(const xy_t &a, const xy_t &b) { return real(conj(a) * b); }

xy_t projection(const line &l, const xy_t &p) {
  double t = dot(p - l.first, l.first - l.second) / norm(l.first - l.second);
  return l.first + t * (l.first - l.second);
}

bool intersectSP(const line &l, const xy_t &p) {
  return abs(l.first - p) + abs(l.second - p) - abs(l.first - l.second) < EPS;
}

double distanceSP(line s, xy_t p) {
  const xy_t r = projection(s, p);
  if (intersectSP(s, r))
    return abs(r - p);
  return min(abs(s.first - p), abs(s.second - p));
}

int next1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2, 1, 0};
int next2[] = {1, 0, 1, 2, 3, 4, 5};

int main() {
  int n;
  cin >> n;
  rep(t, n) {
    int a, b;
    cin >> a >> b;
    if (a > 5 || a <= 5 && a < b) {
      while (a != b) {
        cout << a << " ";
        a = next1[a];
      }
      cout << b << endl;
    } else {
      while (a != b) {
        cout << a << " ";
        a = next2[a];
      }
      cout << b << endl;
    }
  }
  return 0;
}