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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define MAX(a, b) a = a > b ? a : b
#define MIN(a, b) a = a < b ? a : b
#define REP(i, x, n) for (int i = x; i < n; i++)
#define REPR(i, x, n) for (int i = n - 1; i >= x; i--)
#define pb push_back
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLr(obj) (obj).rbegin(), (obj).rend()
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const double PI = 3.14159265358979323846;
ll dp;

int main() {
  ll n, ans = 0, z[402020] = {};
  cin >> n;
  rep(i, n) {
    ll a;
    cin >> a;
    if (i >= a)
      ans += z[i - a];
    if (i + a < 401010)
      z[a + i]++;
    // cout<<ans<<endl;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define MAX(a, b) a = a > b ? a : b
#define MIN(a, b) a = a < b ? a : b
#define REP(i, x, n) for (int i = x; i < n; i++)
#define REPR(i, x, n) for (int i = n - 1; i >= x; i--)
#define pb push_back
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLr(obj) (obj).rbegin(), (obj).rend()
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const double PI = 3.14159265358979323846;
ll dp;

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

#include <bits/stdc++.h>
#define endl '\\n'
#define ll long long
using namespace std;
const ll MAX = 2e5 + 2;
ll res;
pair<ll, ll> a[MAX];
map<ll, ll> ml, mr;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  for (ll i = 0; i < n; i++) {
    ml[a[i].first + a[i].second]++;
    if (mr[a[i].first + a[i].second] > 0) {
      res += mr[a[i].first + a[i].second];
    }
    mr[a[i].second - a[i].first]++;
    if (ml[a[i].second - a[i].first] > 0) {
      res += ml[a[i].second - a[i].first];
    }
  }
  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
using namespace std;
const int MAX = 2e5 + 2;
int res;
pair<int, int> a[MAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a, a + n);
  int ind = n - 1;
  for (int i = 0; i < n; i++) {
    while (a[i].first + a[ind].first >= n && ind > i) {
      ind--;
    }
    for (int j = ind; j > i; j--) {
      if (a[i].first + a[j].first == abs(a[i].second - a[j].second)) {
        res++;
      }
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  map<int, int> add;
  vector<int> sub;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    int temp;
    cin >> temp;
    add[temp + i]++;
    sub.push_back(i - temp);
  }
  long long int ans = 0;
  for (int i = 1; i < sub.size(); i++)
    ans += add[sub[i]];

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;

  cin >> n;
  long long int a[n + 1], ans = 0;

  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[j] + a[i]))
        ++ans;
    }

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

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

int main() {

  int N;
  cin >> N;
  int A[N];
  unordered_map<long long, long long> mapL;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    mapL[i - A[i]] += 1;
  }

  long long ans = 0;

  for (int i = 1; i <= N; i++) {
    ans += mapL[A[i] + i];
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int main() {

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

  long long ans = 0;

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

  cout << ans << endl;

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

#include <algorithm>
#include <cstdio>
using namespace std;

#define N 200005

int m, n, a[N];
long long ok1[5 * N], ok2[5 * N];
long long ans = 0;

inline int read() {
  int s = 0, t = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-')
    c = getchar();
  if (c == '-')
    c = getchar(), t = -1;
  while (c >= '0' && c <= '9')
    s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
  return s * t;
}

int main(void) {
  m = read();
  for (int i = 1; i <= m; i++) {
    ;
    a[i] = read();
    if (a[i] >= m)
      continue;
    ok1[m + a[i] + i]++;
    ok2[m + i - a[i]]++;
  }
  for (int i = m; i <= 4 * m; i++) {
    ans += ok1[i] * ok2[i] * 1ll;
  }
  printf("%lld\\n", ans);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;

#define N 200005

int m, n, a[N];
long long ok1[2 * N], ok2[2 * N];
long long ans = 0;

inline int read() {
  int s = 0, t = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-')
    c = getchar();
  if (c == '-')
    c = getchar(), t = -1;
  while (c >= '0' && c <= '9')
    s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
  return s * t;
}

int main(void) {
  m = read();
  for (int i = 1; i <= m; i++) {
    ;
    a[i] = read();
    if (a[i] >= m)
      continue;
    ok1[m + a[i] + i]++;
    ok2[m + i - a[i]]++;
  }
  for (int i = m; i <= 2 * m; i++) {
    ans += ok1[i] * ok2[i] * 1ll;
  }
  printf("%lld\\n", ans);
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
void sort2array(double *a, long *b, long n) {
  for (int i = 0; i < n; i++) {
    b[i] = i;
  }
  sort(b, b + n, [a](long i, long j) -> bool { return a[i] < a[j]; });
}
// bを何回足せばaを超えるか
int wtover(int a, int b) {
  if (a % b > 0)
    return (a / b) + 1;
  else
    return a / b;
}
const int INF = 100000001, ID_MAX = 20;
int bi_e[ID_MAX] = {0};
// 2進数表示したときの最高桁
int bi_max(long n) {
  int m = 0;
  for (m; pow(2, m) <= n; m++)
    ;
  m = m - 1;
  return m;
}
// bi_eに二進数表示したやつを代入
void bi_exs(long n) {
  rep(i, ID_MAX) { bi_e[i] = 0; }
  if (n < pow(2, ID_MAX)) {
    int m = bi_max(n);
    long l;
    for (m; m > -1; m--) {
      l = pow(2, m);
      bi_e[m] = n / l;
      n = n % l;
    }
  }
}
// x^n mod m (nが負の時は０)
long myPow(long x, long n, long m) {
  if (n < 0)
    return 0;
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return myPow(x * x % m, n / 2, m);
  else
    return x * myPow(x, n - 1, m) % m;
}
const int N = 200001;
int a[N], n, dp[N];

int main() {
  long int i, cou = 0;
  // 入力
  cin >> n;
  rep(i, n) cin >> a[i];
  // 処理
  /*rep(i,n){
      long res;
      if(i==0)res=0;
      else if(i>=a[i]){
          for(int j=i-a[i];j>=0;j--){
              if(i-j==a[j]+a[i])cou++;
          }
      }
  }*/
  long int r[n], l[n];
  rep(i, n) {
    l[i] = a[i] + i;
    r[i] = i - a[i];
  }
  sort(l, l + n);
  rep(i, n) {
    long *a = lower_bound(l, l + n, r[i]), *b = upper_bound(l, l + n, r[i]);
    if (*a == r[i])
      *b == r[i] ? cou = cou + (b - a) + 1 : cou = cou + (b - a);
  }
  // 出力
  cout << cou << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
void sort2array(double *a, long *b, long n) {
  for (int i = 0; i < n; i++) {
    b[i] = i;
  }
  sort(b, b + n, [a](long i, long j) -> bool { return a[i] < a[j]; });
}
// bを何回足せばaを超えるか
int wtover(int a, int b) {
  if (a % b > 0)
    return (a / b) + 1;
  else
    return a / b;
}
const int INF = 100000001, ID_MAX = 20;
int bi_e[ID_MAX] = {0};
// 2進数表示したときの最高桁
int bi_max(long n) {
  int m = 0;
  for (m; pow(2, m) <= n; m++)
    ;
  m = m - 1;
  return m;
}
// bi_eに二進数表示したやつを代入
void bi_exs(long n) {
  rep(i, ID_MAX) { bi_e[i] = 0; }
  if (n < pow(2, ID_MAX)) {
    int m = bi_max(n);
    long l;
    for (m; m > -1; m--) {
      l = pow(2, m);
      bi_e[m] = n / l;
      n = n % l;
    }
  }
}
// x^n mod m (nが負の時は０)
long myPow(long x, long n, long m) {
  if (n < 0)
    return 0;
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return myPow(x * x % m, n / 2, m);
  else
    return x * myPow(x, n - 1, m) % m;
}
const int N = 200001;
int a[N], n, dp[N];

int main() {
  int i, cou = 0;
  // 入力
  cin >> n;
  rep(i, n) cin >> a[i];
  // 処理
  rep(i, n) {
    long res;
    if (i == 0)
      res = 0;
    else if (i >= a[i]) {
      for (int j = i - a[i]; j >= 0; j--) {
        if (i - j == a[j] + a[i])
          cou++;
      }
    }
  }
  // 出力
  cout << cou << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
typedef long long ll;
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 long long INF = 1LL << 60;
const int inf = 1001001001;

int main() {

  ll N, ans = 0, same = 0;
  cin >> N;
  vector<ll> h(N);
  vector<ll> datasum(N);
  vector<ll> datadif(N);
  ll max = 0;
  rep(i, N) {
    cin >> h.at(i);
    datasum.at(i) = (i + 1) + h.at(i);
    datadif.at(i) = (i + 1) - h.at(i);
    if (max < datasum.at(i))
      max = datasum.at(i);
    if (max < datadif.at(i))
      max = datadif.at(i);
    if (datasum.at(i) == datadif.at(i))
      same++;
  }

  map<ll, ll> number;

  rep(i, N) {
    if (number.count(datasum.at(i))) {
      number[datasum.at(i)] = number.at(datasum.at(i)) + 1;
    } else {
      number[datasum.at(i)] = 1;
    }
  }

  rep(i, N) {
    if (datadif.at(i) < 0)
      continue;
    else if (number.count(datadif.at(i))) {
      ans += number.at(datadif.at(i));
    }
  }

  cout << ans - same << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
typedef long long ll;
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 long long INF = 1LL << 60;
const int inf = 1001001001;

int main() {

  ll N, ans = 0, same = 0;
  cin >> N;
  vector<ll> h(N);
  vector<ll> datasum(N);
  vector<ll> datadif(N);
  ll max = 0;
  rep(i, N) {
    cin >> h.at(i);
    datasum.at(i) = (i + 1) + h.at(i);
    datadif.at(i) = (i + 1) - h.at(i);
    if (max < datasum.at(i))
      max = datasum.at(i);
    if (max < datadif.at(i))
      max = datadif.at(i);
    if (datasum.at(i) == datadif.at(i))
      same++;
  }
  vector<ll> number(max, 0);

  rep(i, N) { number.at(datasum.at(i) - 1)++; }

  rep(i, N) {
    if (datadif.at(i) - 1 < 0)
      continue;
    if (number.at(datadif.at(i) - 1) != 0) {
      ans += number.at(datadif.at(i) - 1);
    }
  }

  cout << ans - same << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long int
#define fo(i, n) for (i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
#define endl "\\n"
#define MAXN 1000002
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
const ll mod = 1e9 + 7;

void solve() {
  int n;
  ll x;
  cin >> n;
  vector<ll> V;
  unordered_map<ll, ll> um;
  for (int i = 0; i < n; i++) {
    cin >> x;
    V.pb(x);
    ll val = i + x;
    um[val]++;
  }
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    ll val = i - V[i];
    cnt = cnt + um[val];
  }
  cout << cnt << endl;
}

int main() { fastio solve(); }


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long int
#define fo(i, n) for (i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
#define endl "\\n"
#define MAXN 1000002
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
const ll mod = 1e9 + 7;

void solve() {
  int n;
  ll x;
  cin >> n;
  vector<ll> V;
  unordered_map<int, ll> um;
  for (int i = 0; i < n; i++) {
    cin >> x;
    V.pb(x);
    um[i] = V[i];
  }
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int diff = j - i;
      int height = V[i] + V[j];
      if (diff == height)
        cnt++;
    }
  }
  cout << cnt << endl;
}

int main() { fastio solve(); }
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> P;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;

const ll MAXN = 300000;

ll bit[MAXN];
void add(ll a, ll w) {
  for (ll x = a; x <= MAXN; x += (x & -x))
    bit[x] += w;
}
ll sum(ll a) {
  ll ret = 0;
  for (ll x = a; x > 0; x -= (x & -x))
    ret += bit[x];
  return ret;
}
// Comb

int main() {
  ll N;
  cin >> N;
  vec d(N + 1);
  REP(i, 1, N + 1) cin >> d[i];
  ll ans = 0;
  REP(i, 1, N + 1) {
    ll K = d[i] + i;
    ll S = i - d[i];
    if (S > 1 && S < MAXN) {
      ans += (sum(S) - sum(S - 1));
    }
    if (K < MAXN)
      add(K, 1);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> P;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
vec dx = {1, 0, -1, 0};
vec dy = {0, 1, 0, -1};

int main() {
  ll N;
  cin >> N;
  vec d(N + 1);
  REP(i, 1, N + 1) cin >> d[i];
  unordered_multiset<ll> SET;
  ll ans = 0;
  REP(i, 1, N + 1) {
    ll K = d[i] + i;
    ll S = i - d[i];
    if (i >= 2)
      if (S >= 0) {
        ans += SET.count(S);
      }
    SET.insert(K);
  }
  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++)
using namespace std;
using ll = long long;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  vector<ll> L(N);
  rep(i, N) L[i] = A[i] + i;
  map<ll, ll> M;
  rep(i, N) { M[L[i]]++; }
  ll ans = 0;
  rep(i, N) {
    ll R = i - A[i];
    ans += M[R];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

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; i++) {
    ll k = 1;
    for (ll j = i - A[i] - 1; j >= 0; j--) {
      if (A[j] == k)
        ans++;
      k++;
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
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;
  map<long long, long long> b, c;
  for (long long i = 0; i < n; i++) {
    cin >> x;
    b.insert(make_pair(x + i, 0));
    b.insert(make_pair(i - x, 0));
    sum += b[x + i] + c[i - x];
    b[i - x]++;
    c[x + i]++;
  }
  cout << sum;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
long long b[200002], c[200002];
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;
  long long a[n + 2];
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    sum += b[a[i] + i] + c[i - a[i]];
    b[i - a[i]]++;
    c[a[i] + i]++;
  }
  cout << sum;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>

using ll = long long;

ll n;

std::map<ll, ll> mapa;

int main() {
  std::cin >> n;
  ll solutions = 0;
  for (ll i = 0, tmp; i < n; ++i) {
    std::cin >> tmp;
    solutions += mapa[i - tmp];
    mapa[i + tmp] += 1;
  }
  std::cout << solutions;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

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

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

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f; // 1061109567
const int INF = 0x7fffffff; // 2147483647
const int SUP = 0x80000000; //-2147483648
#define MAX 1000000
ll a[MAX] = {0}, b[MAX] = {0};
int main() {
  int n;
  int t;
  ll sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> t;
    if (i + t <= n)
      a[i + t]++;
    if (i - t >= 1)
      b[i - t]++;
  }
  for (int i = 1; i <= n; i++) {
    sum += a[i] * b[i];
  }
  cout << sum;
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
#include <map>
using namespace std;
int f[250005];
int n;
int a[200005];
long long tot;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    f[i - a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    tot += f[i + a[i]];
  }
  cout << tot;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

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

  long long ans = 0;
  map<int, int> mp;

  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    int target = i - A;
    if (target >= 0) {
      ans += mp[target];
    }
    mp[A + i] = mp[A + i] + 1;
  }

  cout << ans << endl;
}

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() {
  long long N;
  cin >> N;

  long long ans = 0;
  vector<int> chk(1e9, 0);

  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    int target = i - A;
    if (target >= 0) {
      ans += chk.at(target);
    }
    chk.at(A + i) += 1;
  }

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

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

int main() {
  int N;
  cin >> N;
  int64_t ans = 0;
  vector<int> count(200001, 0);
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    if (i > A)
      ans += count[i - A];
    if (i + A < 200001)
      count[i + A]++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  int64_t ans = 0;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N; i++)
    for (int 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>
using namespace std;

#define int long long
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;
}
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define COUT(x) cout << x << endl;
#define PB(x) push_back(x)
#define MP make_pair
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

signed main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  int res = 0;
  map<int, int> map;
  for (int i = 1; i <= n; i++) {
    map[a[i] + i]++;
  }
  for (int i = 1; i <= n; i++) {
    if (i - a[i] > 0)
      res += map[i - a[i]];
  }
  cout << res << endl;
}

Error: TLE

Fixed submission:

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

#define int long long
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;
}
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define COUT(x) cout << x << endl;
#define PB(x) push_back(x)
#define MP make_pair
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

signed main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  int res = 0;
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (a[i] + a[j] == (j - i))
        res++;
    }
  }
  cout << res << endl;
}