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

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

signed main(void) {
  int N;
  cin >> N;
  vector<int> Set1, Set2;
  for (int i = 1; i <= N; i++) {
    int a;
    cin >> a;
    Set1.push_back(i - a);
    Set2.push_back(i + a);
  }

  sort(Set1.begin(), Set1.end());
  sort(Set2.begin(), Set2.end());

  int ans = 0;

  for (auto X : Set2) {

    int itr1 = lower_bound(Set1.begin(), Set1.end(), X) - Set1.begin();
    int itr2 = upper_bound(Set1.begin(), Set1.end(), X) - Set1.begin();

    ans += itr2 - itr1;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

signed main(void) {
  int N;
  cin >> N;
  multiset<int> Set1, Set2;
  for (int i = 1; i <= N; i++) {
    int a;
    cin >> a;
    Set1.insert(i - a);
    Set2.insert(i + a);
  }

  int ans = 0;

  for (auto X : Set2) {

    auto itr1 = Set1.lower_bound(X);
    auto itr2 = Set1.upper_bound(X);

    ans += distance(itr1, itr2);
  }

  cout << ans << endl;

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

#include <iostream>
#include <map>
using namespace std;
map<int, int> f;
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;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int sum = 0;
  int n, i, j;
  int arr[200005];
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
  }
  for (j = 1; j <= n - 1; j++) {
    for (i = 1; i + j <= n; i++) {
      if (j == arr[i] + arr[i + j]) {
        sum++;
      }
    }
  }

  printf("%d", sum);
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vll = vector<vl>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1e9 + 7;
const ll INF = 2e15;
template <class T> void print(const T &t) { cout << t << endl; }
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;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll n;
  cin >> n;
  vl a(n);
  vl L(200007, 0);
  vl R(200007, 0);
  rep(i, n) {
    cin >> a[i];
    ll x = i + 1 + a[i], y = i + 1 - a[i];
    if (0 < x && x < 200007) {
      L[x]++;
    }
    if (0 < y && y < 200007) {
      R[y]++;
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans += L[i] * R[i];
  }
  print(ans);
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vll = vector<vl>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1e9 + 7;
const ll INF = 2e15;
template <class T> void print(const T &t) { cout << t << endl; }
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;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll n;
  cin >> n;
  vl a(n);
  vl L(200007, 0);
  vl R(200007, 0);
  rep(i, n) {
    cin >> a[i];
    L[i + 1 + a[i]]++;
    R[i + 1 - a[i]]++;
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans += L[i] * R[i];
  }
  print(ans);
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;
  int tmp;
  long long count = 0;
  vector<int> H;
  int res;
  // vector < vector<int>> A(N, vector<int>(N));
  map<int, int> Hmap;

  for (int i = 0; i < N; ++i) {
    cin >> tmp;
    H.push_back(tmp);
  }

  for (int i = 0; i < N; ++i) {

    int sa = i - H[i];
    count += Hmap[sa];
    int wa = H[i] + i;
    Hmap[wa]++;
  }

  cout << count << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <vector>

// #include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;
  int tmp;
  long long count = 0;
  vector<int> H;
  int res;
  // vector < vector<int>> A(N, vector<int>(N));

  for (int i = 0; i < N; ++i) {
    cin >> tmp;
    H.push_back(tmp);
  }

  for (int i = 0; i < N; ++i) {

    for (int j = i + 1; j < N; ++j) {
      res = H[i] + H[j];
      if (res == abs(j - i)) {
        count++;
      }
    }
  }

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

#include <iostream>

long long ckc[1000000], ckt[1000000], res;

int main() {
  long long n;
  std::cin >> n;
  long long a[n + 1];
  for (long long i = 1; i <= n; i++) {
    std::cin >> a[i];
    if ((a[i] + i >= 0) && (a[i] + i <= 2 * 100000))
      ckc[a[i] + i]++;
    if ((i - a[i] >= 0) && (i - a[i] <= 2 * 100000))
      ckt[i - a[i]]++;
  }
  for (long long i = 0; i <= 2 * 100000; i++) {
    res += ckc[i] * ckt[i];
  }
  std::cout << res;
}

Error: WA

Fixed submission:

#include <iostream>

long long ckc[1000000], ckt[1000000], res;

int main() {
  long long n;
  std::cin >> n;
  long long a[n];
  for (long long i = 1; i <= n; i++) {
    std::cin >> a[i];
    if ((a[i] + i >= 0) && (a[i] + i <= 2 * 100000))
      ckc[a[i] + i]++;
    if ((i - a[i] >= 0) && (i - a[i] <= 2 * 100000))
      ckt[i - a[i]]++;
  }
  for (long long i = 0; i <= 2 * 100000; i++) {
    res += ckc[i] * ckt[i];
  }
  std::cout << res;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];

  long long ans = 0;
  map<int, int> num_appear;
  set<int> s;
  REP(i, N) {
    auto itr = s.lower_bound(i - A[i]);
    if (itr != s.end() and *itr == i - A[i])
      ans += num_appear[*itr];
    s.insert(i + A[i]);
    ++num_appear[i + A[i]];
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];

  long long ans = 0;
  multiset<int> s;
  REP(i, N) {
    auto itr_s = s.lower_bound(i - A[i]);
    auto itr_l = s.upper_bound(i - A[i]);
    ans += distance(itr_s, itr_l);
    s.insert(i + A[i]);
  }
  cout << ans << endl;

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

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  ll ans = 0;
  // 連想配列を使う map
  map<int, int> mp;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i - a[i] > 0)
      ans += mp[i - a[i]];
    mp[i + a[i]]++;
  }
  cout << ans << endl;
}


Error: -6
free(): invalid pointer


Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  int ans = 0;
  // 連想配列を使う map
  map<int, int> mp;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i - a[i] > 0)
      ans += mp[i - a[i]];
    mp[i + a[i]]++;
  }
  // 二人の持つ番号の差の絶対値が、二人の身長の和に等しい
  // 条件を満たすペアは何通り？
  // 選ぶペアのうち、数字が小さいのをi,数字が大きいのをjとすると
  // j-i=Ai+Aj j-Aj=i+Ai
  // 0<=j-Aj<2*10^5 だから、左辺の範囲内の数値だけ求めておけば良い
  // map使っていきましょう
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
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 ll INF = 1LL << 60;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  map<ll, ll> b;
  for (int i = 1; i <= n; i++) {
    if (i > a[i])
      b[i - a[i]]++;
  }
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    if (b.count(a[i] + i)) {
      sum += b[a[i] + i];
    }
  }
  cout << sum << 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;
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 ll INF = 1LL << 60;

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  vector<int> b(1000000000 + 1, 0);
  for (int i = 1; i <= n; i++) {
    if ((i > a[i]))
      b[i - a[i]]++;
  }
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] + i <= 1000000000) {
      sum += b[a[i] + i];
    }
  }
  cout << sum << endl;

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <typeinfo>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  long long n;
  cin >> n;
  vector<long long> a(n), p(n), m(n);
  map<long long, long long> ma;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    ma[i + 1 + a[i]]++;
  }

  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    ans += ma[i + 1 - a[i]];
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <typeinfo>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> p(n);
  vector<int> m(n);
  map<int, vector<int>> map_p;
  map<int, vector<int>> map_m;
  vector<int> x(1, 0);
  map<int, vector<int>>::iterator it;
  for (int i = 0; i < n; i++) {
    p[i] = (i + 1) + a[i];
    m[i] = (i + 1) - a[i];
    it = map_p.find(p[i]);
    if (it != map_p.end()) {
      (map_p.at(p[i])).push_back(i + 1);
    } else {
      x[0] = i + 1;
      map_p.insert(make_pair(p[i], x));
    }
    it = map_m.find(m[i]);
    if (it != map_m.end()) {
      (map_m.at(m[i])).push_back(i + 1);
    } else {
      x[0] = i + 1;
      map_m.insert(make_pair(m[i], x));
    }
  }

  sort(p.begin(), p.end());
  sort(m.begin(), m.end());
  int npa, nma;
  vector<int> z;
  int count = 0;

  vector<int>::iterator itv;

  for (int num = 1; num < n + 1; num++) {
    npa = num + a[num - 1];
    nma = num - a[num - 1];

    // nma = p[i]はあるか
    itv = lower_bound(p.begin(), p.end(), nma);
    if (itv != p.end() and *itv == nma) {
      z = map_p.at(*itv);
      for (vector<int>::iterator w = z.begin(); w != z.end(); w++) {
        if (*w < n) {
          count += 1;
        }
      }
    }

    itv = lower_bound(m.begin(), m.end(), npa);
    if (itv != m.end() and *itv == npa) {
      z = map_m.at(*itv);
      for (vector<int>::iterator w = z.begin(); w != z.end(); w++) {
        if (*w > n) {
          count += 1;
        }
      }
    }
  }

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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) { cin >> A.at(i); }
  ll cnt = 0;
  map<ll, ll> x;
  rep(i, N) {
    ll query = A.at(i) - i;
    if (x.find(query) != x.end()) {
      cnt += x[query];
    }
    ll key = -(A.at(i) + i);
    if (x.find(key) != x.end()) {
      x[key] += 1;
    } else {
      x[key] = 1;
    }
  }
  cout << cnt << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) { cin >> A.at(i); }
  ll cnt = 0;
  rep(i, N) {
    rep2(j, i + A.at(i), N) {
      if ((j - i) == (A.at(i) + A.at(j))) {
        cnt += 1;
      }
    }
  }
  cout << cnt << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 *    author:  math2do
 *    created: 04.05.2020 04:33:38 IST
 **/

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  map<int, int> right;
  for (int i = n - 1; i >= 0; i--) {
    int pos = i - arr[i];
    right[pos]++;
  }
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    int pos = i - arr[i];
    right[pos]--;
    int at = arr[i] + i;
    auto it = right.find(at);
    if (it != right.end()) {
      ans += it->second;
    }
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

/**
 *    author:  math2do
 *    created: 04.05.2020 04:33:38 IST
 **/

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  vector<map<int, int>> right(n);
  int key = n - 1 - arr[n - 1];
  right[n - 1][key] = 1;
  for (int i = n - 2; ~i; i--) {
    right[i] = right[i + 1];
    int pos = i - arr[i];
    right[i][pos]++;
  }
  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    int at = arr[i] + i;
    auto it = right[i + 1].find(at);
    if (it != right[i + 1].end()) {
      ans += it->second;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define lque queue<ll>
#define lpque priority_queue<ll>
#define dlpque priority_queue<ll, lvector, greater<ll>>
#define P pair<ll, ll>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define print(a) cout << (a) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, ans = 0, val, num;
  cin >> n;
  lvector A(n, 0), v;
  map<ll, lvector> mp;
  rep(i, n) {
    cin >> A[i];
    mp[i - A[i]].emplace_back(i);
  }
  for (ll i = 0; i < n; i++) {
    val = A[i] + i;
    if (mp[val].size()) {
      num =
          mp[val].size() - (lower_bound(ALL(mp[val]), i + 1) - mp[val].begin());
      ans += num;
    }
  }
  print(ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define lque queue<ll>
#define lpque priority_queue<ll>
#define dlpque priority_queue<ll, lvector, greater<ll>>
#define P pair<ll, ll>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define print(a) cout << (a) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, ans = 0;
  cin >> n;
  lvector A(n, 0);
  map<ll, lvector> mp;
  rep(i, n) {
    cin >> A[i];
    mp[i - A[i]].emplace_back(i);
  }
  for (ll i = 0; i < n; i++) {
    ll val = A[i] + i;
    if (mp[val].size()) {
      lvector v = mp[val];
      ll num = v.size() - (lower_bound(ALL(v), val) - v.begin());
      ans += num;
    }
  }
  print(ans);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FRE freopen("in.txt", "r", stdin);
#define endl "\\n"
#define PI acos(-1.0)
typedef long long LL;
typedef pair<int, int> PII;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10, M = 1010;

LL a[N];
map<LL, LL> cnt;

void solve() {
  int n;
  cin >> n;
  LL res = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    res += cnt[i - a[i]];
    cnt[a[i] + i]++;
  }
  cout << res << endl;
}

int main() {
  IOS;
  int t;
  solve();
  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FRE freopen("in.txt", "r", stdin);
#define endl "\\n"
#define PI acos(-1.0)
typedef long long LL;
typedef pair<int, int> PII;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10, M = 1010;

LL a[N];
map<LL, LL> cnt;

void solve() {
  int n;
  cin >> n;
  LL res = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    res += cnt[i - a[i]];
    cnt[a[i] + i]++;
  }
  cout << res << endl;
}

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

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

using namespace std;

int main(int argc, char *argv[]) {
  long long N;
  cin >> N;

  vector<long long> a(N);
  vector<long long> score(N);
  for (int i = 0; i < N; ++i) {
    long long tmp;
    cin >> tmp;
    a[i] = tmp;
    score[i] = i - tmp;
  }

  vector<long long> s = score;
  sort(s.begin(), s.end());

  long long ret = 0;
  for (int i = 0; i < N; ++i) {
    long long cur = i + a[i];
    vector<long long>::iterator j = lower_bound(s.begin(), s.end(), i + a[i]);
    vector<long long>::iterator k = upper_bound(s.begin(), s.end(), i + a[i]);
    if (j == s.end() || *j > cur)
      continue;
    if (k == s.end())
      ret += distance(j, s.end());
    else
      ret += distance(j, k);
  }

  cout << ret << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main(int argc, char *argv[]) {
  long long N;
  cin >> N;

  vector<long long> a(N);
  vector<long long> score(N);
  for (int i = 0; i < N; ++i) {
    long long tmp;
    cin >> tmp;
    a[i] = tmp;
    score[i] = i - tmp;
  }

  vector<long long> s = score;
  sort(s.begin(), s.end());

  long long ret = 0;
  for (int i = 0; i < N; ++i) {
    long long cur = i + a[i];
    vector<long long>::iterator j = lower_bound(s.begin(), s.end(), i + a[i]);
    while (true) {
      if (j == s.end() || *j > cur)
        break;
      if (cur == *j)
        ++ret;
      ++j;
    }
  }

  cout << ret << endl;

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

#include <bits/stdc++.h>
#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++)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using pii = pair<int, int>;

// const ll INF = 1e18L + 5;
// const int INF = 1e9 + 5;
// const double pi = 3.14159265358979323846;

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

  int n;
  cin >> n;
  unordered_map<int, int> mp;

  vector<int> h(n);
  ll ans = 0;
  rep(i, n) cin >> h[i];
  rep(j, n) {
    if (j - h[j] >= 0)
      mp[j - h[j]]++;
  }
  rep(i, n) { ans += mp[i + h[i]]; }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#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++)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using pii = pair<int, int>;

// const ll INF = 1e18L + 5;
// const int INF = 1e9 + 5;
// const double pi = 3.14159265358979323846;

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

  int n;
  cin >> n;
  vector<int> dp(2000001);

  vector<int> h(n);
  ll ans = 0;
  rep(i, n) cin >> h[i];
  rep(j, n) {
    if (j - h[j] >= 0)
      dp[j - h[j]]++;
  }
  rep(i, n) { ans += dp[i + h[i]]; }
  cout << ans << endl;
}