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

#include "bits/stdc++.h"

using namespace std;

struct Edge {
  int to;     // 辺の行き先
  int weight; // 辺の重み
  Edge(int t, int w) : to(t), weight(w) {}
};

// using Graph = vector<vector<Edge>>
using Graph = vector<vector<int>>;

// header area

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;

  long long ans = 0;
  for (int i = N - 1; i >= 0; --i) {
    ans += mp[A[i] + i];
    // j>iなのでそいつよりも大きい添え字の情報だけ保持してればよい
    mp[i - A[i]]++;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

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

using namespace std;

struct Edge {
  int to;     // 辺の行き先
  int weight; // 辺の重み
  Edge(int t, int w) : to(t), weight(w) {}
};

// using Graph = vector<vector<Edge>>
using Graph = vector<vector<int>>;

// header area

int main() {
  int N;
  cin >> N;
  vector<vector<int>> A;

  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (a <= N - 2) {
      vector<int> tmp = {i, a};
      A.push_back(tmp);
    }
  }

  sort(A.begin(), A.end(),
       [](const vector<int> &alpha, const vector<int> &beta) {
         return alpha[1] < beta[1];
       });

  /*for (int i = 0; i < A.size(); i++) {
      cout << i << ":";
      cout << A[i][0] << "," << A[i][1] << endl;
  }*/

  int ans = 0;
  int sum = 0;
  for (int i = 0; i < A.size(); i++) {
    sum = 0;
    for (int j = i + 1; j < A.size(); j++) {
      sum = A[i][1] + A[j][1];
      // cout <<sum << "," << abs(A[j][0] - A[i][0]) << endl;
      if (sum > N - 1)
        break;
      if (sum == (abs(A[j][0] - A[i][0])))
        ans++;
    }
  }
  cout << ans;
}
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<ll, ll>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  vector<ll> a(n);
  map<ll, ll> mp1;
  map<ll, ll> mp2;
  rep(i, n) {
    cin >> a[i];
    a[i];
    ll a1 = a[i];
    mp1[a1 + i]++;
    mp2[i - a1]++;
  }
  ll ans = 0;
  for (auto i = mp1.begin(); i != mp1.end(); ++i) {
    ll now1 = i->first;
    ll cnt2 = mp2[now1];
    ll cnt1 = i->second;
    //  cout<<cnt1<<' '<<now1<<' '<<cnt2<<endl;
    ans += (cnt1 * cnt2);
  }
  cout << ans;
}


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<ll, ll>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i];
  }
  ll ans = 0;
  rep(i, n) {
    ll temp = a[i];
    if ((a[i] + i) >= n)
      continue;
    for (ll j = (a[i] + i); j < n; j++) {
      if ((a[j] + a[i]) == j - i) {
        ans++;
        //  cout<<a[i]<<'i'<<a[j]<<'j'<<endl;
        // cout<<(a[j]+a[i])<<' '<<i<<'i'<<j<<'j'<<endl;
      }
    }
  }
  cout << ans;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INCANT                                                                 \\
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(0),                            \\
      cout << fixed << setprecision(20);
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()
#define PI 3.14159265358979
typedef long long ll;
using namespace std;
const ll MOD = 1e9 + 7LL;
const int INF = 2e9;
int N, A[200005];
unordered_map<int, ll> M;

// i<jのときj-i=A[i]+A[j]
// A[i]+i=j-A[j]
int main() {
  INCANT;
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    M[A[i] + i]++;
  }
  ll res = 0;
  rep(i, N) res += M[i - A[i]];

  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INCANT                                                                 \\
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(0),                            \\
      cout << fixed << setprecision(20);
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()
#define PI 3.14159265358979
typedef long long ll;
using namespace std;
const ll MOD = 1e9 + 7LL;
const int INF = 2e9;
int N, A[100005];
unordered_map<int, ll> M;

// i<jのときj-i=A[i]+A[j]
// A[i]+i=j-A[j]
int main() {
  INCANT;
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    M[A[i] + i]++;
  }
  ll res = 0;
  rep(i, N) res += M[i - A[i]];

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

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

int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> A(N);
  for (int64_t i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  map<int64_t, int64_t> tmp;
  int64_t ans = 0;
  for (int64_t i = 0; i < N; i++) {
    ans += tmp[i - A.at(i)];
    tmp[i + A.at(i)]++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  int ans = 0;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  for (int i = 0; i < N; i++) {
    for (int j = i; j < N; j++) {
      if (j - i == A.at(i) + A.at(j)) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
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++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;
typedef pair<Int, Int> p;

int main() {
  int N;
  cin >> N;
  vector<Int> A(N), B(N);
  rep(i, N) {
    cin >> A[i];
    B[i] = A[i] - i;
  }

  map<Int, int> cnt;
  rep1(i, N - 1) { cnt[B[i]]++; }

  Int ans = 0;
  rep(i, N) ans += cnt[-A[i] - 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++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;
typedef pair<Int, Int> p;

int main() {
  int N;
  cin >> N;
  vector<Int> A(N), B(N);
  rep(i, N) {
    cin >> A[i];
    B[i] = A[i] - i;
  }

  vector<map<int, int>> cnt(N);
  cnt[0][B[0]]++;
  rep1(i, N - 1) {
    cnt[i] = cnt[i - 1];
    cnt[i][B[i]]++;
  }

  Int ans = 0;
  rep(i, N) {
    Int left = i + A[i];
    if (left >= N)
      continue;
    // cout << left << endl;

    ans += cnt[N - 1][-left] - cnt[i + A[i]][-left];
  }

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

#include <bits/stdc++.h>
#define rep(begin, end) for (i = begin; i < end; i++)
#define repj(begin, end) for (j = begin; j < end; j++)
#define init(arr, end, val)                                                    \\
  for (i = 0; i < end; i++)                                                    \\
    arr[i] = val;
#define printint(i0, i1) printf("%d %d\\n", i0, i1)

using namespace std;

typedef long long int ll;
const ll inf = 1000000000;
const ll mod = 1000000007;
const ll nil = -1;
ll i, n, m, k, ans, a;
vector<pair<ll, ll>> iminusa;
ll iplusa[200000];

bool compare_by_b(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

int main() {
  scanf(" %lld", &n);
  rep(0, n) {
    scanf(" %lld", &a);
    iminusa.push_back(make_pair(i, i - a));
    iplusa[i] = i + a;
  }
  sort(iminusa.begin(), iminusa.end(), compare_by_b);
  ans = 0;
  rep(0, n) {
    const ll sum = iplusa[i];
    auto lb = lower_bound(iminusa.begin(), iminusa.end(), make_pair(nil, sum),
                          compare_by_b);
    auto ub = upper_bound(iminusa.begin(), iminusa.end(), make_pair(inf, sum),
                          compare_by_b);
    ans += ub - lb;
  }

  printf("%lld\\n", ans);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(begin, end) for (i = begin; i < end; i++)
#define repj(begin, end) for (j = begin; j < end; j++)
#define init(arr, end, val)                                                    \\
  for (i = 0; i < end; i++)                                                    \\
    arr[i] = val;
#define printint(i0, i1) printf("%d %d\\n", i0, i1)

using namespace std;

typedef long long int ll;
const ll inf = 1000000000;
const ll mod = 1000000007;
const ll nil = -1;
ll i, n, m, k, ans, a;
vector<pair<ll, ll>> iminusa;
ll iplusa[200000];

bool compare_by_b(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

int main() {
  scanf(" %lld", &n);
  rep(0, n) {
    scanf(" %lld", &a);
    iminusa.push_back(make_pair(i, i - a));
    iplusa[i] = i + a;
  }
  sort(iminusa.begin(), iminusa.end(), compare_by_b);
  ans = 0;
  rep(0, n) {
    const ll sum = iplusa[i];
    auto lb = lower_bound(iminusa.begin(), iminusa.end(), make_pair(nil, sum),
                          compare_by_b);
    auto ub = upper_bound(iminusa.begin(), iminusa.end(), make_pair(inf, sum),
                          compare_by_b);
    for (auto it = lb; it != ub; it++) {
      if (it->first > i)
        ans++;
    }
  }

  printf("%lld\\n", ans);
}
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;

void solve(long N, vector<long> A) {
  ll ans = 0;
  unordered_map<long, long> i_pl;
  unordered_map<long, long> i_mi;
  rep(i, N) {
    i_pl[i + A[i]]++;
    i_mi[i - A[i]]++;
  }
  for (auto &pair : i_pl) {
    long x = pair.first;
    if (!i_mi[x])
      continue;
    ans += i_pl[x] * i_mi[x];
  }

  cout << ans << endl;
}

int main() {
  // int 3E4  long 2E9  ll 9E18
  cin.tie(0);
  ios::sync_with_stdio(false);

  long N;
  cin >> N;

  vector<long> A(N);
  rep(i, N) cin >> A[i];

  solve(N, move(A));

  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;

void solve(long N, vector<long> A) {
  ll ans = 0;
  rep(j, N) {
    rep(i, N) {
      if (A[i] + A[j] == i - j)
        ans++;
    }
  }
  cout << ans << endl;
}

int main() {
  // int 3E4  long 2E9  ll 9E18
  cin.tie(0);
  ios::sync_with_stdio(false);

  long N;
  cin >> N;

  vector<long> A(N);
  rep(i, N) cin >> A[i];

  solve(N, move(A));

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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define int long long
#define P pair<int, int>
#define all(vec) vec.begin(), vec.end()

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
int kai(int x, int y) {
  int res = 1;
  for (int i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= mod;
  }
  return res;
}
int mod_pow(int x, int y, int m) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}

int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}

int n;
int cnt[200010];
int ans;

signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (0 <= i - a + 1)
      ans += cnt[i - a + 1];
    cnt[min(n, i + a + 1)]++;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define int long long
#define P pair<int, int>
#define all(vec) vec.begin(), vec.end()

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
int kai(int x, int y) {
  int res = 1;
  for (int i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= mod;
  }
  return res;
}
int mod_pow(int x, int y, int m) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}

int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}

int n;
int cnt[100010];
int ans;

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef unsigned long ul;
typedef long long ll;
typedef pair<ul, ul> P;
map<int, int> dp;
#define REP(i, n) for (int i = 0; i < n; i++)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ul N;
  cin >> N;
  vector<ul> A(N, 0);
  map<int, int> mp;
  ul cnt = 0;
  REP(ii, N) { cin >> A[ii]; }
  REP(ii, N) {
    cnt += mp[ii + 1 - A[ii]];
    mp[A[ii] + ii + 1]++;
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef unsigned long ul;
typedef long long ll;
typedef pair<ul, ul> P;
map<int, int> dp;
#define REP(i, n) for (int i = 0; i < n; i++)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ul N;
  cin >> N;
  vector<ul> A(N, 0);
  ul cnt = 0;
  REP(ii, N) cin >> A[ii];
  REP(ii, N) {
    for (ul jj = 0; jj < ii; ++jj) {
      auto diff = ii - jj;
      auto sum = A[ii] + A[jj];
      if (sum == diff)
        ++cnt;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

/*
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long ll;

#define fi first
#define se second

using Graph = vector<vector<int>>;

void dfs(vector<vector<int>> &ab, vector<bool> &visited, int index, int n) {
  for (auto v : ab[index]) {
    if (visited[v] == false) {
      visited[v] = true;
      dfs(ab, visited, v, n);
    }
  }
}

int main() {
  int n;
  cin >> n;
  map<ll, ll> m1;
  map<ll, ll> m2;
  for (ll i = 0; i < n; i++) {
    ll a;
    cin >> a;
    if (m1.count(i + 1 + a) == 0) {
      m1[i + 1 + a] = 1;
    } else {
      m1[i + 1 + a] += 1;
    }
    if (m2.count(i + 1 - a) == 0) {
      m2[i + 1 - a] = 1;
    } else {
      m2[i + 1 - a] += 1;
    }
  }
  ll ans = 0;
  for (auto itr = m1.begin(); itr != m1.end(); itr++) {
    ans += (itr->second) * m2[itr->first];
  }
  cout << ans << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

/*
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long ll;

#define fi first
#define se second

using Graph = vector<vector<int>>;

int main() {
  int n;
  cin >> n;
  vector<vector<int>> a(200001, vector<int>());

  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x <= 200000) {
      a[x].push_back(i);
    }
  }

  ll ans = 0;

  for (int i = 0; i < a.size(); i++) {
    sort(a[i].begin(), a[i].end());
  }

  for (int i = 0; i < a.size(); i++) {
    for (auto j : a[i]) {
      for (int k = i + 1; k < a.size(); k++) {
        auto b = lower_bound(a[k].begin(), a[k].end(), i + k - j);
        if (*b == i + k - j) {
          ans++;
        }
      }
    }
  }
  cout << ans << endl;

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

#include <iostream>
#include <map>
#include <set>
#include <vector>

int main(void) {
  int n;
  std::cin >> n;
  std::vector<int> a(n);
  std::multiset<int> m;
  std::map<int, int> map;
  int key;
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
    key = i + a[i];
    if (map.find(key) == map.end()) {
      map[key] = 1;
    } else {
      map[key]++;
    }
  }
  long long ans = 0;
  for (int i = n - 1; i >= 1; i--) {
    key = i - a[i];
    if (map.find(key) != map.end() && map[key] != 0) {
      int key2 = i + a[i];
      map[key2] -= 1;
      ans += map[key];
    }
  }
  std::cout << ans << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <vector>

int main(void) {
  int n;
  std::cin >> n;
  std::vector<int> a(n);
  std::multiset<int> m;
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
    m.insert(i + a[i]);
  }
  int ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (m.find(i - a[i]) != m.end()) {
      ans += m.count(i - a[i]);
      m.erase(i + a[i]);
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll Max = 200005;
map<ll, ll> m;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, a[Max], adun[Max], scad[Max];
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    adun[i] = i + a[i];
    scad[i] = i - a[i];
    m[adun[i]]++;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += m[scad[i]];
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, a[Max], adun[Max], scad[Max];
  multiset<ll> s;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    adun[i] = i + a[i];
    scad[i] = i - a[i];
    s.insert(adun[i]);
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    if (s.find(scad[i]) != s.end())
      ans += s.count(scad[i]);
  }
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Map = map<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
ll INF = 1LL << 60;
ll MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N, 0);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<P> C(0);
  vector<P> D(0);
  for (ll i = 0; i < N; i++) {
    C.emplace_back(P(i + A[i], i));
    if (i > 0)
      D.emplace_back(P(i - A[i], i));
    ;
  }

  // 自分より番号大きいものだけ見ろ

  ll ans = 0;

  sort(C.begin(), C.end());
  sort(D.begin(), D.end());

  for (ll i = 0; i < N - 1; i++) {
    ll x = C[i].first;
    ll y = C[i].second; // yより大きい番号しか残すな
    ll s = lower_bound(D.begin(), D.end(), P(x, 0)) - D.begin();
    ll t = upper_bound(D.begin(), D.end(), P(x, INF)) -
           D.begin(); // ｓからｔ-1番目までをみる
    ans += t - s;
    // D.erase(lower_bound(D.begin(), D.end(), P(i+1-A[i+1],i+1)))
    ;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Map = map<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
ll INF = 1LL << 60;
ll MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N, 0);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<P> C(0);
  vector<P> D(0);
  for (ll i = 0; i < N; i++) {
    C.emplace_back(P(i + A[i], i));
    if (i > 0)
      D.emplace_back(P(i - A[i], i));
    ;
  }

  // 自分より番号大きいものだけ見ろ

  ll ans = 0;

  sort(C.begin(), C.end());
  sort(D.begin(), D.end());

  for (ll i = 0; i < N - 1; i++) {
    ll x = C[i].first;
    ll y = C[i].second; // yより大きい番号しか残すな
    ll s = lower_bound(D.begin(), D.end(), P(x, 0)) - D.begin();
    ll t = upper_bound(D.begin(), D.end(), P(x, INF)) -
           D.begin(); // ｓからｔ-1番目までをみる
    ans += t - s;
    D.erase(lower_bound(D.begin(), D.end(), P(i + 1 - A[i + 1], i + 1)));
  }
  cout << ans << endl;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <stdint.h>
#include <utility>

using namespace std;

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

  map<int, int> P, M;

  for (int n = 1; n <= N; ++n) {
    int a;
    cin >> a;

    auto itrP = P.find(n + a);
    if (itrP == P.end())
      P[n + a] = 1;
    else
      ++P[n + a];

    auto itrM = M.find(n - a);
    if (itrM == M.end())
      M[n - a] = 1;
    else
      ++M[n - a];
  }

  int64_t ans = 0;

  for (auto p : P) {
    auto m = M.find(p.first);

    if (m == M.end())
      continue;

    ans += (int64_t)p.second * m->second;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stdint.h>
#include <utility>
#include <vector>

using namespace std;

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

  vector<int> A(N, 0);

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

  int ans = 0;

  for (int a = 0; a < N - 1; ++a) {
    for (int b = a + A[a]; b < N; ++b) {
      int diff = b - a;
      int sum = A[a] + A[b];

      if (diff == sum)
        ++ans;
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define DAU                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define PLEC return 0;
using namespace std;
unordered_map<int, int> freq;
long long res;
int n, x;
int main() {
  DAU cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    res += freq[i - x];
    ++freq[x + i];
  }
  cout << res;
  PLEC
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define DAU                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define PLEC return 0;
using namespace std;
int freq[400004], n, x, caut;
long long res;
int main() {
  DAU cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    caut = i - x;
    if (caut >= 0)
      res += freq[caut];
    ++freq[x + i];
  }
  cout << res;
  PLEC
}