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

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int main() {
  int n;
  while (cin >> n) {
    long long int count = 0;
    if (n <= 1000) {
      for (int i = 0; i <= n; i++)
        count += ((i + 2) * (i + 1) / 2);
      cout << count << endl;
      // printf("%lld\\n",count);
    } else {
      long long int count = 0;
      // vector<P> mm1;
      // vector<P> mm2;
      /// vector<P>::iterator iter;
      int arr[2000 + 5] = {0};
      int arr2[2000 + 5] = {0};
      for (int a = 0; a <= 1000; a++) {
        if ((n - a) > 3000 || (n < a))
          continue;
        for (int b = 0; b <= 1000; b++) {
          if ((n - a - b) > 2000 || (n < (a + b)))
            continue;
          else
            arr[a + b]++;
        }
      }
      for (int c = 0; c <= 1000; c++) {
        if ((n - c) > 3000)
          continue;
        for (int d = 0; d <= 1000; d++) {
          if ((n - c - d) < 0 || (n - c - d) > 2000)
            continue;
          else
            arr2[n - c - d]++;
        }
      }
      for (int i = 0; i <= 2000; i++) {
        if ((arr[i]) && (arr2[i])) {
          count += arr[i] * arr2[i];
        }
      }
      cout << count << endl;
      // printf("%lld,",count);
    }
  }
  // while(1);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int main() {
  int n;
  // while(cin>>n)
  for (int n = 1001; n <= 3000; n++) {
    long long int count = 0;
    if (n <= 1000) {
      for (int i = 0; i <= n; i++)
        count += ((i + 2) * (i + 1) / 2);
      printf("%lld\\n", count);
    } else {
      long long int count = 0;
      // vector<P> mm1;
      // vector<P> mm2;
      /// vector<P>::iterator iter;
      int arr[2000 + 5] = {0};
      int arr2[2000 + 5] = {0};
      for (int a = 0; a <= 1000; a++) {
        if ((n - a) > 3000 || (n < a))
          continue;
        for (int b = 0; b <= 1000; b++) {
          if ((n - a - b) > 2000 || (n < a + b))
            continue;
          else
            arr[a + b]++;
        }
      }
      for (int c = 0; c <= 1000; c++) {
        if ((n - c) > 3000)
          continue;
        for (int d = 0; d <= 1000; d++) {
          if ((n - c - d) < 0 || (n - c - d) > 2000)
            continue;
          else
            arr2[n - c - d]++;
        }
      }
      for (int i = 0; i <= 2000; i++) {
        if ((arr[i]) && (arr2[i])) {
          count += arr[i] * arr2[i];
        }
      }
      printf("%lld,", count);
    }
  }
  // while(1);
  return 0;
}