alexjercan/bugnet huggingface dataset:
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
using namespace std;
int a[200010];
int b[200010];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if ((a[i] + i + 1) <= 200005 && a[i] <= 200000) {
      b[a[i] + i + 1]++;
    }
    if ((i - a[i] + 1) >= 0)
      ans += b[i - a[i] + 1];
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[200010];
int b[200010];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if ((a[i] + i + 1) <= 200005 && a[i] <= 200000) {
      b[a[i] + i + 1]++;
    }
    ans += b[i - a[i] + 1];
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
using namespace std;
signed main() {
  ll n, a[1000000], ans = 0;
  ll l[1000000] = {}, r[1000000] = {};
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (ll i = 1; i <= n; i++) {
    if (a[i] + 1 <= n) {
      l[a[i] + i]++;
    }
    if (i - a[i] >= 1) {
      r[i - a[i]]++;
    }
  }
  for (ll i = 1; i <= n; i++) {
    ans += l[i] * r[i];
  }
  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
signed main() {
  ll n, a[1000000], ans = 0;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (ll i = n; i > 0; i--) {
    for (ll j = i; j > 0; j--) {
      if (a[i] + a[j] == abs(i - j)) {
        ans++;
      }
    }
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define mll map<long long, long long>
#define vl vector<long long>
#define pb push_back
mll m;
vl v;

int main(void) {
  long long i, n, j, a;
  cin >> n;
  v.resize(n);
  for (i = 0; i < n; i++) {

    cin >> a;
    v[i] = i + a;
    m[i - a]++;
  }
  a = 0;
  for (i = 0; i < n; i++) {
    a += m[v[i]];
  }
  cout << a;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #include <stdlib.h>

int main() {
  vector<int> height;
  int n, i, j, number = 0, min = 0, max = 0;
  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%d", &number);
    // if(number>max)max=number;
    // if(number<min||i==0)min=number;
    height.push_back(number);
  }
  // printf("%u\\n",min);
  number = 0;
  max = *max_element(height.begin(), height.end());
  min = *min_element(height.begin(), height.end());

  for (i = 0; i < n - 1; i++) {
    j = (2 * min > height[i]) ? i + 2 * min : i + height[i];

    for (; j < n && (j - i) <= 2 * max; j++) {
      if ((j - i) == height[i] + height[j]) {
        number++;
      }
    }
  }

  printf("%d", number);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

/*
|i-j| == hi + hj != 0 ... i != j
j<i ; i - hi == j + hj
*/

int main() {
  int n = 0, x = 0;
  long long ans = 0;
  cin >> n;
  vector<int> vec(n);
  map<int, int> map;
  rep(i, n) {
    cin >> x;
    vec.at(i) = x;
    if (i + 1 - x > 0) {
      if (!map.count(i + 1 - x))
        map[i + 1 - x] = 0;
      map.at(i + 1 - x) += 1;
    }
  }

  rep(i, n) {
    if (map.count(i + 1 + vec.at(i)))
      ans += map.at(i + 1 + vec.at(i));
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n, ans = 0;
  cin >> n;
  vector<int> vec(n);
  rep(i, n) cin >> vec.at(i);

  rep(i, n) {
    for (int j = i + 1; j < n; ++j) {
      if ((j - i) == vec.at(i) + vec.at(j))
        ++ans;
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define vll vector<long long>
#define vi vector<int>
#define wh(t) while (t--)
#define fi(n) for (long long i = 0; i < n; i++)
#define fj(n) for (long long j = 0; j < n; j++)
#define fu(i, s, e) for (ll i = s; i <= e; i++)
#define fd(i, s, e) for (ll i = s; i >= e; i--)
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define sll stack<ll>
#define qll queue<ll>
using namespace std;
const ll M = 1e9 + 7;
// const ll M = 998244353;

ll pmp(ll x, ll n) {
  ll a = 1;
  while (n > 0) {
    if (n & 1)
      a = (a * x) % M;
    x = (x * x) % M;
    n = n >> 1;
  }
  return a;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t;
  t = 1;
  // cin >> t;
  wh(t) {
    ll n;
    cin >> n;
    vll a(n);
    fi(n) cin >> a[i];
    vll L(n);
    vll R(n);
    fi(n) {
      L[i] = i + a[i];
      R[i] = i - a[i];
    }
    sort(R.begin(), R.end());
    ll count = 0;
    fi(n) {
      ll j = lower_bound(R.begin(), R.end(), L[i]) - R.begin();
      ll jj = upper_bound(R.begin(), R.end(), L[i]) - R.begin();
      if (j != n && R[j] == L[i]) {
        count = count + jj - j;
      }
    }
    cout << count;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define vll vector<long long>
#define vi vector<int>
#define wh(t) while (t--)
#define fi(n) for (long long i = 0; i < n; i++)
#define fj(n) for (long long j = 0; j < n; j++)
#define fu(i, s, e) for (ll i = s; i <= e; i++)
#define fd(i, s, e) for (ll i = s; i >= e; i--)
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define sll stack<ll>
#define qll queue<ll>
using namespace std;
const ll M = 1e9 + 7;
// const ll M = 998244353;

ll pmp(ll x, ll n) {
  ll a = 1;
  while (n > 0) {
    if (n & 1)
      a = (a * x) % M;
    x = (x * x) % M;
    n = n >> 1;
  }
  return a;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t;
  t = 1;
  // cin >> t;
  wh(t) {
    ll n;
    cin >> n;
    vll a(n);
    fi(n) cin >> a[i];
    vll L(n);
    vll R(n);
    fi(n) {
      L[i] = i + a[i];
      R[i] = i - a[i];
    }
    sort(R.begin(), R.end());
    ll count = 0;
    fi(n) {
      ll j = lower_bound(R.begin(), R.end(), L[i]) - R.begin();
      if (j != n && R[j] == L[i]) {
        // cout << i << ' ' << j << endl;
        while (j < n && R[j] == L[i]) {
          count++;
          j++;
        }
      }
    }
    cout << count;
  }
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {

  int n;
  cin >> n;
  map<ll, ll> m;
  vector<ll> sum(n);
  rep(i, n) {
    ll a;
    cin >> a;
    ll gap = i - a;
    sum[i] = i + a;
    if (gap > 0) {
      if (m.count(gap) == 0)
        m.insert(make_pair(gap, 1));
      else {
        ll n = m[gap];
        ++n;
        m.erase(gap);
        m.insert(make_pair(gap, n));
      }
    }
  }

  ll ans = 0;
  rep(i, n) {
    if (m.count(sum[i]) != 0)
      ans += m[sum[i]];
  }

  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
#include <numeric>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main() {

  int n, m;
  cin >> n >> m;

  vector<int> h(n);
  rep(i, n) cin >> h[i];

  vector<vector<int>> ab(m, vector<int>(2));
  rep(i, m) cin >> ab[i][0] >> ab[i][1];

  vector<int> ok(n, 1);
  rep(i, m) {
    if (h[ab[i][0] - 1] <= h[ab[i][1] - 1])
      ok[ab[i][0] - 1] = 0;
    if (h[ab[i][1] - 1] <= h[ab[i][0] - 1])
      ok[ab[i][1] - 1] = 0;
  }

  cout << accumulate(ok.begin(), ok.end(), 0) << endl;

  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;

  long long temp;
  long long counter = 0;
  map<long long, int> ms;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    counter += ms[i - temp];
    ms[i + temp]++;
  }

  cout << counter << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;

  int temp;
  int counter = 0;
  unordered_multiset<int> ms;
  for (int i = 0; i < n; i++) {
    scanf("%d", &temp);
    counter += ms.count(i - temp);
    ms.insert(i + temp);
  }

  cout << counter << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
#define LL long long

int N, A[222222], mi[555555] = {};
LL ans = 0ll;

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    if (i - A[i] > 0)
      mi[i - A[i]]++;
  }
  for (int i = 1; i <= N; i++) {
    int pl = i + A[i];
    if (pl < 500000)
      ans += mi[pl];
    if (i - A[i] > 0)
      mi[i - A[i]]--;
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
#define LL long long

int N, A[222222], mi[555555] = {};
LL ans = 0ll;

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    if (i - A[i] > 0)
      mi[i - A[i]]++;
  }
  for (int i = 1; i <= N; i++) {
    int pl = i + A[i];
    ans += mi[pl];
    if (i - A[i] > 0)
      mi[i - A[i]]--;
  }
  cout << ans << endl;

  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

//
//  main.cpp
//  CoderProj
//
//  Created by Minoru Hayashi on 2020/05/02.
//  Copyright © 2020 Minoru Hayashi. All rights reserved.
//
//  ABC165
//
//

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)

using ll = long long;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

// const ll INF = 1LL<<60;
// const int INF = 1<<29;
// const int MOD = 1000000007;
// #define __DBG__

void solve(void) {

  int N;

  cin >> N;

  vector<int> A(N + 1, 0);
  map<int, int> C;

  REP2(i, 1, N + 1) {
    cin >> A[i];

    auto itr = C.find(i - A[i]);
    if (itr != C.end()) {
      C[i - A[i]]++;
    } else {
      C[i - A[i]] = 1;
    }
  }
  ll ans = 0;

  REP2(i, 1, N + 1) {
    auto itr = C.find(i + A[i]);
    if (itr != C.end()) {
      ans += C[i + A[i]];
    }
  }
  cout << ans << endl;
  return;
}

#define TESTCASES 1

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  REP(i, TESTCASES) { solve(); }

  return 0;
}


Error: TLE

Fixed submission:

//
//  main.cpp
//  CoderProj
//
//  Created by Minoru Hayashi on 2020/05/02.
//  Copyright © 2020 Minoru Hayashi. All rights reserved.
//
//  ABC165
//
//

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)

using ll = long long;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

// const ll INF = 1LL<<60;
// const int INF = 1<<29;
// const int MOD = 1000000007;
// #define __DBG__

void solve(void) {

  int N;

  cin >> N;

  vector<int> A(N + 1, 0);
  vector<int> B, C;

  REP2(i, 1, N + 1) {
    cin >> A[i];
    B.push_back(A[i] + i);
    C.push_back(i - A[i]);
  }
  ll ans = 0;
  /*
    REP(i,N){
      cout << i+1 << " = A " << A[i+1] << " : B " << B[i] << " : C " << C[i] <<
    endl;
    }
   */
  for (auto v : B) {
    ans += count(ALL(C), v);
  }
  // ans/=2;
  cout << ans << endl;
  return;
}

#define TESTCASES 1

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  REP(i, TESTCASES) { solve(); }

  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  rep(i, n) cin >> a.at(i);
  map<long, long> mi, pu;
  rep(i, n) {
    mi[1 + i - a.at(i)]++;
    pu[1 + i + a.at(i)]++;
  }
  long long ans = 0;
  rep(i, n) ans += mi[i] * pu[i];
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  long long ans = 0;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++)
    cin >> a.at(i);
  for (long long i = 0; i < n; i++) {
    for (long long j = i + 1 + a.at(i); j < n; j++) {
      if (a.at(i) + a.at(j) == j - i)
        ans++;
    }
  }
  cout << ans;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  long long prs[n + 1] = {0};
  long long mns[n + 1] = {0};
  for (int i = 1; i <= n; i++) {
    if (i - a[i] >= 0) {
      mns[i - a[i]]++;
    }
    if (i + a[i] < n + 1) {
      prs[i + a[i]]++;
    }
  }

  long long ans = 0;
  for (int i = 0; i <= n; i++) {
    ans += prs[i] * mns[i];
  }
  cout << ans << endl;

  /*
  int hssize = n+1;
  int *hstbm;
  int *hstbp;
  hstbm = new int[hssize];
  hstbp = new int[hssize];
  */

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  int prs[n + 1];
  int mns[n + 1];
  for (int i = 1; i <= n; i++) {
    prs[i] = i + a[i];
    mns[i] = i - a[i];
  }

  int hssize = n + 1;
  int *hstbm;
  int *hstbp;
  hstbm = new int[hssize];
  hstbp = new int[hssize];

  for (int i = 1; i <= n; i++) {
    hstbp[prs[i]]++;
    hstbm[mns[i]]++;
  }

  long long ans = 0;
  for (int i = 1; i <= hssize; i++) {
    ans += hstbp[i] * hstbm[i];
  }

  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<long long>;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

constexpr long long mod = 1000000007;

#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vi a(n + 1, -1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  map<int, ll> mp1, mp2;

  for (int i = 1; i <= n; i++) {
    if (mp1.find(i + a[i]) != mp1.end()) {
      mp1[i + a[i]]++;
    } else {
      mp1[i + a[i]] = 1;
    }

    if (i - a[i] >= 0) {
      if (mp2.find(i - a[i]) != mp2.end()) {
        mp2[i - a[i]]++;
      } else {
        mp2[i - a[i]] = 1;
      }
    }
  }

  ll c = 0;
  for (auto itr = mp1.begin(); itr != mp1.end(); itr++) {
    if (mp2.find(itr->first) != mp2.end()) {
      c += itr->second * mp2[itr->first];
    }
  }

  cout << c << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<long long>;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

constexpr long long mod = 1000000007;

#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vi a(n + 1, -1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  int m = *max_element(a.begin(), a.end());

  vll cnt1(m + n + 1, 0);
  vll cnt2(m + n + 1, 0);

  for (int i = 1; i <= n; i++) {
    cnt1[i + a[i]]++;

    if (i - a[i] >= 0) {
      cnt2[i - a[i]]++;
    }
  }

  ll c = 0;
  for (int i = 0; i <= m + n; i++) {
    c += cnt1[i] * cnt2[i];
  }

  cout << c << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int INF = 1e9;
const int MOD = 1e9 + 7;

int main() {
  long long int n, r;
  cin >> n;
  map<long long int, long long int> mp;
  r = 0;
  for (long long int i = 1; i <= n; i++) {
    long long int alt;
    cin >> alt;
    r += mp[i - alt];
    mp[i + alt]++;
  }
  cout << r << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int INF = 1e9;
const int MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> vals(n);
  vector<int> vis(200100, 0);
  for (int i = 0; i < n; i++) {
    int alt;
    cin >> alt;
    vals[i] = alt + i;
    int val = i - alt;
    if (val <= 2e5 && val >= 0)
      vis[val]++;
  }
  int r = 0;
  for (int i = 0; i < n; i++) {
    r += vis[vals[i]];
  }
  cout << r << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define vsort(v) sort(v.begin(), v.end())
#define ll long long
#define rep(i, s, g) for (ll i = (s); i < (g); i++)
#define MOD 1000000007
using namespace std;
int main() {
  ll N, tallest = 0, ans = 0;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    if (tallest < A[i]) {
      tallest = A[i];
    }
  }
  // L = i + Ai, R = i - Ai
  vector<ll> L(N, 0), R(N, 0);
  for (ll i = 0; i < N; i++) {
    if (i + A[i] > 0 && i + A[i] < N)
      L[i + A[i]]++;
    if (i - A[i] > 0 && i - A[i] < N)
      R[i - A[i]]++;
  }
  for (ll x = 1; x < N; x++) { // time consuming
    ans += L[x] * R[x];
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define vsort(v) sort(v.begin(), v.end())
#define ll long long
#define rep(i, s, g) for (ll i = (s); i < (g); i++)
#define MOD 1000000007
using namespace std;
int main() {
  ll N, tallest = 0, ans = 0;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    if (tallest < A[i]) {
      tallest = A[i];
    }
  }
  // L = i + Ai, R = i - Ai
  vector<ll> L(N + tallest, 0), R(N + tallest, 0);
  for (ll i = 0; i < N; i++) {
    L[i + A[i]]++;
    if (i - A[i] > 0) {
      R[i - A[i]]++;
    }
  }
  for (ll x = 1; x < N + tallest; x++) {
    ans += L[x] * R[x];
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

int main(void) {
  long long int cnt = 0, n, i;
  scanf("%lld", &n);
  vector<long long int> a(n), a_plus(n), a_minus(n), ans(n, 0);
  for (i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  for (i = 0; i < n; i++) {
    a_plus[i] = i + a[i];
    a_minus[i] = i - a[i];
  }
  for (i = 0; i < n; i++) {
    if (a_plus[i] < n) {
      ans[a_plus[i]]++;
    }
  }
  for (i = 0; i < n; i++) {
    if (a_minus[i] < n && a_minus[i] > 0) {
      cnt += ans[a_minus[i]];
    }
  }
  printf("%lld\\n", cnt);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

int main(void) {
  long long int n, i;
  scanf("%d", &n);
  long long int cnt = 0;
  vector<long long int> a(n), a_plus(n), a_minus(n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (i = 0; i < n; i++) {
    a_plus[i] = (i + 1) + a[i];
    a_minus[i] = (i + 1) - a[i];
  }

  sort(a_plus.begin(), a_plus.end(), greater<long long int>());
  vector<long long int> ans(a_plus[0] + 1, 0);
  for (i = 0; i < n; i++) {
    ans[a_plus[i]]++;
  }
  for (i = 0; i < n; i++) {
    if (a_minus[i] > 0) {
      cnt += ans[a_minus[i]];
    }
  }
  printf("%d\\n", cnt);

  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N;
  ll ans = 0;
  cin >> N;
  vector<int> A(N + 1);
  map<int, int> cnt;
  REP(i, N) { cin >> A[i]; }
  REP(i, N) {
    ans += cnt[i - A[i]];
    cnt[i + A[i]]++;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N;
  int ans = 0;
  cin >> N;
  vector<int> A(N + 1);
  REP(i, N) { cin >> A[i]; }
  REP(i, N - 1) {
    for (int j = A[i] + 1; j <= N; j++) {
      if (A[i] + A[j] == j - i) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<int> vi;

int main(void) {
  int N;

  cin >> N;
  vector<ll> A(N + 3);

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  ll c = 0;
  map<int, int> mp;

  for (int i = 1; i <= N; i++) {
    int b = i - A[i];
    int d = i + A[i];
    c += mp[b];
    ++mp[d];
  }

  cout << c;

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<int> vi;

int main(void) {
  int N;

  cin >> N;
  vector<ll> A(N + 3);

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  int c = 0;

  for (int i = 1; i <= N; i++) {
    for (int j = i + 1; j <= N; j++) {
      if ((j - i) == A[i] + A[j]) {
        c++;
      }
    }
  }

  cout << c;

  return 0;
}