alexjercan/bugnet huggingface dataset:
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
using namespace std;

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

  map<int, int> p;
  long long ans = 0;
  long long wa, sa, data;
  for (int i = 0; i < n; i++) {
    cin >> data;
    sa = i - data;
    ans += p[sa];
    wa = i + data;
    p[wa]++;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define N 200000
int main() {
  int a[N];
  int n;

  cin >> n;

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

  int ans = 0;

  for (int j = 1; j < n; j++) {
    for (int k = 0; k < n - j; k++) {
      if (a[k] + a[k + j] == j)
        ans++;
    }
  }

  cout << ans << endl;

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

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

typedef long long
    ll; // -9,223,372,036,854,775,808 から 9,223,372,036,854,775,807

template <class T> using vec = std::vector<T>;
template <class T> using vec2 = vec<vec<T>>;
template <class T> using vec3 = vec<vec<vec<T>>>;

// #define DEBUG

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  // Failed to predict input format
  int N;
  cin >> N;

  vec<ll> A;
  for (int i = 0; i < N; i++) {
    ll temp;
    cin >> temp;
    A.push_back(temp);
  }

  // 1ずれに注意
  // multiset<ll> values;
  map<ll, ll> values;
  values.insert(pair<ll, ll>(-A.at(0) - 1, 1));
  // values.insert(-A.at(0) - 1);
  ll res = 0;
  for (int i = 1; i < N; i++) {
    if (values.find(A.at(i) - (i + 1)) == values.end()) {
    } else {
      res += values.at(A.at(i) - (i + 1));
    }
    // res += values.count(A.at(i) - (i+1));;
    // values.insert(-A.at(i) - (i+1));
    if (values.find(-A.at(i) - (i + 1)) == values.end()) {
      values.insert(pair<ll, ll>(-A.at(i) - (i + 1), 1));
    } else {
      values.at(-A.at(i) - (i + 1))++;
    }

#ifdef DEBUG
    printf("*START: hoge\\n");
    for (auto iter : values) {
      cout << iter.first << " " << iter.second << endl;
    }
    cout << "*" << res << endl;
    printf("*END: hoge\\n");
#endif
  }
  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long
    ll; // -9,223,372,036,854,775,808 から 9,223,372,036,854,775,807

template <class T> using vec = std::vector<T>;
template <class T> using vec2 = vec<vec<T>>;
template <class T> using vec3 = vec<vec<vec<T>>>;

#define DEBUG

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  // Failed to predict input format
  int N;
  cin >> N;

  vec<ll> A;
  for (int i = 0; i < N; i++) {
    ll temp;
    cin >> temp;
    A.push_back(temp);
  }

  // 1ずれに注意
  multiset<ll> values;
  values.insert(-A.at(0) - 1);
  ll res = 0;
  for (int i = 1; i < N; i++) {
    res += values.count(A.at(i) - (i + 1));
    values.insert(-A.at(i) - (i + 1));
  }
  cout << res << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

int main() {
  int n;
  cin >> n;
  vector<long long> a(n, 0);
  rep(i, n) cin >> a[i];
  map<int, int> m;
  ll cnt = 0;
  rep(i, n) {
    int sa = i - a[i];
    cnt += m[sa];
    int wa = a[i] + i;
    m[wa]++;
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

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

int main() {
  int n;
  cin >> n;
  vector<long long> a(n, 0);
  rep(i, n) { cin >> a[i]; }
  long long cnt = 0;
  for (long long i = 0; i < n; i++) {
    for (long long j = a[i] + 1; j < n; j++) {
      if (j - i == a[i] + a[j]) {
        cnt++;
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 10;

const int MAX = 1e9 + 10;
vector<int> g[N];
int vis[N];
int ans;
typedef long long ll;
int a[N];
map<int, int> mp;
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += mp[i - a[i]];
    mp[a[i] + i]++;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  long int a[200005];
  int i;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int j, k, count = 0;
  for (j = 1; j <= n; j++) {
    for (k = j + 1; k < n + 1; k++) {
      if (k - j == a[j] + a[k]) {
        count++;
      }
    }
  }
  cout << count << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 1; i <= n; i++)

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  rep(i, n) cin >> a[i];
  map<ll, ll> dic;

  rep(i, n) dic[i - a[i]] += 1;
  ll ans = 0;
  rep(i, n) { ans += dic[i + a[i]]; }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int count = 0;
  for (int i = 1; i <= n - 1; i++) {
    for (int j = i + 2; j <= n; j++) {
      if (j - i == a[i] + a[j])
        count++;
    }
  }
  cout << count << endl;

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

#include <bits/stdc++.h>
using namespace std;
map<int, int> ct;
map<int, int> ct2;

int main() {
  int n;
  scanf("%d", &n);
  long long ans = 0;
  for (int i = 1, x; i <= n; i++) {
    scanf("%d", &x);
    //        ans += ct[i - x];
    //        ct[i + x]++;
    ct[i - x]++;
    ct2[i + x]++;
  }
  for (int i = 1; i <= 200000; i++) {
    ans += (long long)ct[i] * (long long)ct2[i];
  }
  printf("%lld", ans);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
map<int, int> ct;
map<int, int> ct2;

int main() {
  int n;
  scanf("%d", &n);
  long long ans = 0;
  for (int i = 1, x; i <= n; i++) {
    scanf("%d", &x);
    //        ans += ct[i - x];
    //        ct[i + x]++;
    ct[i - x]++;
    ct2[i + x]++;
  }
  for (int i = 1; i <= 1000200000; i++) {
    ans += ct[i] * ct2[i];
  }
  printf("%lld", ans);
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>

using namespace std;

long long a[200005];
long long a_plus_i[200005];

int main() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] + i <= n && a[i] + i >= 1) {
      a_plus_i[a[i] + i] += 1;
    }
  }
  long long pairs = 0;
  for (long long j = 1; j <= n; j++) {
    if (j - a[j] >= 1 && j - a[j] <= n) {
      pairs += a_plus_i[j - a[j]];
    }
  }
  cout << pairs << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

long long a[100005];
long long a_plus_i[100005];

int main() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] + i <= n && a[i] + i >= 1) {
      a_plus_i[a[i] + i] += 1;
    }
  }
  long long pairs = 0;
  for (long long j = 1; j <= n; j++) {
    if (j - a[j] >= 1 && j - a[j] <= n) {
      pairs += a_plus_i[j - a[j]];
    }
  }
  cout << pairs << "\\n";
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  vector<ll> list_a;
  unordered_map<ll, int> candidates;

  rep(i, n) {
    int a;
    cin >> a;
    list_a.push_back(a + i + 1);
    candidates[i + 1 - a]++;
  }

  ll count = 0;

  rep(i, n) { count += candidates[list_a[i]]; }

  cout << count << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  vector<int> attendees(n + 1);
  set<int> heights;

  rep(i, n) {
    int a;
    cin >> a;
    attendees[i + 1] = a;
    heights.insert(a);
  }

  int count = 0;

  for (int i = 1; i < n; i++) {
    for (auto height : heights) {
      ll sum = attendees[i] + height;
      if (sum > max(i, n - i))
        break;

      ll candidates[] = {i + sum, i - sum};

      for (auto candidate : candidates) {
        if (candidate > i && candidate <= n) {
          if (attendees[candidate] == height)
            count++;
        }
      }
    }
  }

  cout << count << endl;

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

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

long long n, ans = 0;
map<long long, int> m;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    long long a;
    cin >> a;
    ans += m[i - a];
    m[i + a]++;
  }
  cout << ans << "\\n";
}

Error: TLE

Fixed submission:

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

long long n, ans = 0;
multiset<long long> m;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    long long a;
    cin >> a;
    ans += m.count(i - a);
    m.insert(i + a);
  }
  cout << ans << "\\n";
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define BUG puts("no bug")
#define sci(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define ll long long
#define ull unsigned long long
#define REP(a, b, c) for (ll a = b; a <= c; a++)
#define PER(a, b, c) for (ll a = b; a >= c; a--)
#define lb(a) (a & (-a))
#define pp (p << 1)
#define ppp (p << 1 | 1)
#define pi pair<ll, ll>
#define pii pair<pair<ll, ll>, ll>
using namespace std;
const ll MAXN = 1e6 + 5;
ll n, m, t;
map<ll, ll> v;
int main() {
  cin >> n;
  ll ans = 0;
  REP(i, 1, n) {
    ll tmp;
    cin >> tmp;
    ans += v[i - tmp];
    v[(i + tmp)]++;
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define BUG puts("no bug")
#define sci(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define ll long long
#define ull unsigned long long
#define REP(a, b, c) for (ll a = b; a <= c; a++)
#define PER(a, b, c) for (ll a = b; a >= c; a--)
#define lb(a) (a & (-a))
#define pp (p << 1)
#define ppp (p << 1 | 1)
#define pi pair<ll, ll>
#define pii pair<pair<ll, ll>, ll>
using namespace std;
const ll MAXN = 1e6 + 5;
ll n, m, t;
ll v[MAXN];
int main() {
  cin >> n;
  ll ans = 0;
  REP(i, 1, n) {
    ll tmp;
    cin >> tmp;
    if (i - tmp >= 0)
      ans += v[i - tmp];
    v[(i + tmp)]++;
  }
  cout << ans;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  cin >> n;
  ll an[n];
  rep(i, n) cin >> an[i];
  ll pn[n];
  rep(i, n) pn[i] = i - an[i] + 1;
  ll qn[n];
  rep(i, n) qn[i] = i + an[i] + 1;
  ll pdp[n];
  rep(i, n) pdp[i] = 0;
  rep(i, n) if (pn[i] >= 0) { pdp[pn[i]]++; }
  ll cnt = 0;
  rep(i, n) if (qn[i] < n) { cnt += pdp[qn[i]]; }
  cout << cnt;
}


Error: WA

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  ll an[n];
  rep(i, n) cin >> an[i];
  ll pn[n];
  rep(i, n) pn[i] = i - an[i] + 1;
  ll qn[n];
  rep(i, n) qn[i] = i + an[i] + 1;
  ll pdp[n];
  rep(i, n) pdp[i] = 0;
  rep(i, n) { pdp[pn[i]]++; }
  ll cnt = 0;
  rep(i, n) if (qn[i] < n) { cnt += pdp[qn[i]]; }
  cout << cnt;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, n) cin >> a[i];

  map<ll, ll> m1, m2;
  rep(i, n) {
    m1[i + a[i]]++;
    m2[i - a[i]]++;
  }

  ll ans = 0;
  rep(i, n) ans += m1[i] * m2[i];

  cout << ans << endl;
}

Error: WA

Fixed submission:

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

int main() {
  int n, a[n];
  cin >> n;
  rep(i, n) cin >> a[i];

  map<ll, ll> m1, m2;
  rep(i, n) {
    m1[i + a[i]]++;
    m2[i - a[i]]++;
  }

  ll ans = 0;
  rep(i, n) ans += m1[i] * m2[i];

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

#include <bits/stdc++.h>
using namespace std;
#define SORT(a) sort((a).begin(), (a).end())
#define RSORT(a) reverse((a).begin(), (a).end())
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
const long long INF = 1LL << 60;
// const int INF=1010101010;
using Graph = vector<vector<int>>;

int main() {
  ll n;
  cin >> n;
  ll ma = 200010;
  vector<ll> t(ma);
  vector<ll> b(ma);
  vector<bool> k(ma);
  vector<ll> p;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a + i <= ma) {
      b[a + i]++;
      if (k[a + i] == false) {
        p.push_back(a + i);
        k[a + i] = true;
      }
    }

    if (i - a >= 0) {
      t[i - a]++;
      if (k[i - a] == false) {
        p.push_back(i - a);
        k[i - a] = true;
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < p.size(); i++) {
    ans += t[p[i]] * b[p[i]];
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define SORT(a) sort((a).begin(), (a).end())
#define RSORT(a) reverse((a).begin(), (a).end())
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
const long long INF = 1LL << 60;
// const int INF=1010101010;
using Graph = vector<vector<int>>;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  int cnt = 0;
  for (ll i = 0; i < n; i++) {
    ll k = a[i];
    if (k >= n)
      continue;
    for (ll j = k; j < n; j++) {
      if (a[j] + a[i] == j - i)
        cnt++;
    }
  }
  cout << cnt << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (long long i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 0, n + 1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
const int MOD = 1e9 + 7;
const int INF = 1e18;

signed main() {
  int N;
  cin >> N;
  map<int, int> Amemo;
  map<int, int> Bmemo;
  REP(i, N) {
    int a;
    cin >> a;
    Amemo[a + i]++;
    Bmemo[a - i]++;
  }
  int ans = 0;
  for (auto v : Amemo) {
    int pls = Amemo[v.first] * Bmemo[-v.first];
    ans += pls;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define pi 3.14159265359
#define all(i) i.begin(), i.end()
using namespace std;
const long long INF = 1e9 + 7;
const string alp = "abcdefghijklmnopqrstuvwxyz"; // 26
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
// const ll MOD=998244353LL;
// const ll MOD=1777777777LL;

int main() {
  ll N;
  cin >> N;
  ll a[N];
  rep(i, N) { cin >> a[i]; }
  ll ans = 0;
  ll mab = 0, mac = 0;
  ll b[N];
  ll c[N];
  rep(i, N) {
    b[i] = i + 1 + a[i];
    mab = max(mab, b[i]);
    c[i] = i + 1 - a[i];
    mac = max(mac, c[i]);
  }
  vl countb(mab + 10, 0);
  rep(i, N) { countb[b[i]]++; }
  vl countc(mac + 10, 0);
  rep(i, N) {
    if (c[i] >= 2) {
      countc[c[i]]++;
    }
  }
  rep(i, mac + 1) { ans += (countb[i] * countc[i]); }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1e9

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

  map<int, int> mp;

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

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <math.h>
#include <vector>
using namespace std;
typedef long long int lli;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  lli n, k, d, dk, i, j, x, z, cnt = 0;
  cin >> n;
  lli ara[n + 7];
  for (i = 1; i <= n; i++)
    cin >> ara[i];
  for (i = 1; i < n; i++) {
    for (j = i + 1; j <= n; j++) {
      if (abs(i - j) == (ara[i] + ara[j]))
        cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[200010], cnt[200010];
signed main() {
  int n;
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) {
    if (a[i] + i <= n)
      cnt[a[i] + i]++;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    if (i - a[i] >= 0)
      ans += cnt[i - a[i]];
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[200010], cnt[200010];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    if (a[i] + i <= n)
      cnt[a[i] + i]++;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    ans += cnt[i - a[i]];
  printf("%lld\\n", ans);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll a[1000007];
int main() {
  ll i, j, k, l, test, n;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i];

  map<ll, ll> m;
  for (i = 1; i <= n; i++) {
    m[(a[i] - i)]++;
  }
  ll sum = 0;
  for (i = 1; i <= n; i++) {
    sum += m[-(a[i] + i)];
  }
  cout << sum << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll a[1000007];
int main() {
  ll i, j, k, l, test, n, m;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  ll c = 0;
  for (i = 1; i <= n; i++) {
    if (a[i] < n) {
      for (j = i + 1; j <= n; j++) {
        if (abs(i - j) == a[i] + a[j])
          c++;
      }
    }
  }
  cout << c;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define div1 1000000007
const long long INF = 1LL << 60;
using namespace std;
typedef long long ll;
typedef long double ld;
// a>b -> a==b;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// a<b -> a==b;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  ll N, i, j, A[200001], ans = 0;
  map<ll, ll> mp;

  cin >> N;

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

    ans += mp[i - A[i]];

    mp[i + A[i]]++;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define div1 1000000007
const long long INF = 1LL << 60;
using namespace std;
typedef long long ll;
typedef long double ld;
// a>b -> a==b;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// a<b -> a==b;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  ll N, i, j, A[200001], ans = 0;

  cin >> N;

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

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

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> inline bool chmin(T &x, U y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmax(T &x, U y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
#define fr(i, n) for (int i = 0; i < (n); ++i)
#define Fr(i, n) for (int i = 1; i <= (n); ++i)
#define ifr(i, n) for (int i = (n)-1; i >= 0; --i)
#define iFr(i, n) for (int i = (n); i > 0; --i)

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  unordered_map<int, int> mp;
  ll ans{};
  ifr(i, n) {
    ans += mp[a[i] + i];
    mp[i - a[i]]++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>     //sort,二分探索,など
#include <bitset>        //固定長bit集合
#include <cmath>         //pow,logなど
#include <complex>       //複素数
#include <deque>         //両端アクセスのキュー
#include <functional>    //sortのgreater
#include <iomanip>       //setprecision(浮動小数点の出力の誤差)
#include <iostream>      //入出力
#include <iterator>      //集合演算(積集合,和集合,差集合など)
#include <map>           //map(辞書)
#include <numeric>       //iota(整数列の生成),gcdとlcm(c++17)
#include <queue>         //キュー
#include <set>           //集合
#include <stack>         //スタック
#include <string>        //文字列
#include <unordered_map> //イテレータあるけど順序保持しないmap
#include <unordered_set> //イテレータあるけど順序保持しないset
#include <utility>       //pair
#include <vector>        //可変長配列

using namespace std;
typedef long long ll;

// マクロ
// forループ関係
// 引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
// Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// xにはvectorなどのコンテナ
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())      // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))   // 最大値を求める
#define MIN(x) *min_element(ALL(x))   // 最小値を求める
// 定数
#define INF 1000000000000 // 10^12:極めて大きい値,∞
#define MOD 10000007      // 10^9+7:合同式の法
#define MAXR 100000 // 10^5:配列の最大のrange(素数列挙などで使用)
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

signed main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> B(N);
  vector<int> C(N);
  REP(i, N) { cin >> A[i]; }

  // int a, b;
  int ans = 0;

  REP(i, N) {
    B[i] = A[i] - i;
    C[i] = A[i] + i;
  }

  // a[i]-i=-b[j]-j
  // a[i]+i=-b[j]+j

  REP(i, N) {
    ans += count((C).begin() + i, (C).end(), -B[i]);
    ans += count((B).begin() + i, (B).end(), -C[i]);
  }

  cout << ans << endl;
}