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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
ll a[maxn];
map<ll, int> vis;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  int num;
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> num;
    vis[num + i - 1]++;
    int now = vis[i - 1 - num];
    // cout<<"数字"<<num<<" "<<i-1-num<<" "<<now<<endl;
    sum += now;
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
ll a[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  ll cnt = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] >= 2e5)
      continue;
    if (a[i] + 1 >= i)
      continue;
    for (int j = i - 1; j >= 1; j--) {
      if (a[i] + a[j] == i - j)
        cnt++;
    }
  }
  cout << cnt << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long int n, ans = 0;
  cin >> n;
  long long int a[n], diff;
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  map<long long int, vector<long long int>> mp;

  for (int i = 0; i < n; ++i) {
    diff = a[i] - i;
    mp[diff].push_back(i);
  }
  auto begin = mp.begin(), end = mp.end();
  for (auto iter = begin; iter != end; iter++) {
    sort(iter->second.begin(), iter->second.end());
  }
  for (int i = 0; i < n; ++i) {
    long long int I = i + a[i];
    if (!mp[-I].empty()) {
      ans +=
          distance(lower_bound(mp[-I].begin(), mp[-I].end(), I), mp[-I].end());
    }
  }
  cout << ans << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n, ans = 0;
  cin >> n;
  long long int a[n], diff;
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  map<int, vector<int>> mp;

  for (int i = 0; i < n; ++i) {
    diff = a[i] - i;
    mp[diff].push_back(i);
  }
  for (int i = 0; i < n; ++i) {
    long long int I = i + a[i];
    if (!mp[-I].empty()) {
      for (int j = 0; j < mp[-I].size(); ++j) {
        if (mp[-I][j] >= I)
          ans++;
      }
    }
  }
  cout << ans << '\\n';

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

#include <bits/stdc++.h>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

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

  map<s64, u64> A1;
  map<s64, u64> A2;

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

  u64 ans = 0;
  for (const auto &a : A1) {
    if (A2.count(a.first)) {
      ans += A2[a.first] * a.second;
    }
  }

  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

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

  vector<u64> A(N);
  for (s64 i = 0; i < N; i++)
    cin >> A[i];

  u64 ans = 0;
  for (s64 i = 0; i < N; i++) {
    for (s64 j = i + A[i]; j < N; j++) {
      if (j - i == A[i] + A[j]) {
        ans++;
      }
    }
  }

  cout << ans;

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

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

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

Error: TLE

Fixed submission:

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

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

#include <iostream>
#include <map>
#define ll long long

using namespace std;

map<ll, int> mp;

int main() {
  ll n, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    ans += mp[i - x];
    mp[i + x]++;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

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

#include <algorithm>
#include <iostream>
#define ll long long
using namespace std;
int a[200005], b[200005];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (i + a[i] <= n)
      b[a[i] + i]++;
    if (i - a[i] >= 0)
      ans += b[i - a[i]];
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#define endl '\\n'
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;

#define int long long

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  long long int ans = 0;
  cin >> n;
  long long int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  map<long long int, long long int> m1, m2;

  for (int i = 0; i < n; i++) {
    m1[i + a[i]]++;
    m2[i - a[i]]++;
  }

  for (auto it : m1) {
    ans += (it.second) * (m2[it.first]);
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, pos, flag = 0;
  cin >> n;
  int a[n], b[n], c[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    b[i] = i + a[i];
    c[i] = i - a[i];
    if (c[i] > 0 && flag == 0) {
      pos = i;
      flag = 1;
      // cout<<pos;
    }
  }
  int count = 0;
  for (int i = 0; i < n; i++) {
    for (int j = max(i + 1, pos); j < n; j++) {
      if (b[i] == c[j]) {
        count++;
        // cout<<"i"<<i+1<<"j"<<j+1<<endl;
      }
    }
  }
  cout << count;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5, M = 1e9 + 7, OO = 0x3f3f3f3f;
typedef long long ll;
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const double Pii = 3.14159265359;
ll n;
ll a[N], d[N], fr[N];

int main() {
  FIO cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    d[i] = i - a[i];
  }
  for (int i = 0; i < n; ++i)
    if (d[i] < N && d[i] >= 0)
      fr[d[i]]++;
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int didx = a[i] + i;
    if (didx >= 0 && didx < N)
      ans += fr[didx];
    // cout << didx << " " << fr[didx] << endl;
  }
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
long long b[20002], c[20002];
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, x, sum = 0;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> x;
    sum += b[x + i] + c[i - x];
    b[i - x]++;
    c[x + i]++;
  }
  cout << sum;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << "\\n";
}
template <typename Arg1, typename... Args>
void __f(const char *na, Arg1 &&arg1, Args &&...args) {
  const char *c = strchr(na + 1, ',');
  cerr.write(na, c - na) << " : " << arg1 << " , ";
  __f(c + 1, args...);
}
#define SZ(v) (int)v.size()
#define endl "\\n"
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v) sort(ALL(v)), v.erase(unique(ALL(v)), v.end())
typedef long long LL;
typedef pair<int, int> pii;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int inf = 1e9;
const LL INF = 1e18;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;

void solve() {
  int n;
  cin >> n;

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

  LL ans = 0;
  map<LL, int> have;

  for (int i = 1; i <= n; i++) {
    if (have.count(i - a[i]))
      ans += have[i - a[i]];
    have[i + a[i]]++;
  }

  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  int t = 1;
  for (int tc = 1; tc <= t; tc++)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a[200020], b[200020];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    a[i] = i - x;
    b[i] = i + x;
  }
  ll cnt = 0;
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) {
    int pos = lower_bound(a + 1, a + 1 + n, b[i]) - a;
    if (a[pos] != b[i])
      continue;
    for (int j = pos; j <= n; j++) {
      if (a[j] != b[i])
        break;
      else
        cnt++;
    }
  }
  cout << cnt << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

// This code was made by Chinese_zjc_.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define int long long
#define INF 0x3fffffffffffffff
using namespace std;
map<int, int> sum;
int n, a[200001], ans;
signed main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (a[i] < i) {
      ++sum[i - a[i]];
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (a[i] < i) {
      --sum[i - a[i]];
    }
    ans += sum[i + a[i]];
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

// This code was made by Chinese_zjc_.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define int long long
#define INF 0x3fffffffffffffff
using namespace std;
map<int, int> sum;
int n, a[100001], ans;
signed main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (a[i] <= i) {
      ++sum[i - a[i]];
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (a[i] <= i) {
      --sum[i - a[i]];
    }
    ans += sum[i + a[i]];
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
    Author - Param Kothari
    Ah shit, here we go again
*/

#include <bits/stdc++.h>

using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mk make_pair

typedef long long ll;
typedef long double ld;

void solve() {
  int n;
  cin >> n;
  vector<ll> a(n);
  map<ll, ll> m1, m2;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    m1[i - a[i]]++;
    m2[i + a[i]]++;
  }
  ll res = 0;
  for (auto x : m1) {
    res += (x.second * (m2[x.first]));
  }
  cout << res << "\\n";
  return;
}

int main() {
  IOS;
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

/*
    Author - Param Kothari
    Ah shit, here we go again
*/

#include <bits/stdc++.h>

using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mk make_pair
#define debug_vec(x, n)                                                        \\
  cout << (#x) << ": ";                                                        \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << x[i] << (i == n - 1 ? "\\n" : " ");

#define debug(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << *it << " = " << a << "\\n";
  err(++it, args...);
}

typedef long long ll;
typedef long double ld;

ll pow5(ll x) { return (x * x * x * x * x); }

void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = a[i] + i; j < n; j++) {
      if (a[i] + a[j] == (j - i)) {
        res++;
      }
    }
  }
  cout << res << "\\n";
  return;
}

int main() {
  IOS;
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  map<int, int> mp;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int sa = i - a[i];
    int wa = a[i] + i;
    ans += mp[sa];

    mp[wa]++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

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

#include <iostream>
#include <unordered_map>

using namespace std;
using ll = long long;
using ull = unsigned long long;

int main() {
  ull N;
  cin >> N;
  unordered_map<ll, ll> pos, neg;

  for (auto i = 1; i <= N; ++i) {
    ull Ai;
    cin >> Ai;
    ++pos[i + Ai];
    ++neg[i - Ai];
  }

  auto ans = 0ull;
  for (auto [k, v] : pos) {
    ans += v * neg[k];
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

struct T {
  ull height;
  bool good;
};

int main() {
  ull N;
  cin >> N;
  vector<ull> A;
  A.reserve(N);
  for (auto i = 0; i < N; ++i) {
    ull Ai;
    cin >> Ai;
    A.push_back(Ai);
  }
  auto ans = 0ull;
  for (auto i = 0; i < N; ++i) {
    for (auto j = i + 1; j < N; ++j) {
      if ((j - i) == A[i] + A[j])
        ++ans;
    }
  }

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

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MOD = 1e9 + 7;

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

  int n;
  cin >> n;

  vector<int> a(n + 1);
  REP(i, n) cin >> a[i];

  ll ans = 0;
  map<int, int> memo;
  REP(i, n) memo[-a[i] + i]++;
  REP(i, n) {
    memo[-a[i] + i]--;
    ans += memo[a[i] + i];
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MOD = 1e9 + 7;

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

  int n, m;
  cin >> n;

  vector<int> a(n + 1);
  rep(i, n) cin >> a[i + 1];

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

  cout << ans << endl;

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

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

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair<int, int> pii;
const int inf = 100100100;
const int mod = (int)1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  vector<vector<int>> s(2 * 10 * 10 * 10 * 10 * 10 + 1, vector<int>(2));
  rep(i, 1, n + 1) {
    cin >> a.at(i);
    // if(a.at(i)>2*10*10*10*10*10)continue;
    // else{
    // if(i+a.at(i)<2*10*10*10*10*10 or i-a.at(i)>1){
    if (i + a.at(i) < 2 * 10 * 10 * 10 * 10 * 10) {
      s.at(i + a.at(i)).at(0)++;
      // cout<<i+a.at(i);
    }
    if (i - a.at(i) > 1) {
      s.at(i - a.at(i)).at(1)++;
      // cout<<' '<<i-a.at(i)<<endl;;
    }
    //}
    //}
  }
  ll ans = 0;
  rep(i, 1, 2 * 10 * 10 * 10 * 10 * 10 + 1) {
    ans = ans + (ll)s.at(i).at(0) * s.at(i).at(1);
  }
  cout << ans << endl;

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 18446744073709551613) >= this->size() (which is 200000)


Fixed submission:

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

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair<int, int> pii;
const int inf = 100100100;
const int mod = (int)1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  vector<vector<int>> s(2 * 10 * 10 * 10 * 10 * 10, vector<int>(2));
  rep(i, 1, n + 1) {
    cin >> a.at(i);
    if (a.at(i) > 2 * 10 * 10 * 10 * 10 * 10)
      continue;
    else {
      s.at(a.at(i) + i).at(1)++;
      s.at(a.at(i) - i).at(0)++;
    }
  }
  ll ans = 0;
  rep(i, 1, 2 * 10 * 10 * 10 * 10 * 10) {
    ans = ans + s.at(i).at(0) * s.at(i).at(1);
  }
  cout << 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++)
#define rep1(i, n) for (int i = 1; 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 + 1);
  rep1(i, N) { cin >> A[i]; }
  ll ans = 0;
  map<int, int> mp;
  rep1(i, N) { mp[i + A[i]]++; }
  rep1(i, N) { ans += mp[i - A[i]]; }
  cout << ans << endl;
}

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 + 1);
  rep(i, N + 1) {
    if (i == 0) {
      continue;
    }
    cin >> A[i];
  }
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = i + 1; j <= N; j++) {
      if (abs(i - j) == A[i] + A[j]) {
        // cout << i << ',' << j << endl;
        // cout << A[i] << ',' << A[j] << endl;
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#include <map>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n, 0);
  FOR(i, 0, n) { cin >> a[i]; }

  vector<ll> J(n);
  FOR(i, 0, n) { J[i] = i + 1 - a[i]; }
  sort(J.begin(), J.end());

  map<ll, ll> mat;
  FOR(i, 0, n) {
    // cout << J[i] << ' ';
    mat[J[i]] = 0;
  }
  for (ll i : J) {
    mat[i] += 1;
    // cout << i << "->" << "+1" << endl;
  }

  ll ans = 0;

  FOR(i, 0, n) { ans += mat[a[i] + i + 1]; }
  cout << ans << endl;
}


Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#include <map>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n + 1, 0);
  FOR(i, 1, n + 1) { cin >> a[i]; }
  vector<ll> J(n + 1);
  FOR(i, 1, n + 1) { J[i] = i - a[i]; }
  J[0] = -1;
  sort(J.begin(), J.end());
  vector<ll> mat(J[n] + 1, 0);
  FOR(i, 1, n + 1) { mat[J[i]] += 1; }
  mat[0] = -1;
  ll ans = 0;

  FOR(i, 1, n + 1) { ans += mat[a[i] + i]; }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;

long long i, j, k, n, m, N, M, ans, a, b, X[200001] = {0};

int main() {
  cin >> N;

  vector<long long> L(N + 1), R(N + 1);
  // map<long long ,long long> L,R;

  for (i = 1; i <= N; i++) {
    cin >> j;
    L[i] = i - j;
    R[i] = i + j;
  }
  sort(L.begin(), L.end());
  sort(R.begin(), R.end());
  ans = 0;

  i = 1;
  j = 1;
  while (i <= N && j <= N) {
    if (L[i] < R[j]) {
      i++;
    } else if (L[i] > R[j]) {
      j++;
    } else {

      for (n = i + 1; n <= N; n++) {
        if (L[i] == L[n]) {
          continue;
        } else {
          break;
        }
      }

      for (m = j + 1; m <= N; m++) {
        if (R[j] == R[m]) {
          continue;
        } else {
          break;
        }
      }

      ans += (n - i) * (m - j);
      i = n;
      j = m;
    }
  }

  cout << ans;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;

long long i, j, k, n, m, N, M, ans, a, b, X[200001] = {0};

int main() {
  cin >> N;

  // vector<long long> L(N+1),R(N+1);
  map<long long, long long> L, R;

  for (i = 1; i <= N; i++) {
    cin >> j;
    L[i + j] += 1;
    R[i - j] += 1;
  }

  ans = 0;
  for (auto u : L) {
    for (auto v : R) {
      if (u.first == v.first) {
        ans += u.second * v.second;
      }
    }
  }

  cout << ans;
}