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

#include <bits/stdc++.h>

using namespace std;

long long int a[200111];
long long int f[200111];
long long int ans = 0;
int main() {
  long long int n;
  cin >> n;

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

  for (long long int i = 1; i <= n; i++)
    if (i + a[i] <= n)
      f[i + a[i]]++;

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

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

long long int a[200111];
long long int f[200111];
long long int ans = 0;
int main() {
  long long int n;
  cin >> n;

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

  for (long long int i = 1; i <= n; i++)
    f[i + a[i]]++;

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

  cout << ans << endl;

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

#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define ll long long

using namespace std;

int main() {
  ll N, ans = 0;
  cin >> N;
  vector<ll> A(N + 1, 0), B(N + 1, 0), C(N + 1, 0), D(N + 1, 0);

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

  for (ll i = 1; i <= N; i++) {
    if (i - A[i] >= 1) {
      B[i - A[i]] += 1;
    }
    if (i + A[i] <= N) {
      C[i + A[i]] += 1;
    }
  }

  for (ll i = 1; i <= N; i++) {
    D[i] = B[i] * C[i];
  }

  for (ll i = 1; i <= N; i++) {
    ans += D[i];
  }

  cout << ans << endl;

  return 0;
}


Error: -6
double free or corruption (out)


Fixed submission:

#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define ll long long

using namespace std;

int main() {
  int N, ans = 0;
  cin >> N;
  vector<int> A(N + 1, 0), B(N + 1, 0), C(N + 1, 0), D(N + 1, 0);

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

  for (size_t i = 1; i <= N; i++) {
    if (i - A[i] >= 1) {
      B[i - A[i]] += 1;
    }
    if (i + A[i] <= N) {
      C[i + A[i]] += 1;
    }
  }

  for (size_t i = 1; i <= N; i++) {
    D[i] = B[i] * C[i];
  }

  for (size_t i = 1; i <= N; i++) {
    ans += D[i];
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
#define endl "\\n"
#define print_array(name, n)                                                   \\
  for (ll o = 0; o < n; o++)                                                   \\
  cout << name[o] << " "
#define fr(i, f, t) for (ll i = (f); i < t; i++)
#define fb(i, f, t) for (ll i = (f); i >= t; i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define inf 2e18
bool sortbysecond(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.ss > b.ss);
}
template <typename T> string NumberTostring(T Number) {
  ostringstream ss;
  ss << Number;
  return ss.str();
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> vec(n + 1, 0);
  map<ll, ll> mp1;
  map<ll, ll> mp2;
  for (int i = 1; i <= n; i++) {
    cin >> vec[i];
    mp1[i - vec[i]]++;
    mp2[i + vec[i]]++;
  }
  ll cnt = 0;
  map<ll, ll>::iterator itr;
  for (itr = mp1.begin(); itr != mp1.end(); itr++) {
    if (mp2.find(itr->ff) != mp2.end()) {
      cnt += (itr->ss * mp2[itr->ff]);
    }
  }
  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
#define endl "\\n"
#define print_array(name, n)                                                   \\
  for (ll o = 0; o < n; o++)                                                   \\
  cout << name[o] << " "
#define fr(i, f, t) for (ll i = (f); i < t; i++)
#define fb(i, f, t) for (ll i = (f); i >= t; i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define inf 2e18
bool sortbysecond(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.ss > b.ss);
}
template <typename T> string NumberTostring(T Number) {
  ostringstream ss;
  ss << Number;
  return ss.str();
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> vec(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> vec[i];
  }
  ll cnt = 0;
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (abs(i - j) == vec[i] + vec[j]) {
        cnt++;
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

//
//  spy.cpp
//  learningCplusplus
//
//  Created by Tran Duc Chinh on 2020/04/04.
//  Copyright © 2020 Tran Duc Chinh. All rights reserved.
//

#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
ll mod = 1000000007;
#define PI 3.14159265

int main(int argc, const char *argv[]) {

  ll n;

  std::cin >> n;
  std::map<ll, ll> m;
  ll a[n];

  for (int i = 0; i < n; i++) {
    std::cin >> a[i];

    m[i + a[i]] += 1;
  }

  ll sum = 0;
  for (int i = 0; i < n; i++) {
    if (i > a[i]) {
      if (m.count(i - a[i])) {
        sum += m[i - a[i]];
        // std::cout << i-a[i] << " " << m[i-a[i]] << std::endl;
      }
    }
  }
  // for (std::map<ll, ll>::iterator it = m.begin(); it != m.end(); it++) {
  //   sum += it->second - 1;
  //   std::cout << it->first << " " << it->second << std::endl;
  // }

  std::cout << sum << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

//
//  spy.cpp
//  learningCplusplus
//
//  Created by Tran Duc Chinh on 2020/04/04.
//  Copyright © 2020 Tran Duc Chinh. All rights reserved.
//

#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
ll mod = 1000000007;
#define PI 3.14159265

int main(int argc, const char *argv[]) {

  ll n;

  std::cin >> n;
  ll a[n];

  ll dp[n];
  dp[0] = 0;
  std::cin >> a[0];

  for (int i = 1; i < n; i++) {
    std::cin >> a[i];
    dp[i] = dp[i - 1];
    for (int j = 0; j < i; j++) {
      if (a[i] + a[j] == i - j) {
        dp[i]++;
      }
    }
  }

  std::cout << dp[n - 1] << std::endl;

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

#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define Mod 1000000007

int main() {
  ll n;
  cin >> n;
  ll h[n];
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  vector<ll> sa;
  for (ll j = 0; j < n; ++j) {
    sa.push_back(j - h[j]);
  }
  ll ans = 0;
  sort(sa.begin(), sa.end());
  for (ll k = 0; k < n; ++k) {
    ans += upper_bound(sa.begin(), sa.end(), k + h[k]) -
           lower_bound(sa.begin(), sa.end(), k + h[k]);
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define Mod 1000000007

int main() {
  ll n;
  cin >> n;
  ll h[n];
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  vector<ll> sa;
  for (ll j = 0; j < n; ++j) {
    sa.push_back(j - h[j]);
  }
  ll ans = 0;
  sort(sa.begin(), sa.end());
  for (ll k = 0; k < n; ++k) {
    ans += upper_bound(sa.begin(), sa.end(), k + h[k]) -
           lower_bound(sa.begin(), sa.end(), k + h[k]);
    sa.erase(lower_bound(sa.begin(), sa.end(), k - h[k]));
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
#define ld long double
#define inf 0x7f7f7f7f7f7f7f7f
#define maxn 1000010
#define ri register int
#define il inline
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define mod 998244353
#define db(x) cout << x << endl;
#define eps 0.0001
using namespace std;
int a[maxn], b[maxn], c[maxn];
int s[maxn];
signed main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] + i <= 200000)
      b[a[i] + i]++;
    //	cout << i << " " << a[i]+i<<endl;
    if (i - a[i] > 0)
      c[i - a[i]]++;
  }
  int ans = 0;
  for (int i = 1; i <= 200000; i++)
    if (b[i]) {
      ans += b[i] * c[i];
    }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define ld long double
#define inf 0x7f7f7f7f7f7f7f7f
#define maxn 1000010
#define ri register int
#define il inline
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define mod 998244353
#define db(x) cout << x << endl;
#define eps 0.0001
using namespace std;
int a[maxn], b[maxn], c[maxn];
int s[maxn];
signed main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[a[i] + i]++;
    //	cout << i << " " << a[i]+i<<endl;
    if (i - a[i] > 0)
      c[i - a[i]]++;
  }
  int ans = 0;
  for (int i = 1; i <= 200000; i++)
    if (b[i]) {
      ans += b[i] * c[i];
    }
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

#define E "\\n"
#define int long long
#define f(i, l, n) for (int i = l; i < n; i++)

int solve() {
  int n;
  cin >> n;
  int a[n];
  f(i, 0, n) cin >> a[i];

  map<int, int> mp;
  int ans = 0;
  f(i, 0, n) {
    int temp = i + 1 - a[i];
    if (mp[temp] != 0)
      ans += mp[temp];
    mp[a[i] + i + 1]++;
  }
  cout << ans;
  return 0;
}

int32_t main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin >>t;
  while (t--)
    solve();
}

Error: WA

Fixed submission:

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

#define E "\\n"
#define int long long
#define f(i, l, n) for (int i = l; i < n; i++)

int solve() {
  int n;
  cin >> n;
  int a[n];
  f(i, 0, n) cin >> a[i];

  map<int, int> mp;
  int ans = 0;
  f(i, 0, n) {
    int temp = i + 1 - a[i];
    if (mp[temp] != 0)
      ans += mp[temp];
    mp[a[i] + i + 1]++;
  }
  cout << ans;
}

int32_t main() {

  int t = 1;
  // cin >>t;
  while (t--)
    solve();
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <set>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define ld long double
#define repl(i, n) for (ll i = 0; i < (ll)(n); i++)
#define outdeb(fir, sec, thr) cout << fir << ":" << sec << ":" << thr << endl

template <typename T> void inputList(vector<T> *list, int n, int sortType = 0) {
  repl(i, n) {
    T tmp;
    cin >> tmp;
    list->push_back(tmp);
  }
  if (sortType == 0) {
    sort(list->begin(), list->end());
  } else if (sortType == 1) {
    sort(list->begin(), list->end(), greater<T>());
  }
}

int main() {
  ll n;
  cin >> n;
  vector<ll> list;
  inputList<ll>(&list, n, 2);
  map<ll, ll> tmp;
  tmp[0] = 1;

  ll ans = 0;
  ll baseh = list[0];
  repl(i, n) {
    if (i == 0) {
      continue;
    }

    ll v = list[i] + list[0] - i;
    ans += tmp[v];

    ll v2 = -list[i] + list[0] - i;
    if (tmp.find(v2) == tmp.end()) {
      tmp[v2] = 1;
    } else {
      tmp[v2] = tmp[v2] + 1;
    }
  }

  cout << fixed << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <set>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define ld long double
#define repl(i, n) for (ll i = 0; i < (ll)(n); i++)
#define outdeb(fir, sec, thr) cout << fir << ":" << sec << ":" << thr << endl

template <typename T> void inputList(vector<T> *list, int n, int sortType = 0) {
  repl(i, n) {
    T tmp;
    cin >> tmp;
    list->push_back(tmp);
  }
  if (sortType == 0) {
    sort(list->begin(), list->end());
  } else if (sortType == 1) {
    sort(list->begin(), list->end(), greater<T>());
  }
}

int main() {
  ll n;
  cin >> n;
  vector<ll> list;
  inputList<ll>(&list, n, 2);
  ll ans = 0;

  repl(i, n) {
    for (ll j = i + list[i] + 1; j <= n; j++) {
      if (list[j] + list[i] == j - i) {
        ans++;
      }
    }
  }

  cout << fixed << ans << endl;

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

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  map<int, int> M;
  rep(i, N) { cin >> A[i]; }
  ll sum = 0;
  for (int i = N - 1; i >= 0; i--) {
    sum += M[i + 1 + A[i]];
    M[(i + 1) - A[i]]++;
  }
  cout << sum << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  multiset<int> M;
  rep(i, N) { cin >> A[i]; }
  ll sum = 0;
  for (int i = N - 1; i >= 0; i--) {
    sum += M.count(i + 1 + A[i]);
    M.insert((i + 1) - A[i]);
  }
  cout << sum << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mod 1000000007
#define N 100005

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll t = 1;
  // cin>>t;

  while (t--) {

    ll n;
    cin >> n;

    vector<ll> vec(n + 1);
    map<int, int> mp1, mp2;

    for (int i = 1; i <= n; i++) {
      cin >> vec[i];
      mp1[vec[i] - i]++;
      mp2[vec[i] + i]++;
    }

    ll ans = 0;
    for (int i = 1; i <= n; i++) {
      ll k1 = vec[i] - i;
      ll k2 = vec[i] + i;
      k1 = -k1;
      k2 = -k2;
      ans += mp1[k2] + mp2[k1];
    }
    ans /= 2;
    cout << ans << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mod 1000000007
#define N 100005

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll t = 1;
  // cin>>t;

  while (t--) {

    ll n;
    cin >> n;

    vector<ll> vec(n + 1);
    for (int i = 1; i <= n; i++) {
      cin >> vec[i];
    }

    ll ans = 0;

    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        if (abs(i - j) == vec[i] + vec[j])
          ans++;
      }
    }
    cout << ans << "\\n";
  }
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

// g++ -std=gnu++14 a.cpp

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
// #include <ostream>

using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  vector<int> wa(N + 10, 0);
  vector<int> sa(N + 10, 0);
  for (int i = 1; i <= N; i++) {
    if (i + A[i - 1] < 300000 && i + A[i - 1] >= 0)
      wa[i] = i + A[i - 1];
    if (i - A[i - 1] >= 0 && i - A[i - 1] < 300000)
      sa[i] = i - A[i - 1];
  }

  vector<int> cnt1(300000, 0);
  for (int i = 1; i <= N; i++) {
    cnt1[wa[i]]++;
  }
  vector<int> cnt2(300000, 0);
  for (int i = 1; i <= N; i++) {
    cnt2[sa[i]]++;
  }
  ll ans = 0;
  rep(i, 300000) {
    if (i == 0)
      continue;
    ll plus = 1ll * cnt1[i] * cnt2[i];
    ans += plus;
  }
  cout << ans << endl;
}


Error: WA

Fixed submission:

// g++ -std=gnu++14 a.cpp

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
// #include <ostream>

using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  vector<int> wa(N + 10, 0);
  vector<int> sa(N + 10, 0);
  for (int i = 1; i <= N; i++) {
    wa[i] = i + A[i - 1];
    if (i - A[i - 1] >= 0)
      sa[i] = i - A[i - 1];
  }

  vector<int> cnt1(300000, 0);
  for (int i = 1; i <= N; i++) {
    cnt1[wa[i]]++;
  }
  vector<int> cnt2(300000, 0);
  for (int i = 1; i <= N; i++) {
    cnt2[sa[i]]++;
  }
  int ans = 0;
  rep(i, 300000) {
    int plus = min(cnt1[i], cnt2[i]);
    ans += plus;
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

#define SIZE 200000

using namespace std;
long long int A[SIZE], A1[SIZE], A2[SIZE];

bool comparaison(long long int i, long long int j) {
  return ((i - A[i]) < (j - A[j]));
}

int main() {
  long long int i, j, k, N, M, K, Q, X;
  //  long long int A[100][100];

  cin >> N;

  long long int actuel, e;
  long encore;

  // on cherche d'abord les solutions positifs ;

  long long s, s2, s3;
  s = 0;

  for (i = 0; i < N; i++) {
    cin >> A[i];
    A1[i] = i - A[i];
    A2[i] = i + A[i];
  }

  sort(A1, A1 + N);
  sort(A2, A2 + N);

  /*
        for (i=0;i<N;i++)
          cerr << i << " " << pos[i]-A[pos[i]] << endl;
          */

  long long int a, b, c;
  i = 0;
  j = 0;
  while ((i < N) && (j < N)) {
    if (A1[i] == A2[j]) {
      k = j + 1;
      encore = 1;
      s2 = 1;
      while ((k < N) && encore) {
        if (A1[i] == A2[k]) {
          s2++; // s2 compte le nombre de collisions
        } else
          encore = 0;
        k++;
      }
      encore = 1;
      s3 = 1;
      i++;
      while ((i < N) && encore) {
        if (A1[i] == A1[i - 1]) {
          s3++;
          i++;
        } else
          encore = 0;
      }
      s += (s2 * s3);
    } else if (A1[i] < A2[j])
      i++;
    else
      j++;
  }

  cout << s << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

#define SIZE 200000

using namespace std;
long long int A[SIZE], A1[SIZE], A2[SIZE];

bool comparaison(long long int i, long long int j) {
  return ((i - A[i]) < (j - A[j]));
}

int main() {
  long long int i, j, k, N, M, K, Q, X;
  //  long long int A[100][100];

  cin >> N;

  long long int actuel, e;
  long encore;

  // on cherche d'abord les solutions positifs ;

  long long s;
  s = 0;

  for (i = 0; i < N; i++) {
    cin >> A[i];
    A1[i] = i - A[i];
    A2[i] = i + A[i];
  }

  sort(A1, A1 + N);
  sort(A2, A2 + N);

  /*
        for (i=0;i<N;i++)
          cerr << i << " " << pos[i]-A[pos[i]] << endl;
          */

  long long int a, b, c;
  i = 0;
  j = 0;
  while ((i < N) && (j < N)) {
    if (A1[i] == A2[j]) {
      k = j;
      encore = 1;
      while ((k < N) && encore) {
        if (A1[i] == A2[k]) {
          s++;
        } else
          encore = 0;
        k++;
      }
      i++;
    } else if (A1[i] < A2[j])
      i++;
    else
      j++;
  }

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

#include <algorithm> // std::unique
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

using ll = long long;
constexpr auto PrimeBigNum10_9_7 = 1000000007;

// 素数判定
template <typename T> bool IsPrimeNumber(T value) {

  for (int i = 2; i * i <= value; ++i) {
    if ((value % i) == 0) {
      return true;
    }
  }
  return false;
}

// 桁数判定
template <typename T> T GetFigureLength(T value) {
  T figure = 1;
  while (value >= 10) {
    value /= 10;
    ++figure;
  }
  return figure;
}

// 素因数分解結果
template <typename T> map<T, T> GetPrimeFactorize(T value) {

  map<T, T> res;

  for (T i = 2; i * i <= value; ++i) {
    if ((value % i) != 0)
      continue;

    T count = 0;
    while ((value % i) == 0) {
      value /= i;
      ++count;
    }

    res[i] = count;
  }

  if (value != 1) {
    res[value] = 1;
  }
  return res;
}

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

  map<ll, ll> answers;

  ll resultCount = 0;

  for (int i = 0; i < n; ++i) {
    ll a;
    cin >> a;

    resultCount += answers[i - a];
    answers[i + a]++;
  }

  auto output = resultCount;
  cout << output;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm> // std::unique
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

using ll = long long;
constexpr auto PrimeBigNum10_9_7 = 1000000007;

// 素数判定
template <typename T> bool IsPrimeNumber(T value) {

  for (int i = 2; i * i <= value; ++i) {
    if ((value % i) == 0) {
      return true;
    }
  }
  return false;
}

// 桁数判定
template <typename T> T GetFigureLength(T value) {
  T figure = 1;
  while (value >= 10) {
    value /= 10;
    ++figure;
  }
  return figure;
}

// 素因数分解結果
template <typename T> map<T, T> GetPrimeFactorize(T value) {

  map<T, T> res;

  for (T i = 2; i * i <= value; ++i) {
    if ((value % i) != 0)
      continue;

    T count = 0;
    while ((value % i) == 0) {
      value /= i;
      ++count;
    }

    res[i] = count;
  }

  if (value != 1) {
    res[value] = 1;
  }
  return res;
}

vector<ll> scores;
ll resultCount;

bool IsApplicable(ll index1, ll index2) {

  ll score1 = scores[index1] + scores[index2];
  ll score2 = index2 - index1;

#if 0 
	{
		cout << index1 << " " << index2 << " ";
		cout << ":" << scores[index1]  << " " << scores[index2];
		cout << ":" << score1 << " " << score2;
		cout << "=" << ((score1 == score2) ? "@" : "");
		cout << endl;
	}
#endif
  return score1 == score2;
}

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

  scores.clear();
  resultCount = 0;

  for (int i = 0; i < n; ++i) {
    ll a;
    cin >> a;
    scores.push_back(a);
  }

  for (ll i = 0; i < n; ++i) {
    for (ll j = i + 1; j < n; ++j) {
      if (IsApplicable(i, j)) {
        resultCount++;
      }
    }
  }

  auto output = resultCount;
  cout << output;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, sum = 0;
  cin >> n;
  vector<long long int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<long long int> c(n);
  map<long long int, long long int> m;
  for (int i = n - 1; i >= 0; i--) {
    c[i] = m[i + a[i]];
    m[i - a[i]]++;
  }
  for (int i = 0; i < n; i++) {
    sum += c[i];
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, sum = 0;
  cin >> n;
  vector<long long int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<map<int, int>> c(n);
  map<int, int> m;
  for (int i = n - 1; i >= 0; i--) {
    c[i] = m;
    m[i - a[i]]++;
  }
  for (int i = 0; i < n; i++) {
    sum += c[i][i + a[i]];
  }
  cout << sum << endl;
}