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


// author-Shivam gupta

#include <bits/stdc++.h>

using namespace std;

#define MEM(a, b) memset(a, (b), sizeof(a))

#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)

#define IN(A, B, C) assert(B <= A && A <= C)

#define MP make_pair
#define FOR(i, a) for (int i = 0; i < a; i++)

#define FOR1(i, j, a) for (int i = j; i < a; i++)

#define EB emplace_back

#define INF (int)1e9

#define EPS 1e-9

#define PI 3.1415926535897932384626433832795

#define MOD 1000000007
const int N = 3e5 + 5;

#define read(type) readInt<type>()
#define max1 10000001
#define out(x) cout << x << '\\n'
#define out1(x) cout << x << " "
#define END cout << '\\n'
const double pi = acos(-1.0);

typedef pair<int, int> PII;

typedef vector<int> VI;

typedef vector<string> VS;

typedef vector<PII> VII;

typedef vector<VI> VVI;

typedef map<int, int> MPII;

typedef set<int> SETI;

typedef multiset<int> MSETI;

typedef long int li;

typedef unsigned long int uli;

typedef long long int ll;

typedef unsigned long long int ull;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 998244353;

bool isPowerOfTwo(int x) {
  /* First x in the below expression is
    for the case when x is 0 */
  return x && (!(x & (x - 1)));
}
void fast() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
ll power(ll x, ll y, ll p) {
  ll res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

// Returns n^(-1) mod p
ll modInverse(ll n, ll p) { return power(n, p - 2, p); }

// Returns nCr % p using Fermat's little
// theorem.

int main() {
  fast();
  ll n;

  cin >> n;

  vector<ll> a(n);

  FOR(i, n) cin >> a[i];

  vector<ll> x(n);

  vector<ll> y(n);

  vector<ll> h1(2 * n + 1, 0);

  vector<ll> h2(2 * n + 1, 0);

  FOR(i, n) {

    x[i] = i - a[i];

    if (x[i] <= n && x[i] >= -n)
      h1[x[i] + n]++;

    y[i] = i + a[i];

    if (y[i] <= n && y[i] >= -n)
      h2[y[i] + n]++;
  }

  ll cnt = 0;

  FOR(i, n) {

    ll t;

    t = i + a[i];

    if (t <= n && t >= -n)
      cnt += h1[t + n];

    t = i - a[i];

    if (t <= n && t >= -n)
      cnt += h2[t + n];
  }

  cout << cnt / 2 << endl;

  return 0;
}
/*

           .$                                  $.
          /:;                                  :;\\
         : $                                    $ ;
         ;:$                                    $;:
        : $:              ________              ;$ ;
        ; $;;     _..gg$$SSP^^^^T$S$$pp.._     ::$ :
       : :$;|  .g$$$$$$SSP"      "TS$$$$$$$p.  |:$; ;
       ; :$;:.d$$$$$$$SSS          SS$$$$$$$$b.;:$; :
      :  :$$$$$$$$$$$$SSS          SS$$$$$$$$$$$$$;  ;
      ;  $$$$$$$$$$$$$$SSb.      .dS$$$$$$$$$$$$$$;  :
     :  :S$$$$$$$$$$$$$$SSSSppggSSS$$$$$$$$$$$$$$$;   ;
     |  :SS$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$;   :
     |  :SS$$$$$$$$$$$$$$$$$^^^^^^^^^$$$$$$$$$$$$$$   :
     ;   SS$$$$$$$$$$P^"                 "^T$$$$$$$   :
    :    :SS$$$$$$$$$                       T$$$$$;   :
    |     SSS$$$$$$$;                        T$$$$    :
    |     :SSS$$$$$$;                        :$$$;    :
    ;      SSS$$$$$$;                        :S$$;    :
    ;      :SS$$P"^P                          S$$;    :
    ;    ..d$$$P    `                         S$$$    :
    ;     T$$$P                          dS   T$$$b.  :
    ;    :$$$$.     .                   dSS;   $$$$$b.:
    ;    :$$$$$b     Tb.   .          .dSS$$b.d$$$$$$$:
    :    $$$$$$$b     TSb   Tb..g._, :$$SS$$$$$$SSS$$$:
    :   :$$$$$$$$b     SSb   T$SS$P   "^TS$$$$$$P"TS$$:
    :   $$$$$$$$$$b._.dS$$b _ T$$P _     TSS$SSP  :SS$:
    :  :$$$$SSS$$$$$$$$$P" d$b. _.d$P     TSSP"    SS$:
    :  :P"TSSP"^T$$$$$$P  :$$$$$$$$P d$$b          $S$;
    :  :b.dS^^-.  ""^^"    $b T d$$$s$$$$b __..--""$ $;
    :  :$$$S    ""^^..ggSS$$$$$$$$$$$$$$P^^""     .$ $;
    :   $$$$$pp..__   `j$$$$$$^$$$$$b. d....ggppTSSS$$;
    :   $$$$SP     """t  :$$$$ $$$$$$$b.  d$b    `TSS$;
    :  \\:$$SP   _.gd$$P_d$$$$$ $$$$$$$$$bd$P'    .dSPd;
    :   \\"^S     "^T$$$$$$$$$$ $$$$$SS$$$$b.    dSS'd$;
    $    $. "-.__.gd$$$$$$$SP:S$$P  TSS$$$$$bssS^".d$$:
   :$    $$b.   ""^^T$$$$SP' :S$P    TSSSP^^""  .d$$$$:
   :$   :$$$P        "^SP'   :S;     .^"`.     $$$$$$$:
   $;   :$$$            "-.  :S;  .-"     \\    :$$$$$$:
  .$ :  $$$;   :           `.:S;.'         ;    $$$$$$:;
 .P :S  $$$    ;             `^'                :$$$$$:;
.P  S;.d$$;   :               -'                 $$$$$:;
$  :SS$$$$    ;     __....----  --...____        :$$$ :S
$  $SSS$$;    :  ; d$$$$$$$pppqqqq$$$$$$L;       :$$$ SS
: :SSSSS$$     ; : \\ "^T$$$$$$$$$$$$$P' .':      $PT$ SS;
 $SP^"^TSP\\    :  \\ "-.  """"""""""" .-"  ;     /   $ SSSb.
 :S     S  \\           "--...___..--"    /  :  /    :gSSSSSb.
  T bug T   \\       `.      _____       /   ; /
   `         \\ :      "==="""""""""===""   : /
              `:                           ;'
                "-.                     .-"
                   ""--..         ..--""
*/


Error: TLE

Fixed submission:


// author-Shivam gupta

#include <bits/stdc++.h>

using namespace std;

#define MEM(a, b) memset(a, (b), sizeof(a))

#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)

#define IN(A, B, C) assert(B <= A && A <= C)

#define MP make_pair
#define FOR(i, a) for (int i = 0; i < a; i++)

#define FOR1(i, j, a) for (int i = j; i < a; i++)

#define EB emplace_back

#define INF (int)1e9

#define EPS 1e-9

#define PI 3.1415926535897932384626433832795

#define MOD 1000000007
const int N = 3e5 + 5;

#define read(type) readInt<type>()
#define max1 10000001
#define out(x) cout << x << '\\n'
#define out1(x) cout << x << " "
#define END cout << '\\n'
const double pi = acos(-1.0);

typedef pair<int, int> PII;

typedef vector<int> VI;

typedef vector<string> VS;

typedef vector<PII> VII;

typedef vector<VI> VVI;

typedef map<int, int> MPII;

typedef set<int> SETI;

typedef multiset<int> MSETI;

typedef long int li;

typedef unsigned long int uli;

typedef long long int ll;

typedef unsigned long long int ull;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 998244353;

bool isPowerOfTwo(int x) {
  /* First x in the below expression is
    for the case when x is 0 */
  return x && (!(x & (x - 1)));
}
void fast() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
ll power(ll x, ll y, ll p) {
  ll res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

// Returns n^(-1) mod p
ll modInverse(ll n, ll p) { return power(n, p - 2, p); }

// Returns nCr % p using Fermat's little
// theorem.

int main() {
  fast();
  int n;
  cin >> n;
  vector<pair<ll, int>> v;
  pair<ll, int> p;
  ll x;
  for (int i = 0; i < n; i++) {
    cin >> x;
    p.first = x;
    p.second = i;
    v.EB(p);
  }
  sort(v.begin(), v.end());
  int i = 0;
  int cnt = 0;
  while (i < n) {
    if (v[i].first > n - 1) {
      break;
    }
    int j = i + 1;

    while (j < n) {
      if ((v[j].first + v[i].first) >= n) {
        break;
      }
      int x = abs(v[j].second - v[i].second);
      if (x == (v[j].first + v[i].first)) {
        cnt++;
      }
      j++;
    }
    i++;
  }
  cout << cnt;
}
/*

           .$                                  $.
          /:;                                  :;\\
         : $                                    $ ;
         ;:$                                    $;:
        : $:              ________              ;$ ;
        ; $;;     _..gg$$SSP^^^^T$S$$pp.._     ::$ :
       : :$;|  .g$$$$$$SSP"      "TS$$$$$$$p.  |:$; ;
       ; :$;:.d$$$$$$$SSS          SS$$$$$$$$b.;:$; :
      :  :$$$$$$$$$$$$SSS          SS$$$$$$$$$$$$$;  ;
      ;  $$$$$$$$$$$$$$SSb.      .dS$$$$$$$$$$$$$$;  :
     :  :S$$$$$$$$$$$$$$SSSSppggSSS$$$$$$$$$$$$$$$;   ;
     |  :SS$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$;   :
     |  :SS$$$$$$$$$$$$$$$$$^^^^^^^^^$$$$$$$$$$$$$$   :
     ;   SS$$$$$$$$$$P^"                 "^T$$$$$$$   :
    :    :SS$$$$$$$$$                       T$$$$$;   :
    |     SSS$$$$$$$;                        T$$$$    :
    |     :SSS$$$$$$;                        :$$$;    :
    ;      SSS$$$$$$;                        :S$$;    :
    ;      :SS$$P"^P                          S$$;    :
    ;    ..d$$$P    `                         S$$$    :
    ;     T$$$P                          dS   T$$$b.  :
    ;    :$$$$.     .                   dSS;   $$$$$b.:
    ;    :$$$$$b     Tb.   .          .dSS$$b.d$$$$$$$:
    :    $$$$$$$b     TSb   Tb..g._, :$$SS$$$$$$SSS$$$:
    :   :$$$$$$$$b     SSb   T$SS$P   "^TS$$$$$$P"TS$$:
    :   $$$$$$$$$$b._.dS$$b _ T$$P _     TSS$SSP  :SS$:
    :  :$$$$SSS$$$$$$$$$P" d$b. _.d$P     TSSP"    SS$:
    :  :P"TSSP"^T$$$$$$P  :$$$$$$$$P d$$b          $S$;
    :  :b.dS^^-.  ""^^"    $b T d$$$s$$$$b __..--""$ $;
    :  :$$$S    ""^^..ggSS$$$$$$$$$$$$$$P^^""     .$ $;
    :   $$$$$pp..__   `j$$$$$$^$$$$$b. d....ggppTSSS$$;
    :   $$$$SP     """t  :$$$$ $$$$$$$b.  d$b    `TSS$;
    :  \\:$$SP   _.gd$$P_d$$$$$ $$$$$$$$$bd$P'    .dSPd;
    :   \\"^S     "^T$$$$$$$$$$ $$$$$SS$$$$b.    dSS'd$;
    $    $. "-.__.gd$$$$$$$SP:S$$P  TSS$$$$$bssS^".d$$:
   :$    $$b.   ""^^T$$$$SP' :S$P    TSSSP^^""  .d$$$$:
   :$   :$$$P        "^SP'   :S;     .^"`.     $$$$$$$:
   $;   :$$$            "-.  :S;  .-"     \\    :$$$$$$:
  .$ :  $$$;   :           `.:S;.'         ;    $$$$$$:;
 .P :S  $$$    ;             `^'                :$$$$$:;
.P  S;.d$$;   :               -'                 $$$$$:;
$  :SS$$$$    ;     __....----  --...____        :$$$ :S
$  $SSS$$;    :  ; d$$$$$$$pppqqqq$$$$$$L;       :$$$ SS
: :SSSSS$$     ; : \\ "^T$$$$$$$$$$$$$P' .':      $PT$ SS;
 $SP^"^TSP\\    :  \\ "-.  """"""""""" .-"  ;     /   $ SSSb.
 :S     S  \\           "--...___..--"    /  :  /    :gSSSSSb.
  T bug T   \\       `.      _____       /   ; /
   `         \\ :      "==="""""""""===""   : /
              `:                           ;'
                "-.                     .-"
                   ""--..         ..--""
*/