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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define Rep(i, n) for (intl i = 0, nn = (intl)(n); i < nn; ++i)
#define Rep1(i, n) for (intl i = 1, nn = (intl)(n) + 1; i < nn; ++i)
#define vec vector
#define fi first
#define se second
#define pub push_back
#define pob pop_back
#define SortA(v) sort(v.begin(), v.end())
#define SortD(v) sort(v.rbegin(), v.rend())
#define All(a) (a).begin(), (a).end()
#define Input(a)                                                               \\
  for (intl ii = 0, nn = (intl)(a.size()); ii < nn; ii++) {                    \\
    cin >> (a).at(ii);                                                         \\
  }
#define Input2(a, b)                                                           \\
  for (intl ii = 0, nn = (intl)(a.size()); ii < nn; ii++) {                    \\
    cin >> (a).at(ii);                                                         \\
    cin >> (b).at(ii);                                                         \\
  }
#define mp make_pair
#define mt make_tuple
using intl = int64_t;
using vi = vec<intl>;
using vvi = vec<vi>;
using pi = pair<intl, intl>;
using vpi = vec<pi>;
using si = set<intl>;
using vsi = vec<si>;
using mii = map<intl, intl>;
using vmii = vec<mii>;
vec<intl> dx = {0, 1, 0, -1, 1, 1, -1, -1};
vec<intl> dy = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr intl INF = 9223372036854775807;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  intl N;
  cin >> N;
  intl A;
  mii K;
  intl ans = 0;
  Rep1(i, N) {
    cin >> A;
    if (i - A > 0) {
      ans += K[i - A];
    }
    K[A + i]++;
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define Rep(i, n) for (intl i = 0; i < (intl)(n); i++)
#define Rep1(i, n) for (intl i = 1; i < (intl)(n) + 1; i++)
#define vec vector
#define fi first
#define se second
#define pub push_back
#define pob pop_back
#define SortA(v) sort(v.begin(), v.end())
#define SortD(v) sort(v.rbegin(), v.rend())
#define All(a) (a).begin(), (a).end()
using intl = int64_t;
using vi = vec<intl>;
using vvi = vec<vi>;
using pi = pair<intl, intl>;
using vpi = vec<pi>;
vec<intl> dx = {0, 1, 0, -1, 1, 1, -1, -1};
vec<intl> dy = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr intl INF = 9223372036854775807;

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

  vector<tuple<intl, intl, intl>> attendant(N);
  Rep1(i, N) {
    intl A;
    cin >> A;
    attendant.at(i - 1) = make_tuple(i - A, A, i);
  }

  SortA(attendant);

  intl ans = 0;
  for (auto a : attendant) {
    intl i = get<2>(a);
    intl A = get<1>(a);
    auto L = lower_bound(All(attendant), make_tuple(i + A, 0, 0));
    auto U = upper_bound(All(attendant), make_tuple(i + A, INF, INF));
    ans += distance(L, U);
    // cout << get<0>(a) << ' '<<get<1>(a) << ' '<<get<2>(a) << endl;
  }

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// #include<bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1000000000000000000;
const int mod = 1000000007;
int test[12345678];
signed main() {
  vector<vector<int>> v(123456);
  map<int, int> cost;
  int n, m, hi, a, b, c, cnt = 0, ans = 0;
  string s, t;
  cin >> n;
  for (int h = 0; h < n; h++) {
    cin >> a;
    test[h] = a;
    cost[a + h]++;
  }
  for (int h = 0; h < n; h++) {
    if (h - test[h] >= 0) {
      ans += cost[h - test[h]];
    }
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// #include<bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1000000000000000000;
const int mod = 1000000007;
int test[12345678], cost[12345678];
signed main() {
  vector<vector<int>> v(123456);
  map<string, int> mn;
  int n, m, hi, a, b, c, cnt = 0, ans = 0;
  string s, t;
  cin >> n;
  for (int h = 0; h < n; h++) {
    cin >> a;
    test[h] = a;
    cost[a + h]++;
  }
  for (int h = 1; h < n; h++) {
    if (h - test[h] >= 0) {
      ans += cost[h - test[h]];
    }
  }
  cout << ans << endl;
  return 0;
}
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++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using ll = long long;
const long long INF = 1LL << 60;
const int MOD = 1e9 + 7;

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() {

  ll N;
  cin >> N;
  std::vector<ll> A(N);

  rep(i, N) { cin >> A.at(i); }

  std::vector<ll> init(N);

  rep(i, N) { init.at(i) = -A.at(i) - i; }

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

  ll ans = 0;
  rep(i, N) {
    ll key = A.at(i) - i;

    auto iter_lower = lower_bound(init.begin(), init.end(), key);
    auto iter_upper = upper_bound(init.begin(), init.end(), key);

    // インデックスを取得
    long idx_lower =
        distance(init.begin(), iter_lower); // vec[idx_lower] = vec[7] = 6
    long idx_upper =
        distance(init.begin(), iter_upper); // vec[idx_upper] = vec[8] = 8

    ans += (idx_upper - idx_lower);
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using ll = long long;
const long long INF = 1LL << 60;
const int MOD = 1e9 + 7;

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() {

  ll N;
  cin >> N;
  std::vector<ll> A(N);

  rep(i, N) { cin >> A.at(i); }

  std::vector<ll> init(N);

  rep(i, N) { init.at(i) = -A.at(i) - i; }

  sort(init.begin(), init.end());
  reverse(init.begin(), init.end());

  ll ans = 0;
  rep(i, N) {
    rep(j, N) {
      if (A.at(i) - i == init.at(j)) {
        ans++;
      } else if (A.at(i) - i > init.at(j)) {
        break;
      }
    }
  }

  cout << ans << endl;

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

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

ll r(ll x, ll y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return r(x, y / 2) * r(x, y / 2) % mod;
  else
    return x * r(x, (y - 1) / 2) % mod * r(x, (y - 1) / 2) % mod;
}

int main() {
  int n;
  cin >> n;
  vector<ll> wa(n, 0);
  map<ll, ll> sa;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    wa[i] += (i + a);
    if (i - a >= 0) {
      sa[i - a]++;
    }
  }
  ll count = 0;
  for (int i = 0; i < n; i++) {
    count += sa[wa[i]];
  }
  printf("%lld\\n", count);
}

Error: 0

Fixed submission:

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

ll r(ll x, ll y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return r(x, y / 2) * r(x, y / 2) % mod;
  else
    return x * r(x, (y - 1) / 2) % mod * r(x, (y - 1) / 2) % mod;
}

int main() {
  int n;
  cin >> n;
  vector<ll> wa(n, 0);
  vector<int> sa(2000005, 0);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    wa[i] += (i + a);
    if (i - a >= 0) {
      sa[i - a]++;
    }
  }
  ll count = 0;
  for (int i = 0; i < n; i++) {
    count += sa[wa[i]];
  }
  printf("%lld\\n", count);
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long int64;
map<int, int> P;
map<int, int> M;

int main() {
  int N;
  cin >> N;
  int tmp;
  int64 ans = 0;
  for (int i = 0; i < N; ++i) {
    cin >> tmp;
    if (tmp < 200000) {
      ++P[i + tmp];
      if (i - tmp > 0)
        ++M[i - tmp];
    }
  }
  for (int i = 1; i < N; ++i) {
    ans += (int64)P[i] * M[i];
  }
  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long int64;
vector<pair<int, int>> vpii;

int main() {
  int N;
  cin >> N;
  int tmp;
  int64 ans = 0;
  vpii.reserve(N);
  for (int i = 0; i < N; ++i) {
    cin >> tmp;
    if (tmp < 200000)
      vpii.push_back(make_pair(i, tmp));
  }
  int len = vpii.size();
  for (int i = 0; i < len - 1; ++i) {
    tmp = vpii[i].first + vpii[i].second;
    for (int j = i + 1; j < len; ++j) {
      if (vpii[j].first == vpii[j].second + tmp)
        ++ans;
    }
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
const int U = 2e5 + 5;
using namespace std;
using ll = long long;

int main() {
  int N;
  cin >> N;
  ll a[U];
  map<ll, int> m;
  for (int i = 0; i < N; i++)
    cin >> a[i];
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ans += m[i + 1 - a[i]];
    m[i + 1 + a[i]]++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

#include <algorithm>
#include <map>
#include <stdio.h>
#include <vector>
using namespace std;

typedef long long ll;

map<ll, vector<ll>> M;
ll a[222222], t[222222];

int main(void) {
  ll n, i;

  scanf("%lld", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    t[i] = i - a[i];
  }

  for (i = 1; i <= n; i++)
    if (t[i] > 0)
      M[t[i]].push_back(i);

  ll ans = 0;
  for (i = 1; i <= n; i++) {
    ll p = i + a[i];

    ll it = upper_bound(M[p].begin(), M[p].end(), i) - M[p].begin();

    ans += M[p].size() - it;
  }

  printf("%lld", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <map>
#include <stdio.h>
#include <vector>
using namespace std;

typedef long long ll;

map<ll, vector<ll>> M;
ll a[222222], t[222222];

int main(void) {
  ll n, i;

  scanf("%lld", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    t[i] = i - a[i];
  }

  for (i = 1; i <= n; i++)
    if (t[i] > 0)
      M[t[i]].push_back(i);

  ll ans = 0;
  for (i = 1; i <= n; i++) {
    ll p = i + a[i];

    vector<ll> v = M[p];

    ll it = upper_bound(v.begin(), v.end(), i) - v.begin();

    ans += v.size() - it;
  }

  printf("%lld", ans);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n;
  cin >> n;
  vector<ll> data(n);
  vector<ll> a(1000000, 0);
  ll count = 0;
  map<ll, ll> hindo;
  for (int i = 0; i < n; i++) {
    cin >> data[i];
  }
  for (int i = 0; i < n; i++) {
    if (n >= data[i])
      a[data[i] + i]++;
  }
  for (int i = 0; i < n; i++) {
    if (i > data[i])
      count += a[i - data[i]];
  }
  cout << count << 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;
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  vector<ll> data(n);
  vector<ll> a(1000000000, 0);
  ll count = 0;
  map<ll, ll> hindo;
  for (int i = 0; i < n; i++) {
    cin >> data[i];
  }
  for (int i = 0; i < n; i++) {
    a[data[i] + i]++;
  }
  for (int i = 0; i < n; i++) {
    if (i > data[i])
      count += a[i - data[i]];
  }
  cout << count << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n;
  cin >> n;
  vector<ll> a(n);
  map<ll, ll> as;
  rep(i, n) {
    cin >> a[i];
    as[a[i] - i]++;
  }
  ll ans = 0;
  rep(i, n) {
    ll v = (a[i] - i + i * 2) * -1;
    as[a[i] - i] = max(0LL, as[a[i] - i]);
    ans += as[v];
  }
  cout << ans << 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##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n;
  cin >> n;
  vector<ll> a(n);
  multiset<ll> as;
  rep(i, n) {
    cin >> a[i];
    as.insert(a[i] - i);
  }
  ll ans = 0;
  rep(i, n) {
    ll v = (a[i] - i + i * 2) * -1;
    auto it = as.lower_bound(a[i] - i);
    as.erase(it);
    ans += as.count(v);
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
typedef long long ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  ll ans = 0;
  map<int, int> m;
  for (int j = 1; j <= n; j++) {
    ans += m[j - a[j]];
    m[a[j] + j]++;
  }

  cout << ans << endl;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
typedef long long ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  ll ans = 0;
  map<int, int> m;
  for (int j = 1; j <= n; j++) {
    ans += m[j - a[j]];
    m[a[j] + j]++;
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int N;
  cin >> N;
  ll A[N + 1];
  for (int i = 1; i < N + 1; i++)
    cin >> A[i];
  map<int, int> mp; // keyにA[i] + iの値を、valueに、j番目までで出てきたA[i] +
                    // iの個数を代入する
  ll ans = 0;
  for (int i = 1; i < N + 1; i++) {
    int right = i - A[i]; // jの値とみる
    ans += mp[right];
    int left = i + A[i]; // iの値とみる
    mp[left]++; // leftに該当するkeyがない場合は自動で生成されて、1が代入される。
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)

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

#include <iostream>
#include <unordered_map>

#define ll long long

using namespace std;

int main() {
  ll n, cnt = 0;
  cin >> n;
  ll arr[n];
  unordered_map<ll, int> mp;

  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    arr[i] = x;
    mp[x + i + 1]++;
  }

  for (int i = 0; i < n; i++) {
    ll x = i + 1 - arr[i];
    if (mp[x])
      cnt += mp[x];
  }
  cout << cnt;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int arr[n], cnt = 0;
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  for (int i = 0; i < n; i++) {
    int range = i + arr[i];
    for (int j = range + 1; j < n; j++) {
      if (range == j - arr[j])
        cnt++;
    }
  }
  cout << cnt;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
typedef long long ll;
ll a[maxn];
map<ll, int> mp;
int main() {
  ll n;
  scanf("%d", &n);
  for (ll i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (i >= a[i])
      mp[i - a[i]]++;
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans += mp[a[i] + i];
  }
  printf("%lld\\n", ans);
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200005;
ll a[maxn];
ll l[maxn], r[maxn];
ll cnt[100030003];
ll cnt2[100030003];
int main() {
  int n;
  ll ma = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    l[i] = i + a[i];
    r[i] = i - a[i];
    cnt[l[i]]++;
    if (r[i] > 0)
      cnt2[r[i]]++;
    ma = max(l[i], ma);
  }
  ll ans = 0;
  for (ll i = 1; i <= ma; i++) {
    ans += cnt[i] * cnt2[i];
  }
  printf("%lld\\n", ans);
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const ll INF = 1LL << 60;
const ll MOD = ll(1e9) + 7;

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

  vector<ll> A(N);
  vector<ll> B(400001);
  vector<ll> C(400001);

  for (ll i = 0; i < N; i++) {
    cin >> A.at(i);

    if (A.at(i) <= 200000) {
      B.at(i + A.at(i))++;

      if ((i - A.at(i)) <= 400000 && (i - A.at(i)) >= 0) {
        C.at(i - A.at(i))++;
      }
    }
  }

  ll ans = 0;

  for (ll i = 0; i < B.size(); i++) {
    ans += B.at(i) * C.at(i);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

const ll INF = 1LL << 60;
const ll MOD = ll(1e9) + 7;

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

  vector<ll> A(N);
  vector<vector<ll>> B(210000);

  for (ll i = 0; i < N; i++) {
    cin >> A.at(i);

    if (A.at(i) <= 200000) {
      vector<ll> tmp;
      tmp.push_back(i);
      tmp.push_back(A.at(i));
    }
  }

  ll sum;
  ll ans = 0;

  for (ll i = 0; i < N; i++) {
    for (ll j = i + 1; j < N; j++) {
      sum = A.at(i) + A.at(j);

      if (sum == (j - i))
        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;

// int/long:            -2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long:      -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)

#define INF (1 << 30)
// 1,073,741,824
//= 536,870,912 *2

#define MOD 1000000007

#define Rep0(i, n) for (auto i = 0; i < n; i++)
#define Rep1(i, n) for (auto i = 1; i <= n; i++)
#define Sort(P) sort(P.begin(), P.end())
#define Rev(P) reverse(P.begin(), P.end())

int main() {
  int N;
  cin >> N;
  vector<int> A(N, 0);
  Rep0(i, N) cin >> A.at(i);
  // Rep0(i, N) {
  //       int AA=A.at(i);
  // vector<int> AA(500000, 0);
  // vector<D> AA;
  map<ll, int> AA;
  ll S = 0;
  Rep0(i, N) {
    ll ix = i - A.at(i);
    S += AA[ix];
    AA[A.at(i) + i]++;
  }
  cout << S << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

// int/long:		-2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)

#define INF (1 << 30)
// 1,073,741,824
//= 536,870,912 *2

#define MOD 1000000007

#define Rep0(i, n) for (auto i = 0; i < n; i++)
#define Rep1(i, n) for (auto i = 1; i <= n; i++)
#define Sort(P) sort(P.begin(), P.end())
#define Rev(P) reverse(P.begin(), P.end())

/*
struct	D {
        ll L;
        int N;
};
*/

// auto	F(const ll ix, const vector<D> &AA) {
int F(const ll &ix, const vector<vector<ll>> &AA) {
  int L = 0;
  int R = AA.size();
  while (R - L > 1) {
    int M = (L + R) / 2;
    // if (AA.at(M).L<=ix)
    if (AA.at(M).at(0) <= ix)
      L = M;
    else
      R = M;
  }
  return L;
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N, 0);
  Rep0(i, N) cin >> A.at(i);
  // Rep0(i, N) {
  //	int AA=A.at(i);
  // vector<int> AA(500000, 0);
  // vector<D> AA;
  vector<vector<ll>> AA;
  AA.push_back({0, 0});
  ll S = 0;
  Rep0(i, N) {
    ll ix = i - A.at(i);
    if (ix >= 0) {
      int IX = F(ix, AA);
      if (ix == AA.at(IX).at(0))
        S += AA.at(IX).at(1);
    }
    ll V = A.at(i) + i;
    int IX = F(V, AA);
    if ((IX < AA.size()) && (V == AA.at(IX).at(0)))
      AA.at(IX).at(1)++;
    else {
      IX++;
      if (IX < AA.size()) {
        reverse(AA.begin() + IX, AA.end());
        AA.push_back({V, 1});
        reverse(AA.begin() + IX, AA.end());
      } else {
        AA.push_back({V, 1});
      }
    }
  }
  cout << S << endl;
  return 0;
}