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

#include <bits/stdc++.h>
#define exrep(i, a, b) for (long long i = a; i <= b; i++)
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vector<ll>> vvl;
typedef vector<vector<P>> vvp;
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 ll MOD = 1e9 + 7;
const ll INF = 1e18;
const int MAX = 510000;
const double pi = acos(-1);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

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

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

  map<ll, ll> mp1, mp2;
  rep(i, n) {
    if (a[i] >= n - 1)
      continue;
    mp1[a[i] + i]++;
    mp2[i - a[i]]++;
  }
  ll cnt = 0;
  for (auto p1 : mp1) {
    cnt += p1.second * mp2[p1.first];
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define exrep(i, a, b) for (long long i = a; i <= b; i++)
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vector<ll>> vvl;
typedef vector<vector<P>> vvp;
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 ll MOD = 1e9 + 7;
const ll INF = 1e18;
const int MAX = 510000;
const double pi = acos(-1);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

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

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

  map<ll, ll> mp1, mp2;
  rep(i, n) {
    mp1[a[i] + i]++;
    mp2[i - a[i]]++;
  }
  ll cnt = 0;
  for (auto p1 : mp1) {
    for (auto p2 : mp2) {
      if (p1.first == p2.first)
        cnt += p1.second * p2.second;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

  ll count = 0;
  map<ll, ll> mp;

  rep(i, N) {
    count += mp[i - Ai[i]];
    // cout << mp[i-Ai[i]] << "\\n";
    mp[Ai[i] + i]++;
  }

  cout << count;
}

Error: TLE

Fixed submission:

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

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

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

  ll count = 0;
  for (ll i = 2; i <= N - 1; i++) {
    for (ll j = 0; j < N - i; j++) {
      if (Ai[j] + Ai[j + i] == i) {
        count++;
        // cout << j << j+i << "\\n";
      }
    }
  }
  cout << count;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <set>
#include <string>

using namespace std;

int main(void) {
  long long N, ans = 0, tmp;
  map<long long, long long> X, Y;

  cin >> N;

  for (long long i = 1; i <= N; i++) {
    cin >> tmp;
    X[i + tmp]++;
    Y[i - tmp]++;
  }

  for (auto itr = X.begin(); itr != X.end(); ++itr) {
    ans = ans + X[itr->first] * Y[itr->first];
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <set>
#include <string>

using namespace std;

int main(void) {
  long long N, ans = 0;
  ;
  map<long long, long long> A;

  cin >> N;

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

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

  cout << ans << endl;

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

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

int main() {
  ll n, a, ans;
  map<ll, ll> Am, Ap;
  cin >> n;
  REP(int, i, 0, n) {
    cin >> a;
    Am[a - i]++;
    Ap[a + i]++;
  }

  ans = 0;
  for (auto i : Am) {
    ans += i.second * Ap[-i.first];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, ans;
  int a[MAX];
  cin >> n;
  REP(int, i, 0, n) cin >> a[i];

  ans = 0;
  REP(int, i, 0, n - 1) {
    REP(int, j, i + 1, n) {
      if (a[i] + a[j] == abs(i - 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 REP(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define REPD(i, n) for (ll i = n; 0 < i; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)

#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

#define INF 1000000000000 // 10^12

int main() {
  ll N, a, count = 0;
  map<ll, ll> m{};

  cin >> N;

  REP(i, N) {
    cin >> a;
    count += m[i - a];
    m[a + i] += 1;
  }

  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)

#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

#define INF 1000000000000 // 10^12

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

  ll *A = new ll[N + 1];
  FOR(i, 1, N) cin >> A[i];

  ll count = 0;
  FOR(i, 1, N) {
    ll B = A[i] + i;
    FOR(j, i + 1, N) {
      if (B == j - A[j]) {
        count++;
      }
    }
  }

  cout << count;

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, x, n) for (int i = x; i <= n; i++)
#define per(i, n, x) for (int i = n; i >= x; i--)
#define sz(a) int(a.size())
#define rson mid + 1, r, p << 1 | 1
#define pii pair<int, int>
#define lson l, mid, p << 1
#define ll long long
#define pb push_back
#define mp make_pair
#define se second
#define fi first
using namespace std;
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int inf = 1e9;
int n, a[N];
map<int, int> cnt;
int main() {
  // ios::sync_with_stdio(false);
  // freopen("in","r",stdin);
  cin >> n;
  ll ans = 0;
  rep(i, 1, n) {
    cin >> a[i];
    ans += cnt[i - a[i]];
    cnt[i + a[i]]++;
  }
  cout << ans << endl;
  return 0;
}
// j+a[j]==i-a[i];

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, x, n) for (int i = x; i <= n; i++)
#define per(i, n, x) for (int i = n; i >= x; i--)
#define sz(a) int(a.size())
#define rson mid + 1, r, p << 1 | 1
#define pii pair<int, int>
#define lson l, mid, p << 1
#define ll long long
#define pb push_back
#define mp make_pair
#define se second
#define fi first
using namespace std;
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int inf = 1e9;
int n, a[N];
map<int, int> cnt;
int main() {
  // ios::sync_with_stdio(false);
  // freopen("in","r",stdin);
  cin >> n;
  ll ans = 0;
  rep(i, 1, n) {
    cin >> a[i];
    ans += cnt[i - a[i]];
    cnt[i + a[i]]++;
  }
  cout << ans << endl;
  return 0;
}
// j+a[j]==i-a[i];
Problem p02691 (C++) — buggy submission, verdict: Runtime Error


// Problem : E - This Message Will Self-Destruct in 5s
// Contest : AtCoder - AtCoder Beginner Contest 166
// URL : https://atcoder.jp/contests/abc166/tasks/abc166_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>

const int N = 2e5 + 5;
int n, a[N];
std::map<int, int> b, c;

int main() {
  scanf("%d", &n);
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    ans += b[i - a[i]] + c[a[i] + i];
    ++b[i + a[i]], ++c[i - a[i]];
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:


// Problem : E - This Message Will Self-Destruct in 5s
// Contest : AtCoder - AtCoder Beginner Contest 166
// URL : https://atcoder.jp/contests/abc166/tasks/abc166_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>

const int N = 2e5 + 5;
int n, a[N], b_pool[N * 2], c[N * 2];

int main() {
  scanf("%d", &n);
  long long ans = 0;
  int *b = b_pool + N - 1;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    ans += b[i - a[i]] + c[a[i] + i];
    ++b[i + a[i]], ++c[i - a[i]];
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define rep(i, cc, n) for (int i = cc; i <= n; ++i)
#define drep(i, cc, n) for (int i = cc; i >= n; --i)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
typedef long long ll;
using namespace std;

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

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

ll choose2(ll n) { return n * (n - 1) / 2; }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define rep(i, cc, n) for (int i = cc; i <= n; ++i)
#define drep(i, cc, n) for (int i = cc; i >= n; --i)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
typedef long long ll;
using namespace std;

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

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

ll choose2(ll n) { return n * (n - 1) / 2; }

int n, m, q;
int A[10];
int a[50], b[50], c[50], d[50];
ll ans = 0;

void dfs(int i, int val) {
  if (i == n) {
    ll tmp = 0;
    rep(k, 0, q - 1) {
      if (A[b[k] - 1] - A[a[k] - 1] == c[k])
        tmp += d[k];
    }
    ans = max(ans, tmp);
    return;
  }
  rep(j, val, m) {
    A[i] = j;
    dfs(i + 1, j);
  }
  return;
}

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

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
#define rep(i, j, n) for (int i = j; i < (int)(n); i++)
const ll MOD = 1000000007;
const ll INFL = (1LL << 60);
const int INFI = (1 << 30);

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

  vector<ll> A(N + 1);
  map<ll, ll> sumAi, gapAi;
  for (ll i = 1; i < N + 1; i++) {
    cin >> A.at(i);
    sumAi[i + A.at(i)]++;
    gapAi[i - A.at(i)]++;
  }

  ll ans = 0;

  for (auto p : sumAi) {
    ans += p.second * gapAi[p.first];
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
#define rep(i, j, n) for (int i = j; i < (int)(n); i++)
const ll MOD = 1000000007;
const ll INFL = (1LL << 60);
const int INFI = (1 << 30);

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

  vector<ll> A(N + 1);
  rep(i, 1, N + 1) cin >> A.at(i);

  vector<ll> sumAi(N + 1), gapAi(N + 1);
  rep(i, 1, N + 1) {
    sumAi.at(i) = i + A.at(i);
    gapAi.at(i) = i - A.at(i);
  }

  ll ans = 0;

  rep(i, 1, N + 1) {
    vector<ll> tmp = gapAi;
    sort(tmp.begin() + i + 1, tmp.end());
    ans += upper_bound(tmp.begin() + i + 1, tmp.end(), sumAi.at(i)) -
           lower_bound(tmp.begin() + i + 1, tmp.end(), sumAi.at(i));
  }

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

#include <bits/stdc++.h>
#include <cctype>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<typename key, typename val>
// using ordered_tree =
//   tree<key, val, std::less<>, rb_tree_tag,
//   tree_order_statistics_node_update>;
using namespace std;
typedef long long int64;
typedef unsigned long long uint64;
typedef complex<double> comp;
const double pi = 3.14159265358979323846;
const int inf = (int)1e+9 + 2;
const int64 inf64 = (int64)1e+18 + 2;
const double dinf = 1e+20;
const int mod = 1'000'000'007; //*/998244353;
const int base = 2187;
const double eps = 1e-9;
const int N = 300'000;
const int LOGN = 19;

int n, m, k;

void solve(int) {
  cin >> n;
  map<int, int> mp;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i] -= i;
    ++mp[a[i]];
  }
  int64 ans = 0;
  for (int i = 0; i < n; ++i) {
    auto it = mp.find(a[i]);
    --it->second;
    if (!it->second) {
      mp.erase(it);
    }
    it = mp.find(-a[i] - i - i);
    if (it != end(mp)) {
      ans += it->second;
    }
  }
  cout << ans << '\\n';
}

void precalc() {}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  precalc();
  int test = 1;
  // cin >> test;
  // auto start = chrono::high_resolution_clock::now();
  for (int i = 1; i <= test; ++i) {
    solve(i);
  }
  // cerr <<
  // chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()
  // - start).count() << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cctype>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<typename key, typename val>
// using ordered_tree =
//   tree<key, val, std::less<>, rb_tree_tag,
//   tree_order_statistics_node_update>;
using namespace std;
typedef long long int64;
typedef unsigned long long uint64;
typedef complex<double> comp;
const double pi = 3.14159265358979323846;
const int inf = (int)1e+9 + 2;
const int64 inf64 = (int64)1e+18 + 2;
const double dinf = 1e+20;
const int mod = 1'000'000'007; //*/998244353;
const int base = 2187;
const double eps = 1e-9;
const int N = 300'000;
const int LOGN = 19;

int n, m, k;

void solve(int) {
  cin >> n;
  multiset<int> st;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i] -= i;
    st.insert(a[i]);
  }
  int64 ans = 0;
  for (int i = 0; i < n; ++i) {
    st.erase(st.find(a[i]));
    ans += st.count(-a[i] - i - i);
  }
  cout << ans << '\\n';
}

void precalc() {}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  precalc();
  int test = 1;
  // cin >> test;
  // auto start = chrono::high_resolution_clock::now();
  for (int i = 1; i <= test; ++i) {
    solve(i);
  }
  // cerr <<
  // chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()
  // - start).count() << '\\n';
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long N;
  std::cin >> N;

  std::vector<long long> A(N);
  for (long long i = 0; i < N; i++)
    std::cin >> A.at(i);

  long long ans = 0;
  // {}で0で初期化
  // https://tt4cs.hatenadiary.org/entry/20120219/1329636595
  long long cnt[200005] = {};
  for (int i = 0; i < N; i++) {
    // j-A_j==i+A_i. j-A_j<=N, i+A_i>0
    if (i - A.at(i) > 0)
      ans += cnt[i - A.at(i)];

    // i+A_i>Nは考えなくていい
    if (i + A.at(i) <= N)
      cnt[i + A.at(i)]++;
  }
  std::cout << ans;
}

Error: WA

Fixed submission:

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

int main() {
  long long N;
  std::cin >> N;

  std::vector<long long> A(N);
  for (long long i = 0; i < N; i++)
    std::cin >> A.at(i);

  long long ans = 0;
  // {}で0で初期化
  // https://tt4cs.hatenadiary.org/entry/20120219/1329636595
  long long cnt[200005] = {};
  for (int i = 0; i < N; i++) {
    if (i - A.at(i) > 0) {
      ans += cnt[i - A.at(i)];
    }
    cnt[i + A.at(i)]++;
  }
  std::cout << ans;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define INF 1000000007
#define LINF (1LL << 62)
#define PI 3.14159265358979

typedef long long i64;
typedef pair<i64, i64> P;

inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

i64 n, a[202020];
i64 cnt[202020];
void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  if (a[0] <= n)
    cnt[a[0]]++;
  i64 ans = 0;
  for (i64 i = 1; i < n; i++) {
    if (a[i] < i)
      ans += cnt[i - a[i]];
    if (a[i] + i <= n)
      cnt[a[i] + i]++;
  }

  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF 1000000007
#define LINF (1LL << 62)
#define PI 3.14159265358979

typedef long long i64;
typedef pair<i64, i64> P;

inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

i64 n, a[202020];
i64 cnt[202020];
void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

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

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define LL long long
using namespace std;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double EPS = 1e-8;

int n;
map<int, int> tu;
LL ans, a[maxn];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    ans += tu[i - a[i]];
    tu[a[i] + i]++;
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define LL long long
using namespace std;
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double EPS = 1e-8;

int n, a[maxn];
map<int, int> tu;
LL ans;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    ans += tu[i - a[i]];
    tu[a[i] + i]++;
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <vector>
typedef long long ll;
#define N 200010
using namespace std;
// int a[N];
map<int, int> ai;
void solve() {
  int n;
  cin >> n;
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    int num;
    cin >> num;
    ai[i + num]++;
    if (i - num > 0)
      ans += ai[i - num];
  }
  cout << ans;
}
int main() {
  ios::sync_with_stdio(false);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
typedef long long ll;
#define N 200010
using namespace std;
int a[N];
void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] > max(n - i, i - 1))
      continue;
    for (int j = i + 1; j <= n; j++)
      if (j - i == abs(a[j] + a[i])) {
        ans++;
        // cout<<j<<"  $ "<<i<<endl;
      }
  }

  cout << ans;
}
int main() {
  solve();
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

typedef pair<ll, ll> P;
const ll MO = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n, 0);
  map<ll, ll> mpl, mpr;
  rep(i, n) cin >> a[i];
  rep(i, n) {
    ll x, y;
    x = i + a[i];
    y = i - a[i];
    mpl[x]++;
    mpr[y]++;
  }
  ll ans = 0;
  for (auto ele : mpl) {
    ans += ele.second * mpr[ele.first];
  }

  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>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n + 1); ++i)
using namespace std;
using ll = long long;
const ll INF = +10010010000;

typedef pair<ll, ll> P;
const ll MO = 1000000007;

const ll MAX = 100000000;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n, 0), l(MAX, 0), r(MAX, 0);
  rep(i, n) cin >> a[i];
  // cout << "t"<<endl;
  rep(i, n) {
    ll x, y;
    x = i + a[i];
    y = i - a[i];
    ++l[x];
    ++r[y];
  }
  ll ans = 0;
  rep(i, n) { ans += l[i] * r[i]; }
  cout << ans << endl;
  return 0;
}