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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int INF = 1 << 29;
typedef long long ll;
inline int two(int n) { return 1 << n; }

inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int &n, int b) { n |= two(b); }
inline void unset_bit(int &n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) {
  int res = 0;
  while (n && ++res)
    n -= n & (-n);
  return res;
}
template <class T> void chmax(T &a, const T &b) { a = max(a, b); }
template <class T> void chmin(T &a, const T &b) { a = min(a, b); }

int binarySearch(int arr[], int l, int r, int x) {
  if (r >= l) {
    int mid = l + (r - l) / 2;

    // If the element is present at the middle
    // itself
    if (arr[mid] == x)
      return mid;

    // If element is smaller than mid, then
    // it can only be present in left subarray
    if (arr[mid] > x)
      return binarySearch(arr, l, mid - 1, x);

    // Else the element can only be present
    // in right subarray
    return binarySearch(arr, mid + 1, r, x);
  }

  // We reach here when element is not
  // present in array
  return -1;
}

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  int a[n + 1][n + 1];
  FOR(i, 0, n)
  FOR(j, 0, n)
  a[i][j] = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < i; j++)
      a[i][j] = i - j;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      int temp = abs(j - x) + 1 + abs(i - y);
      temp = min(temp, abs(i - x) + 1 + abs(j - y));
      a[i][j] = min(temp, a[i][j]);
    }
  }

  for (int i = 1; i <= n; i++)
    sort(a[i] + 1, a[i] + n + 1);

  // for(int i=1;i<=n;i++)
  //     {for(int j=1;j<=n;j++)
  //         cout<<a[i][j]<<" ";
  //     cout<<"\\n";
  //     }

  for (int p = 1; p < n; p++) {
    int ans = 0;
    for (int i = 1; i <= n; i++) {

      int index = binarySearch(a[i], 1, n, p);
      if (index != -1) {
        // cout<<"array is\\n";
        // for(int m=1;m<=n;m++)
        //     cout<<a[i][m]<<" ";
        //     cout<<"\\n";
        int upper = index, lower = index;
        while (a[i][upper] == p && upper <= n)
          upper++;
        while (a[i][lower] == p && lower >= 1)
          lower--;
        ans = ans + upper - lower + 1;
        if (a[i][upper] != p)
          ans--;
        if (a[i][lower] != p)
          ans--;
        // printf("i  %d index %d lower %d upper %d val=
        // %d\\n",i,index,lower,upper,a[i][index]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int INF = 1 << 29;
typedef long long ll;
inline int two(int n) { return 1 << n; }

inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int &n, int b) { n |= two(b); }
inline void unset_bit(int &n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) {
  int res = 0;
  while (n && ++res)
    n -= n & (-n);
  return res;
}
template <class T> void chmax(T &a, const T &b) { a = max(a, b); }
template <class T> void chmin(T &a, const T &b) { a = min(a, b); }

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  int a[n + 1][n + 1];
  FOR(i, 0, n)
  FOR(j, 0, n)
  a[i][j] = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < i; j++)
      a[i][j] = i - j;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      // if(j>=y||i<=x)
      //     continue;

      // temp=min(j-i);
      int temp = abs(j - x) + 1 + abs(i - y);
      temp = min(temp, abs(i - x) + 1 + abs(j - y));

      a[i][j] = min(temp, a[i][j]);
      // if(j<=x&&i>=y){
      //     a[i][j]-=(y-x-1);
      // int temp=x-j + 1 + i-y;
      // if(a[i][j]>temp)
      //     a[i][j]=temp;
      // }
    }
  }
  // FOR(i,1,n){
  //     FOR(j,1,n){
  //         cout<<a[i][j]<<" ";
  //     }
  // cout<<endl;

  // }

  for (int p = 1; p < n; p++) {
    int ans = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j < i; j++) {
        if (a[i][j] == p)
          ans++;
      }
    cout << ans << endl;
  }
  return 0;
}