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

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

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define uniqueV(x)                                                             \\
  sort(x.begin(), x.end());                                                    \\
  x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int(i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int(i) = (m); (i) < (n); ++(i))
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y)                                                             \\
  cerr << "(" << #x << ", " << #y << ") = "                                    \\
       << "(" << x << ", " << y << ")" << endl;
#define dbg3(x, y, z)                                                          \\
  cerr << "(" << #x << ", " << #y << ", " << #z << ") = "                      \\
       << "(" << x << ", " << y << ", " << z << ")" << endl;
#define dbgB(value, size) cerr << #value << ": " << bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const double PI = 3.14159265358979323846;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> void printA(vector<T> &printArray, char between = ' ') {
  int paSize = printArray.size();
  for (int i = 0; i < paSize; i++) {
    cerr << printArray[i] << between;
  }
  if (between != '\\n') {
    cerr << endl;
  }
}

// ------------------------------------------------------------------------------------------

ll X(ll d) { return d * (d + 1) / 2; }

ll get_value(ll len, ll l) {
  ll r = l + len;
  return X(r) - X(l);
}

int main() {
  ll B;
  while (cin >> B, B) {
    for (int len = 50000; len >= 0; len--) {
      if (X(len) > B)
        continue;

      ll ub = B + 10, lb = -1;
      while (ub - lb > 1) {
        ll mid = (ub + lb) / 2;

        if (get_value(len, mid) >= B)
          ub = mid;
        else
          lb = mid;
      }
      if (get_value(len, ub) == B) {
        printf("%lld %d\\n", ub + 1, len);
        break;
      }
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define uniqueV(x)                                                             \\
  sort(x.begin(), x.end());                                                    \\
  x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int(i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int(i) = (m); (i) < (n); ++(i))
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y)                                                             \\
  cerr << "(" << #x << ", " << #y << ") = "                                    \\
       << "(" << x << ", " << y << ")" << endl;
#define dbg3(x, y, z)                                                          \\
  cerr << "(" << #x << ", " << #y << ", " << #z << ") = "                      \\
       << "(" << x << ", " << y << ", " << z << ")" << endl;
#define dbgB(value, size) cerr << #value << ": " << bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const double PI = 3.14159265358979323846;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> void printA(vector<T> &printArray, char between = ' ') {
  int paSize = printArray.size();
  for (int i = 0; i < paSize; i++) {
    cerr << printArray[i] << between;
  }
  if (between != '\\n') {
    cerr << endl;
  }
}

// ------------------------------------------------------------------------------------------

ll b;
vec<ll> A;
pair<ll, ll> ans;

// ペアを返す
ll bin(ll key, ll Max) {
  auto check = [&](ll mid) -> bool {
    // 判定処理を書く
    return A[mid] >= key;
  };

  ll ok = Max; // 解が存在する値
  ll ng = -1;  // 解が存在しない値

  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    if (check(mid)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }

  return ok;
}

void solve() {
  for (ll i = 0; i < A.size(); i++) {
    ll key = A[i] - b;
    if (key < 0)
      continue;
    ll j = bin(key, i);

    if (A[i] - A[j] == b && i - j > ans.second) {
      ans.second = i - j;
      ans.first = j + 1;
    }
  }
  printf("%lld %lld\\n", ans.first, ans.second);
}

int main() {
  rep(i, 1e7) { A.push_back(1LL * i * (i + 1) / 2); }
  while (1) {
    cin >> b;
    if (b == 0)
      break;
    // ans.first = b;
    // ans.second = 1;
    ans.first = ans.second = 0;
    solve();
  }

  return 0;
}