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

/*
          , -- 、
        /;;;;;;', 　　　
        i二二二i
      , ' 　    ヽ ???
     i　ﾉﾉﾉﾉﾉﾍ .', 　　
     〉ﾘi' ﾟ ヮﾟﾉﾚく　 WAyon..........
  ○⊂}〈lﾄ--ｲl〉{つ● 　　
      ,ｒ/ー i-ヽヽ 　　　
     ｀｀i_ﾗi_ﾗ
*/

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define int long long
const int INF = (int)1e12;
const int mod = (int)1e9 + 7;
#define all(a) (a).begin(), (a).end()
typedef vector<vector<int>> graph;

#define pb push_back

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> cnt(n + 100);
  rep(i, n) cin >> a[i];

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


Error: 0

Fixed submission:

/*
          , -- 、
        /;;;;;;', 　　　
        i二二二i
      , ' 　    ヽ ???
     i　ﾉﾉﾉﾉﾉﾍ .', 　　
     〉ﾘi' ﾟ ヮﾟﾉﾚく　 WAyon..........
  ○⊂}〈lﾄ--ｲl〉{つ● 　　
      ,ｒ/ー i-ヽヽ 　　　
     ｀｀i_ﾗi_ﾗ
*/

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define int long long
const int INF = (int)1e12;
const int mod = (int)1e9 + 7;
#define all(a) (a).begin(), (a).end()
typedef vector<vector<int>> graph;

#define pb push_back

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> cnt(n + 100);
  rep(i, n) cin >> a[i];

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

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

long long A[200005];

int main() {
  long long N;

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

  map<long long, long long> count;

  long long ans = 0;
  for (long long i = 0; i < N; i++) {
    ans += count[i - A[i]];
    count[i + A[i]]++;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

long long A[100005];

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

  map<long long, long long> count;

  long long ans = 0;
  for (long long i = 0; i < N; i++) {
    ans += count[i - A[i]];
    count[i + A[i]]++;
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

typedef long long ll;
typedef long double ld;

#define n1 "\\n"
#define B begin()
#define E end()
#define F first
#define S second
#define pb push_back
#define pf push_front
#define popb pop_back()
#define popf pop_front()

const int BIG = 1e5 + 55;
const int BIGG = 1e9 + 7;
const ll BIGGE = 1e12 + 55;
const double SML = (1e-7);

using namespace std;

ll n, a[2 * BIG], b[2 * BIG], ans;
map<ll, ll> mp;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

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

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

  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

typedef long long ll;
typedef long double ld;

#define n1 "\\n"
#define B begin()
#define E end()
#define F first
#define S second
#define pb push_back
#define pf push_front
#define popb pop_back()
#define popf pop_front()

const int BIG = 1e5 + 55;
const int BIGG = 1e9 + 7;
const ll BIGGE = 1e12 + 55;
const double SML = (1e-7);

using namespace std;

int n, a[2 * BIG], ans;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

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

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

  cout << ans;

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;

  cin >> n;
  vector<int64_t> a(n);

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

  map<int64_t, int> b; // rabel ga ookisugiru

  int64_t ans = 0;
  for (int i = 1; i <= n; i++) { // a[i]+i==j-a[j]
    ans += b[i - a[i - 1]];
    b[a[i - 1] + i]++;
  }

  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;

  cin >> n;
  vector<int64_t> a(n);

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

  map<int64_t, int> b; // rabel ga ookisugiru

  int ans;
  for (int i = 0; i < n; i++) { // a[i]+i==j-a[i]
    ans += b[a[i] + i];
    b[i - a[i]]++;
  }

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

#include <bits/stdc++.h>
#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define rep(i, n) REP(i, 0, n)
#define rep1(i, n) REP(i, 1, n)
#define repe(i, n) for (auto &&i : n)
#define all(v) (v).begin(), (v).end()
#define decimal fixed << setprecision(20)
#define fastcin()                                                              \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define int long long
using namespace std;
using LL = long long;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const LL LLINF = 1e16;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

signed main() {
  fastcin();
  int n;
  cin >> n;
  vector<LL> a(n);
  rep(i, n) cin >> a[i];
  vector<LL> tmp;
  rep(i, n) { tmp.push_back(a[i] + i); }
  sort(all(tmp));
  int ans = 0;
  rep(i, n) {
    LL k = -a[i] + i;
    ans += upper_bound(all(tmp), k) - lower_bound(all(tmp), k);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, s, e) for (LL i = (s); i < (e); i++)
#define rep(i, n) REP(i, 0, n)
#define rep1(i, n) REP(i, 1, n)
#define repe(i, n) for (auto &&i : n)
#define all(v) (v).begin(), (v).end()
#define decimal fixed << setprecision(20)
#define fastcin()                                                              \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
using namespace std;
using LL = long long;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const LL LLINF = 1e16;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int main() {
  fastcin();
  int n;
  cin >> n;
  vector<LL> a(n);
  rep(i, n) cin >> a[i];
  multiset<LL> tmp;
  tmp.insert(a[0] + 1);
  int ans = 0;
  rep1(i, n) {
    LL k = (i + 1) - a[i];
    ans += tmp.count(k);
    tmp.insert(a[i] + i + 1);
    // cout << i << " " << ans << endl;
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
int arr[maxn];
int pre[maxn];
int suf[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  for (int i = 0; i < n; ++i) {
    if (i + arr[i] < n)
      ++pre[i + arr[i]];
    if (i - arr[i] >= 0)
      ++suf[i - arr[i]];
  }
  long long int ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += (long long)pre[i] * suf[i];
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
int arr[maxn];
int pre[maxn];
int suf[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  for (int i = 0; i < n; ++i) {
    ++pre[i + arr[i]];
    if (i - arr[i] >= 0)
      ++suf[i - arr[i]];
  }
  long long int ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += (long long)pre[i] * suf[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, a;
  long long ans = 0;
  cin >> n;
  map<int, int> cnt;
  for (int i = 0; i < n; i++) {
    cin >> a;
    ans += cnt[i - a];
    cnt[i + a]++;
  }
  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 0) >= this->size() (which is 0)


Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<long long> a = {};
  int i, ans = 0;
  map<long long, long long> renso{};

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

  for (i = 0; i < int(a.size()); i++) {
    ans += renso[i - a[i]];
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;
using pll = pair<long long, long long>;

int main() {
  // 標準入力が早くなる。Cスタイルの入出力とC++スタイルの入出力を混合させてはならない。
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  map<long long, long long> sum;
  map<long long, long long> dif;

  for (int i = 1; i <= N; i++) {
    int tmp;
    cin >> tmp;
    ++sum[i + tmp];
    ++dif[i - tmp];
  }
  long long res = 0;

  for (int i = 0; i < N; i++) {
    res += sum[i] * dif[i];
  }
  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;
using pll = pair<long long, long long>;

int main() {
  // 標準入力が早くなる。Cスタイルの入出力とC++スタイルの入出力を混合させてはならない。
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> sum(N);
  vector<int> dif(N);

  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    sum.at(i) = tmp + i;
    dif.at(i) = i - tmp;
  }
  int res = 0;

  for (int i = 0; i < N; i++) {
    // for (int j = i + 1; j < N; j++) {
    //   if (sum.at(i) == dif.at(j)) {
    //     ++res;
    //   }
    // }
    res += count(dif.begin(), dif.end(), sum.at(i));
  }
  cout << res << endl;

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

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
ll MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  unordered_map<ll, ll> L;
  unordered_map<ll, ll> R;
  for (ll i = 0; i < N; i++) {
    ll A;
    cin >> A;
    L[i + A]++;
    R[i - A]++;
  }
  ll ans = 0;
  for (ll i = 1; i < 2 * N; i++) {
    ans += L[i] * R[i];
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

int a[200009];
int b[200009];

int main() {
  int n;
  int ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i] + i;
  }
  for (int j = n; j >= 1; j--) {
    int temp = j - a[j];
    for (int i = 1; i < j && i < temp; i++) {
      if (b[i] == temp) {
        ans++;
      }
    }
  }

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

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll n, ans, temp;
  cin >> n;
  ans = 0;
  ll a[n + 5];
  map<ll, ll> hoge;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    hoge[i + a[i]]++;
  }
  for (ll i = 0; i < n; i++) {
    ans += hoge[i - a[i]];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll n, ans;
  cin >> n;
  ans = 0;
  ll a[n + 5];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < n; i++) {
    for (ll j = i + 1; j < n; j++) {
      if (a[i] + a[j] == j - i)
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_ll(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
typedef long long ll;
typedef unsigned long long u_ll;
typedef pair<int, int> pair_;
const long double PI = (acos(-1));
const int INF = 1001001001;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
ll mod = 1000000007;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll cnt = 0;
  map<int, int> mp;
  rep(j, n) {
    int j_aj = j - a[j];
    int i_p_ai = j + a[j];
    cnt += mp[j_aj];
    mp[i_p_ai]++;
  }
  cout << cnt;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_ll(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
typedef long long ll;
typedef unsigned long long u_ll;
typedef pair<int, int> pair_;
const long double PI = (acos(-1));
const int INF = 1001001001;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
ll mod = 1000000007;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll cnt = 0;

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

// #include <math.h>
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#include <iostream>
#include <stdio.h>
#include <vector>
// #include <string.h>
using namespace std;

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

  cout << ans << endl;
}


Error: TLE

Fixed submission:

// #include <math.h>
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#include <iostream>
#include <stdio.h>
#include <vector>
// #include <string.h>
using namespace std;

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

  int ans = 0;
  rep(i, n - 1) {
    for (int j = i + 1; j < n; j++) {
      if ((j - i) * (j - i) == (a[i] + a[j]) * (a[i] + a[j]))
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  ll N, A;
  cin >> N;
  map<ll, ll> m, l;
  ll ans = 0;
  REP(i, N) {
    cin >> A;
    ans += m[i - A];
    m[A + i]++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  ll ans = 0;
  REP(i, N) {
    FOR(j, i, N) {
      ll I, J;
      I = A[i] + i;
      J = j - A[j];
      if (I == J)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#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() {
  ll n;
  cin >> n;
  vector<ll> a(n), b(n);
  ll aa, bb;

  rep(i, n) {
    cin >> aa;
    a[i] = i + aa + 1;
    b[i] = i - aa + 1;
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  ll k = 0, k2 = 0;
  ll ans = 0;

  rep(i, n) {

    if (a[i] == a[i - 1] and i != 0) {
      ans += k2;
    } else {
      k2 = 0;
      if (k > n - 1) {
        break;
      }
      while (a[i] >= b[k]) {
        if (a[i] == b[k]) {
          ++ans;
          ++k2;
        }
        ++k;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

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() {
  ll n;
  cin >> n;
  vector<ll> a(n), b(n);
  ll aa, bb;

  rep(i, n) {
    cin >> aa;
    a[i] = i + aa + 1;
    b[i] = i - aa + 1;
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  ll k = 0, k2 = 0;
  ll ans = 0;

  rep(i, n) {

    if (a[i] == a[i - 1] and i != 0) {
      ans += k2;
    } else {
      k2 = 0;
      while (a[i] >= b[k]) {
        if (a[i] == b[k]) {
          ++ans;
          ++k2;
        }
        ++k;
      }
    }
  }

  cout << ans << endl;

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

#include <iomanip>
#include <iostream>
#include <map>
#include <vector>

#include <cfenv>
#include <cmath>
typedef long long int ll;

using namespace std;

int main() {

  ll N;

  cin >> N;

  vector<ll> A(N);

  map<int, int> mp;

  ll sum = 0;

  for (ll i = 1; i <= N; i++) {

    ll a;
    cin >> a;

    sum += mp[i - a];

    mp[i + a]++;
  }

  cout << sum << endl;

  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

#include <cfenv>
#include <cmath>
typedef long long int ll;

using namespace std;

int main() {

  ll N;

  cin >> N;

  vector<ll> A(N);

  vector<vector<ll>> L_R(2, vector<ll>(1000200005, 0));

  for (ll i = 1; i <= N; i++) {

    ll a;
    cin >> a;

    L_R[0][a + i]++;

    if (i - a > 0) {
      L_R[1][i - a]++;
    }
  }

  int sum = 0;
  for (ll i = 0; i < 1000200005; i++) {

    sum += L_R[0][i] * L_R[1][i];
  }

  cout << sum << endl;

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define endl "\\n"
#define ll long long
using namespace std;

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

  unordered_map<ll, ll> mp;
  ll count = 0;
  for (ll i = 0; i < n; i++) {
    ll value;
    cin >> value;
    count += mp[i - value];
    mp[i + value]++;
  }

  cout << count << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define endl "\\n"
#define ll long long
using namespace std;

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

  vector<pair<ll, ll>> v(n);

  for (ll i = 0; i < n; i++) {
    cin >> v[i].first;
    v[i].second = i + 1;
  }

  sort(v.begin(), v.end());

  ll maxm = n - 1;
  ll count = 0;
  for (ll i = 0; i < n - 1; i++) {
    if (v[i].first >= maxm)
      break;
    for (ll j = i + 1; j < n; j++) {
      ll sum = v[i].first + v[j].first;
      if (sum > maxm)
        break;
      else if (sum == abs(v[i].second - v[j].second))
        count++;
    }
  }
  cout << count << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define all(v) v.begin(), v.end()
using pint = pair<int, int>;
using tint = tuple<int, int, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N;
  cin >> N;
  vector<int64_t> v(N);
  rep(i, N) { cin >> v.at(i); }

  int64_t a = 0, b = 0, c = 0, x, y, z;
  int64_t p[1000000];
  x = 500000;
  rep(i, N) {
    y = v.at(i);
    if (x + y < 1000000) {
      a += p[x + y];
    }
    if (x - y >= 0) {
      p[x - y]++;
    }
    x--;
  }
  std::cout << a << '\\n';
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define all(v) v.begin(), v.end()
using pint = pair<int, int>;
using tint = tuple<int, int, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N;
  cin >> N;
  vector<int> v(N);
  rep(i, N) { cin >> v.at(i); }

  int a = 0, b = 0, c = 0, x, y, z;
  int p[100000000];
  x = 50000000;
  rep(i, N) {
    y = v.at(i);
    if (x + y < 100000000) {
      a += p[x + y];
    }
    if (x - y >= 0) {
      p[x - y]++;
    }
    x--;
  }
  std::cout << a << '\\n';
}