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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll>;
using vs = vector<string>;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (ll i = (a); i < (n); i++)
#define LINF ((ll)1ll << 60)
#define INF ((int)1 << 30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout << setprecision(a) << fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

int dx[] = {1, 0, -1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool a = 1;
  for (auto s : t) {
    os << (a ? "" : " ") << s;
    a = 0;
  }
  return os;
}
void OUT(bool b) { cout << (b ? "YES" : "NO") << endl; }
void Out(bool b) { cout << (b ? "Yes" : "No") << endl; }
void out(bool b) { cout << (b ? "yes" : "no") << endl; }
template <class T> void puta(T &&t) { cout << t << endl; }
template <class H, class... T> void puta(H &&h, T &&...t) {
  cout << h << ' ';
  puta(t...);
}

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int bsr(int x) { return 31 - __builtin_clz(x); }
int bsr(ll x) { return 63 - __builtin_clzll(x); }
int bsf(int x) { return __builtin_ctz(x); }
int bsf(ll x) { return __builtin_ctzll(x); }

/*他のライブラリを入れる場所*/

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  while (cin >> n, n) {
    int ans = 0;
    for (int i = 1; i * i <= 32768; i++) {
      if (i * i == n)
        ans++;
      for (int j = i; i * i + j * j <= 32768; j++) {
        if (i * i + j * j == n)
          ans++;
        for (int k = j; i * i + j * j + k * k <= 32768; k++) {
          if (i * i + j * j + k * k == n)
            ans++;
          for (int l = k; i * i + j * j + k * k + l * l <= 32768; l++) {
            if (i * i + j * j + k * k + l * l == n)
              ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll>;
using vs = vector<string>;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (ll i = (a); i < (n); i++)
#define LINF ((ll)1ll << 60)
#define INF ((int)1 << 30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout << setprecision(a) << fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

int dx[] = {1, 0, -1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool a = 1;
  for (auto s : t) {
    os << (a ? "" : " ") << s;
    a = 0;
  }
  return os;
}
void OUT(bool b) { cout << (b ? "YES" : "NO") << endl; }
void Out(bool b) { cout << (b ? "Yes" : "No") << endl; }
void out(bool b) { cout << (b ? "yes" : "no") << endl; }
template <class T> void puta(T &&t) { cout << t << endl; }
template <class H, class... T> void puta(H &&h, T &&...t) {
  cout << h << ' ';
  puta(t...);
}

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int bsr(int x) { return 31 - __builtin_clz(x); }
int bsr(ll x) { return 63 - __builtin_clzll(x); }
int bsf(int x) { return __builtin_ctz(x); }
int bsf(ll x) { return __builtin_ctzll(x); }

/*他のライブラリを入れる場所*/

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  while (cin >> n, n) {
    int ans = 0;
    range(i, 1, 182) {
      if (i * i == n)
        ans++;
      range(j, i, 182) {
        if (i * i + j * j == n)
          ans++;
        range(k, j, 182) {
          if (i * i + j * j + k * k == n)
            ans++;
          range(l, k, 182) {
            if (i * i + j * j + k * k + l * l == n)
              ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}