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

// #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <typeinfo>
using namespace std;
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long ll;
#define REP(i, a, b) for (ll i = a; i < (ll)b; i++)
#define PER(i, a, b) for (ll i = b - 1; i >= (ll)a; i--)
#define all(v) (v).begin(), (v).end()
#define fill(v, n) fill(v.begin(), v.end(), n)
#define deb(sum) cout << #sum << "=" << sum << "\\n"
#define lpi(v, x)                                                              \\
  (lower_bound(all(v), x) - v.begin()) // lowermost possible index
#define upi(v, x)                                                              \\
  (upper_bound(all(v), x) - v.begin()) // uppermost possible index
#define bpc(n) __builtin_popcountll(n)
// #define PI 3.1415926535897932384626
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vii;
const ll INF = 9223372036854775807;
const int mod = 1000000007;
const long double PI = acos(-1.0);
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  ll x, y;
  ll ans;
  /*
  for(int i=a; i<=b; i++){
      for(int j=c;j<=d; j++){
          ans = max(ans,i*j);
      }
  }
  */
  if (a >= 0 && b >= 0) {
    ll prod1 = b * d;
    ll prod2 = a * d;
    ans = max(prod1, prod2);
    // cout<<ans;
  } else if (a <= 0 && b >= 0) {
    ll prod1 = a * c;
    ll prod2 = a * d;
    ll prod3 = b * d;
    ans = max(prod1, max(prod2, prod3));
  } else if (a <= 0 && b <= 0) {
    ll prod1 = a * c;
    ll prod2 = b * c;
    ans = max(prod1, prod2);
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

// #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <typeinfo>
using namespace std;
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long ll;
#define REP(i, a, b) for (ll i = a; i < (ll)b; i++)
#define PER(i, a, b) for (ll i = b - 1; i >= (ll)a; i--)
#define all(v) (v).begin(), (v).end()
#define fill(v, n) fill(v.begin(), v.end(), n)
#define deb(sum) cout << #sum << "=" << sum << "\\n"
#define lpi(v, x)                                                              \\
  (lower_bound(all(v), x) - v.begin()) // lowermost possible index
#define upi(v, x)                                                              \\
  (upper_bound(all(v), x) - v.begin()) // uppermost possible index
#define bpc(n) __builtin_popcountll(n)
// #define PI 3.1415926535897932384626
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vii;
const ll INF = 9223372036854775807;
const int mod = 1000000007;
const long double PI = acos(-1.0);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) {
  ll res = (a * b) / gcd(a, b);
  return res;
}
bool isprime(ll n) {
  for (ll i = 2; i * i <= n; i++) {
    // i and itself are factors of prime
    if (n % i == 0)
      return false;
  }
  return true;
}
vector<ll> factors(ll n) {
  vector<ll> v;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      v.PB(i);
      if (i != n / i) {
        v.PB(n / i);
      }
    }
  }
  return v;
}
ll power(ll a, ll b) // O(log(b))
{
  ll ans = 1;
  b = b % mod; // if b is larger than or equal to mod
  while (b > 0) {
    if (b & 1) // b is odd
    {
      ans = (ans * a) % mod;
    }
    // b is even
    b >>= 1;                       // b = b/2
    a = (a % mod * a % mod) % mod; // a =a^2
  }
  return ans;
}
int bs_upper_bound(vector<int> &a, int n, int x) {
  int l = 0;
  int h = n; // Not n - 1
  while (l < h) {
    int mid = l + (h - l) / 2;
    // when a[mid] == x,i am incrementing l,i.e not giving lowerbound(equal)
    if (x >= a[mid]) {
      l = mid + 1;
    } else {
      h = mid;
    }
  }
  return l;
}

int bs_lower_bound(vector<int> &a, int n, int x) {
  int l = 0;
  int h = n; // Not n - 1
  while (l < h) {
    int mid = l + (h - l) / 2;
    // when a[mid]==x, l is in its position only,i.e getting its lowerbound
    if (x <= a[mid]) {
      h = mid;
    } else {
      l = mid + 1;
    }
  }
  return l;
}
ll BINARY_SEARCH(ll dp[], ll n, ll key) {
  ll s = 1;
  ll e = n;
  while (s <= e) {
    ll mid = (s + e) / 2;
    if (dp[mid] == key)
      return mid;
    else if (dp[mid] > key)
      e = mid - 1;
    else
      s = mid + 1;
  }
  return -1;
}
string CONVERT_TO_BINARY(ll s) {
  string res = "";
  while (s != 0) {
    res += (char)('0' + s % 2);
    s /= 2;
  }
  reverse(res.begin(), res.end());
  return res;
}
template <typename T> T minimum(const T &lhs, const T &rhs) {
  return lhs < rhs ? lhs : rhs;
}
template <typename T> T maximum(const T &lhs, const T &rhs) {
  return lhs < rhs ? rhs : lhs;
}
// int fact(int n){
//     if(n==0){
//         return 1;
//     }
//     return n*fact(n-1);//not tail recursive
// }
int fact(int n, int k) {
  // k must be sent as 1,initially
  if (n == 0 || n == 1) {
    return k;
  }
  return fact(n - 1, (k * n) % mod); // tail recursive
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int ans = INT_MIN;
  for (int i = a; i <= b; i++) {
    for (int j = c; j <= d; j++) {
      ans = max(ans, i * j);
    }
  }
  cout << ans;
  return 0;
}