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

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

using namespace std;
typedef long long int ll;

int N;

map<ll, ll> L, R;
// vector<int> A;

int main() {
  int i, p;
  int a;
  ll ans = 0;
  unsigned long c;

  cin >> N;
  for (i = 1; i <= N; i++) {
    cin >> a;
    auto itl = L.find(i + a);
    if (itl == L.end())
      L.emplace(i + a, 1);
    else {
      p = itl->second + 1;
      L.erase(itl);
      L.emplace(i + a, p);
    }
    auto itr = R.find(i - a);
    if (itr == R.end())
      R.emplace(i - a, 1);
    else {
      p = itr->second + 1;
      R.erase(itr);
      R.emplace(i - a, p);
    }

    //    A[i] = a;
  }

  auto itl_last = L.end();
  for (auto itl = L.begin(); itl != itl_last; itl++) {
    auto itr = R.find(itl->first);
    if (itr != R.end()) {
      ans += itl->second * itr->second;
    }
  }

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

  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;
typedef long long int ll;

int N;

vector<int> L;
multimap<int, int> R;
// vector<int> A;

int main() {
  int i;
  int a;
  int ans = 0;

  cin >> N;
  L.resize(N);
  for (i = 0; i < N; i++) {
    cin >> a;
    L[i] = i + a;
    R.emplace(i - a, i);
    //    A[i] = a;
  }

  //  auto itl_last = L.end();
  //  for (auto itl=L.begin(); itl!=itl_last; itl++) {
  //    unsigned long c = R.count(itl->first);
  for (i = 0; i < N; i++) {
    unsigned long c = R.count(L[i]);
    // printf("%d, %d : %lu\\n", itl->first, itl->second, c);
    // printf("%d, %d : %lu\\n", L[i], i, c);
    ans += c;
    /*
    auto itr_last = R.upper_bound(itl->first);
    for (auto itr = R.lower_bound(itl->first); itr != itr_last; itr++) {
//printf("%d:%d %d:%d\\n", itl->second, A[itl->second], itr->second,
A[itr->second]); ans++;
    }
      */
  }

  printf("%d\\n", ans);

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

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

typedef long long ll;

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

  map<ll, ll> m;
  ll ans = 0;

  for (int i = 0; i < n; i++)
    m[i - a[i]]++;
  for (int i = 0; i < n; i++)
    ans += 1ll * m[i + a[i]];

  cout << 1ll * ans;
}

Error: TLE

Fixed submission:

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

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

  int ans = 0;
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 2; j < n; j++) {
      long long diff = abs(i - j);
      long long sum = a[i] + a[j];
      if (diff == sum)
        ans++;
    }
  }

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

#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
template <typename T> using V = vector<T>;
constexpr int Mod = 998244353;
constexpr int mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
template <typename T> constexpr bool chmax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}
template <typename T> constexpr bool chmin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

/*-------------------------------------------*/

ll n;
ll a[200009];
map<ll, ll> mp;

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

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

  ll ans = 0;
  rep(i, n) {
    if (mp.count(i - 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 FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
template <typename T> using V = vector<T>;
constexpr int Mod = 998244353;
constexpr int mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
template <typename T> constexpr bool chmax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}
template <typename T> constexpr bool chmin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

/*-------------------------------------------*/

ll n;
ll a[200009];
multiset<ll> st;

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

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

  ll ans = 0;
  rep(i, n) {
    ans += st.count(i - a[i]);
    st.insert(i + a[i]);
  }

  cout << ans << endl;

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

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

int main() {
  int N;
  cin >> N;
  map<long long, pair<long long, long long>> data;
  queue<long long> keep;
  for (long long i = 0; i < N; i++) {
    long long A;
    cin >> A;
    if (!data.count(i + 1 + A)) {
      data[i + 1 + A] = make_pair(1, 0);
      keep.push(i + 1 + A);
    } else {
      data.at(i + 1 + A).first++;
    }
    if (!data.count(i + 1 - A)) {
      data[i + 1 - A] = make_pair(0, 1);
      keep.push(i + 1 - A);
    } else {
      data.at(i + 1 - A).second++;
    }
  }
  long long ans = 0;
  while (!keep.empty()) {
    long long K = keep.front();
    keep.pop();
    ans += data.at(K).first * data.at(K).second;
  }
  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.at(i);
  }
  long long ans = 0;
  for (int i = 1; i <= N + 1; i++) {
    for (int j = 0; j + i < N; j++) {
      if (A.at(j) + A.at(j + i) == i) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
namespace {
using std::cin, std::cout, std::endl, std::string, std::vector;
}

int main() {
  std::unordered_map<int, int> map;
  int N;
  cin >> N;
  long long count = 0;
  for (int i = 0; i < N; ++i) {
    int A;
    cin >> A;
    if (i + A < N)
      map[i + A]++;
    if (i - A >= 0)
      count += map[i - A];
  }
  cout << count << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

namespace {
using std::cin, std::cout, std::endl, std::string, std::vector;
}

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

/**
 * Title: E - This Message Will Self-Destruct in 5s
 * Url: https://atcoder.jp/contests/abc166/tasks/abc166_e
 */
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  map<ll, ll> Li, Ri;
  for (int i = 0; i < n; i++) {
    Li[i + 1 - a[i]]++;
    Ri[i + 1 + a[i]]++;
  }
  ll ans = 0;
  for (auto li : Li) {
    if (Ri.count(li.first) != 0) {
      ans += li.second * Ri[li.first];
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

/**
 * Title:
 * Url:
 */
#include <bits/stdc++.h>

using namespace std;

#define ll long long

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

  ll ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] > i)
      continue;
    for (int j = 0; j < i; j++) {
      if (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>

int e() {
  int n;
  std::cin >> n;

  std::vector<long long> ha_a(n, 0);
  std::vector<long long> hb_b(n, 0);
  for (int i = 0; i < n; i++) {
    long long temp;
    std::cin >> temp;
    ha_a[i] = i + 1 - temp;
    hb_b[i] = i + 1 + temp;
  }

  std::sort(hb_b.begin(), hb_b.end());

  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    std::vector<long long>::iterator it =
        std::lower_bound(hb_b.begin(), hb_b.end(), ha_a[i]);
    std::vector<long long>::iterator it2 =
        std::upper_bound(hb_b.begin(), hb_b.end(), ha_a[i]);
    if (*it == ha_a[i]) {
      ans += std::distance(it, it2);
    }
  }

  std::cout << ans << std::endl;

  return 0;
}

int main(int argc, const char *argv[]) {
  e();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

int e() {
  int n;
  std::cin >> n;

  std::vector<long long> ha_a(n, 0);
  std::vector<long long> hb_b(n, 0);
  for (int i = 0; i < n; i++) {
    long long temp;
    std::cin >> temp;
    ha_a[i] = i + 1 - temp;
    hb_b[i] = i + 1 + temp;
  }

  std::sort(hb_b.begin(), hb_b.end());

  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    std::vector<long long>::iterator it =
        std::lower_bound(hb_b.begin(), hb_b.end(), ha_a[i]);
    while (true) {
      if (*it != ha_a[i]) {
        break;
      }
      ans++;
      ++it;
    }
  }

  std::cout << ans << std::endl;

  return 0;
}

int main(int argc, const char *argv[]) {
  e();
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n;
map<ll, ll> One, Two;

int main() {
  scanf("%d", &n);
  for (ll i = 1; i <= n; i++) {
    ll x;
    scanf("%lld", &x);
    One[x + i]++;
    Two[i - x]++;
  }
  ll ans = 0;
  for (auto &Now : One)
    ans += Two[Now.first] * Now.second;
  printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
ll n;
ll a[200005];

int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = i + a[i] + 1; j <= n; j++) {
      if (a[i] + a[j] == j - i)
        ans++;
    }
  }
  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 int
int main() {
  int n;
  cin >> n;
  vector<int> v(n + 1);
  map<ll, ll> mp;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    mp[i - v[i]]++;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += mp[i + v[i]];
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define debug(x) cerr << #x << ":" << x << endl
#define fix cout << fixed << setprecision(20);
const int maxn = (int)1e6 + 5;
long long a[maxn], ans;
unordered_map<int, long long> mp;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  for (int i = n; i >= 1; i--) {
    ans += mp[a[i] + i];
    mp[i - a[i]]++;
  }
  cout << ans << endl;
}
/*
i>j:
Ai+Aj=i-j
Ai-i=-j-Aj
i<j:
Ai+Aj=j-i
Ai+i=j-Aj
 */

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define debug(x) cerr << #x << ":" << x << endl
#define fix cout << fixed << setprecision(20);
const int maxn = (int)1e6 + 5;
int a[maxn], ans;
unordered_multiset<int> st;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  for (int i = n; i >= 1; i--) {
    ans += st.count(a[i] + i);
    st.insert(i - a[i]);
  }
  cout << ans << endl;
}
/*
i>j:
Ai+Aj=i-j
Ai-i=-j-Aj
i<j:
Ai+Aj=j-i
Ai+i=j-Aj
 */
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;
  unordered_map<ll, ll> sum;
  unordered_map<ll, ll> sub;
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    ans += sub[i + a];
    ans += sum[i - a];
    sum[i + a]++;
    sub[i - a]++;
  }
  cout << ans << 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<ll> a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = i + 1; j < n; j++) {
      if (abs(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;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define endl '\\n'
#define y1 holyshit
#define all(x) x.begin(), x.end()
const int inf = 0x3f3f3f3f;

int N, d[200010], A[200010], B[200010], cnt[500010];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  vector<int> comp;
  for (int i = 1; i <= N; i++) {
    cin >> d[i];
    A[i] = -d[i] - i;
    B[i] = d[i] - i;
    comp.push_back(A[i]);
    comp.push_back(B[i]);
  }
  sort(all(comp));
  comp.erase(unique(all(comp)), comp.end());
  for (int i = 1; i <= N; i++) {
    A[i] = lower_bound(all(comp), A[i]) - comp.begin();
    B[i] = lower_bound(all(comp), B[i]) - comp.begin();
    cnt[B[i]]++;
  }
  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    cnt[B[i]]--;
    ans += cnt[A[i]];
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define endl '\\n'
#define y1 holyshit
#define all(x) x.begin(), x.end()
const int inf = 0x3f3f3f3f;

int N, d[200010], A[200010], B[200010];
multiset<int> s;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> d[i];
    A[i] = d[i] + i;
    B[i] = d[i] - i;
  }
  for (int i = 1; i <= N; i++)
    s.insert(B[i]);
  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    s.erase(s.lower_bound(B[i]));
    ans += s.count(-A[i]);
  }
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;
#define REP(i, a, b) for (int i = int(a); i <= int(b); i++)
#define DEP(i, a, b) for (int i = int(a); i >= int(b); i--)
#define TR(c, it) for (auto it = (c).begin(); it != (c).end(); it++)

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n, m, i, j, k, l;
  cin >> n;
  map<ll, ll> mp;
  ll a[n];
  REP(i, 0, n - 1) { cin >> a[i]; }
  ll dif[n], sum[n];
  REP(i, 0, n - 1) {
    dif[i] = i - a[i];
    sum[i] = a[i] + i;
  }
  REP(i, 0, n - 1) {
    vector<ll> v;
    if (mp.find(sum[i]) == mp.end()) {
      mp.insert({sum[i], 1});
    } else {
      mp.find(sum[i])->second++;
    }
  }
  /*REP(i,0,n-1){
          cout<<dif[i]<<" ";
  }*/

  ll ans = 0;

  REP(i, 0, n - 1) {
    if (mp.find(dif[i]) != mp.end()) {

      ans += mp.find(dif[i])->second;
    }
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;
#define REP(i, a, b) for (int i = int(a); i <= int(b); i++)
#define DEP(i, a, b) for (int i = int(a); i >= int(b); i--)
#define TR(c, it) for (auto it = (c).begin(); it != (c).end(); it++)

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n, m, i, j, k, l;
  cin >> n;
  map<ll, vector<ll>> mp;
  ll a[n];
  REP(i, 0, n - 1) { cin >> a[i]; }
  ll dif[n], sum[n];
  REP(i, 0, n - 1) {
    dif[i] = i - a[i];
    sum[i] = a[i] + i;
  }
  REP(i, 0, n - 1) {
    vector<ll> v;
    if (mp.find(sum[i]) == mp.end()) {
      v.push_back(i);
      mp.insert({sum[i], v});
    } else {
      mp.find(sum[i])->second.push_back(i);
    }
  }
  /*REP(i,0,n-1){
          cout<<dif[i]<<" ";
  }*/

  ll ans = 0;

  REP(i, 0, n - 1) {
    if (mp.find(dif[i]) != mp.end()) {
      vector<ll> v = mp.find(dif[i])->second;
      ans += lower_bound(v.begin(), v.end(), i) - v.begin();
    }
  }
  cout << ans;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define REP(i, p, n) for (ll i = p; i < ll(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long int;
using pll = pair<ll, ll>;

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

  map<ll, ll> L, R;

  rep(i, N) {
    ll a;
    cin >> a;

    ++L[i + a];
    ++R[i - a];
  }

  ll ans = 0;

  for (const auto &l : L) {
    const auto lv = l.second;
    const auto rv = R[l.first];
    ans += lv * rv;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define REP(i, p, n) for (ll i = p; i < ll(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long int;
using pll = pair<ll, ll>;

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

  vector<ll> A(N);
  rep(i, N) { cin >> A[i]; }

  vector<ll> H(N), L(N);

  H[N - 1] = A[N - 1];
  for (ll i = N - 2; i >= 0; --i) {
    H[i] = max(H[i + 1], A[i]);
  }

  L[N - 1] = A[N - 1];
  for (ll i = N - 2; i >= 0; --i) {
    L[i] = min(L[i + 1], A[i]);
  }

  ll ans = 0;

  for (ll i = 0; i < N; ++i)
    for (ll j = i + 1; j < N; ++j) {
      const auto dd = j - i;
      const auto aa = A[i] + A[j];
      if (dd == aa) {
        ++ans;
      }

      const auto hh = A[i] + H[i];
      if (hh <= dd) {
        break;
      }

      if (dd <= A[i] + L[i]) {
        j += max(0LL, A[i] + L[i] - dd - 1);
      }
    }

  cout << ans << endl;

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

#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: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

int main(void) {
  vector<int> a(200001);
  vector<int> data(1000000001);
  vector<string> s(200001);
  map<int, int> sum;
  int n, ans = 0;
  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;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  long long int N;
  cin >> N;
  long long int A[N];
  for (long long int i = 0; i < N; i++)
    cin >> A[i];

  long long int help[N], help2[N];
  for (long long int i = 0; i < N; i++) {
    help[i] = i + 1 - A[i];
    help2[i] = i + 1 + A[i];
  }

  long long int aa[1000001], bb[1000001];

  for (long long int i = 0; i < 1000001; i++) {
    aa[i] = 0;
    bb[i] = 0;
  }

  for (long long int i = 0; i < N; i++) {
    if (help[i] >= 400000)
      continue;
    if (help2[i] >= 400000)
      continue;
    aa[help[i] + 500001]++;
    bb[help2[i] + 500001]++;
  }

  long long int count = 0;

  for (long long int i = 0; i < 1000001; i++) {
    count += aa[i] * bb[i];
  }

  cout << count << endl;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  long long int N;
  cin >> N;
  long long int A[N];
  for (long long int i = 0; i < N; i++)
    cin >> A[i];

  long long int help[N], help2[N];
  for (long long int i = 0; i < N; i++) {
    help[i] = i + 1 - A[i];
    help2[i] = i + 1 + A[i];
  }

  long long int aa[1000001], bb[1000001];

  for (long long int i = 0; i < 1000001; i++) {
    aa[i] = 0;
    bb[i] = 0;
  }

  for (long long int i = 0; i < N; i++) {
    aa[help[i] + 500001]++;
    bb[help2[i] + 500001]++;
  }

  long long int count = 0;

  for (long long int i = 0; i < 1000001; i++) {
    count += aa[i] * bb[i];
  }

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

#include <bits/stdc++.h>
using namespace std;
#define INFTY 1 << 20
typedef long long int ll;
const int MOD = 1000000007;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (n); ++i)

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {

  ll n, mx = 0;
  cin >> n;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  vector<ll> l(n + 1, 0), r(n + 1, 0);

  for (int i = 1; i <= n; i++) {
    if (n >= i + a[i])
      l[i + a[i]]++;
    if (i >= a[i])
      r[i - a[i]]++;
  }
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += l[i] * r[i];
  }
  cout << sum << endl;
  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INFTY 1 << 20
typedef long long int ll;
const int MOD = 1000000007;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (n); ++i)

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {

  ll n, mx = 0;
  cin >> n;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    mx = max(mx, a[i] + i);
  }
  vector<ll> l(mx + 1, 0), r(mx + 1, 0);

  for (int i = 1; i <= n; i++) {
    l[i + a[i]]++;
    r[i - a[i]]++;
  }
  ll sum = 0;
  for (int i = 1; i <= mx; i++) {
    sum += l[i] * r[i];
  }
  cout << sum << endl;
  return 0;
}