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

#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
int binary_search(vector<int> v, int n) { // sorted v, count v=n
  if (v.size() == 0)
    return 0;
  if (v[0] > n)
    return 0;
  if (v[v.size() - 1] < n)
    return 0;
  int l, r, mid, max, min;
  l = 0;
  r = v.size();
  if (v[v.size() - 1] == n) {
    max = v.size() - 1;
  } else {
    while (r - l > 1) {
      mid = (l + r) / 2;
      if (v[mid] <= n) { // v[l]<=n<v[r]
        l = mid;
      } else {
        r = mid;
      }
    }
    max = l; // max means v[all]<=n. all<=max
  }
  l = 0;
  r = v.size();
  if (v[0] == n) {
    min = 0;
  } else {
    while (r - l > 1) {
      mid = (l + r) / 2;
      if (n <= v[mid]) { // v[l]<n<=v[r]
        r = mid;
      } else {
        l = mid;
      }
    }
    min = r; // min means v[all]<=n. all>=min
  }
  // cout<<max<<" "<<min<<endl;
  return max - min + 1;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  long long int cnt = 0;
  vector<int> v;
  rep(i, n) {
    // count v==i-a[i]
    cnt += upper_bound(v.begin(), v.end(), i - a[i]) -
           lower_bound(v.begin(), v.end(), i - a[i]);
    // sorted_insert i+a[i]
    v.insert(std::lower_bound(v.begin(), v.end(), i + a[i]), i + a[i]);
    // binary_insert(v,i+a[i]);
    // rep(i,v.size())cout<<v[i]<<" ";
    // cout<<endl;
  }
  cout << cnt << endl;
  /*
  int k;
  cin>>k;
  binary_insert(a,k);
  rep(i,a.size())cout<<a[i]<<" ";
  cout<<endl;
  */

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
int binary_search(vector<int> v, int n) { // sorted v, count v=n
  if (v.size() == 0)
    return 0;
  if (v[0] > n)
    return 0;
  if (v[v.size() - 1] < n)
    return 0;
  int l, r, mid, max, min;
  l = 0;
  r = v.size();
  if (v[v.size() - 1] == n) {
    max = v.size() - 1;
  } else {
    while (r - l > 1) {
      mid = (l + r) / 2;
      if (v[mid] <= n) { // v[l]<=n<v[r]
        l = mid;
      } else {
        r = mid;
      }
    }
    max = l; // max means v[all]<=n. all<=max
  }
  l = 0;
  r = v.size();
  if (v[0] == n) {
    min = 0;
  } else {
    while (r - l > 1) {
      mid = (l + r) / 2;
      if (n <= v[mid]) { // v[l]<n<=v[r]
        r = mid;
      } else {
        l = mid;
      }
    }
    min = r; // min means v[all]<=n. all>=min
  }
  // cout<<max<<" "<<min<<endl;
  return max - min + 1;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  long long int cnt = 0;
  vector<int> v;
  rep(i, n) {
    // count v==i-a[i]
    cnt += binary_search(v, i - a[i]);
    // sorted_insert i+a[i]
    v.insert(std::lower_bound(v.begin(), v.end(), i + a[i]), i + a[i]);
    // binary_insert(v,i+a[i]);
    // rep(i,v.size())cout<<v[i]<<" ";
    // cout<<endl;
  }
  cout << cnt << endl;
  /*
  int k;
  cin>>k;
  binary_insert(a,k);
  rep(i,a.size())cout<<a[i]<<" ";
  cout<<endl;
  */

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

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

// j-i=a[j]+a[i]
// j-a[j]=i+a[i]
int main() {
  ll n;
  cin >> n;
  vector<ll> wa(200000), sa(200000);
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    if (a + i + 1 < 200000)
      wa[a + i + 1]++;
    if (i + 1 - a >= 0)
      sa[i + 1 - a]++;
  }
  ll ans = 0;
  for (ll i = 1; i < 200000; i++) {
    ans += wa[i] * sa[i];
  }

  cout << ans << endl;
}


Error: -11

Fixed submission:

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

// j-i=a[j]+a[i]
// j-a[j]=i+a[i]
int main() {
  ll n;
  cin >> n;
  vector<ll> wa(200000), sa(20000);
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    if (a + i + 1 < 200000)
      wa[a + i + 1]++;
    if (i + 1 - a >= 0)
      sa[i + 1 - a]++;
  }
  ll ans = 0;
  for (ll i = 1; i < 200000; i++) {
    ans += wa[i] * sa[i];
  }

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

#include <bits/stdc++.h>
typedef long long int lld;
typedef unsigned long long int llu;
#define INF (lld)(9223372036854775807)
#define IINF (int)(2147483647)

#define POW5(n) (n * n * n * n * n)

using namespace std;
int main(void) {
  lld a[200000], n, k, s = 0;
  cin >> n;
  unordered_map<lld, lld> m;
  for (lld i = 0; i < n; i++) {
    cin >> a[i];
    a[i] -= i;
    auto it = m.find(a[i]);
    if (it != m.end()) {
      it->second++;
    } else {
      m[a[i]] = 1;
    }
  }
  for (lld i = 0; i < n; i++) {
    k = -(a[i] + i) - i;
    auto it = m.find(k);
    if (it != m.end()) {
      s += it->second;
    }
  }
  cout << s << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long int lld;
typedef unsigned long long int llu;
#define INF (lld)(9223372036854775807)
#define IINF (int)(2147483647)

#pragma GCC optimize("O3")

using namespace std;
int main(void) {
  lld a[200000], n, s = 0;
  cin >> n;
  for (lld i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (lld i = 0; i < n; i++) {
    for (lld j = i + 1; j < n; j++) {
      if (j - i == a[i] + a[j]) {
        s++;
      }
    }
  }
  cout << s << "\\n";
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
const long long INF = 1LL << 60;

int main(void) {
  long long N, rst;
  cin >> N;
  map<long long, long long> a;
  long long b[N];
  int i, j;
  rst = 0;
  long long tmp = 0, cnt = 0;

  for (i = 0; i < N; i++) {
    cin >> tmp;
    if (a[i + tmp]) {
      a[i + tmp] += 1;
    } else {
      a[i + tmp] = 1;
    }
    b[i] = i - tmp;
    // cout<<i<<" "<<i-tmp<<endl;
  }
  for (i = 0; i < N; i++) {
    if (a[b[i]] > 0) {
      // cout<<i<<" "<<b[i]<<" "<<a[b[i]]<<endl;
      rst += a[b[i]];
    }
  }
  // cout<<a.size()<<" "<<b.size()<<endl;

  cout << rst << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
const long long INF = 1LL << 60;

int main(void) {
  int N, rst;
  cin >> N;
  long a[N], b[N];
  int i, j;
  rst = 0;
  int tmp = 0, cnt = 0;

  for (i = 0; i < N; i++) {
    cin >> a[i];
    b[i] = i - a[i];
  }

  for (i = 0; i < N - 2; i++) {
    // if(i+a[i]<N){
    tmp = a[i] + i;
    for (j = i + a[i]; j < N; j++) {
      if (b[j] == tmp) {
        rst += 1;
        // cout<<i<<" "<<j<<endl;
      }
    }
    //}
  }

  cout << rst << endl;

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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

// j - i = Ai + Aj
// i + Ai = j - Aj

int main() {

  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
  }

  map<ll, ll> mp;
  ll ans = 0;
  for (ll i = 0; i < n; ++i) {
    ll r = i - a[i]; // j - Ajを計算
    // rに等しいi + Aiの個数をansに足し算
    ans += mp[r];
    ll l = i + a[i];
    mp[l]++;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {

  ll n;
  cin >> n;

  vector<ll> a(n);
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
  }

  ll ans = 0;
  for (ll i = 2; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      if (j + i >= n)
        break;
      if (a[j] >= i)
        continue;

      if (a[j] + a[j + i] == i)
        ++ans;
    }
  }

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

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

int main() {
  unordered_map<int, pair<vector<int>, vector<int>>> m;
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
    m[i + arr[i]].first.push_back(i + 1);
    if (i - arr[i] > 0)
      m[i - arr[i]].second.push_back(i + 1);
  }

  long long c(0);
  for (const auto &[k, v] : m) {
    for (const int &i : v.first) {
      c += distance(lower_bound(v.second.begin(), v.second.end(), i + 1),
                    v.second.end());
    }
  }
  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:



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

int main() {
  map<int, unordered_set<int>> m;
  int n;
  cin >> n;
  int arr[n];
  for (int i = 1; i <= n; ++i) {
    cin >> arr[i - 1];
    m[arr[i - 1]].insert(i);
  }
  // sum bounded by n - 1
  int c(0);
  for (int i = 0; i < n; ++i) {
    m[arr[i]].erase(i + 1);
    // arr[i] + k >= 1
    // arr[i] + k <= n - (i + 1)
    auto it = m.lower_bound(1 - arr[i]);
    auto jt = m.lower_bound(n - (i + 1) - arr[i]);
    while (it != m.end()) {
      // if (it->first + arr[i] >= n) break;
      if (it->second.count(arr[i] + it->first + i + 1))
        c++;
      if (it == jt)
        break;
      ++it;
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
  int n;
  cin >> n;
  vll a(n);
  rep(i, n) cin >> a[i];
  vll x;
  rep(i, n) { x.push_back(-a[i] + n - i - 1); }
  sort(all(x));
  // vout(x);
  // cout << endl;
  ll ans = 0;
  for (ll i = n - 1; i >= 0; i--) {
    ans += upper_bound(all(x), a[i] + n - 1 - i) -
           lower_bound(all(x), a[i] + n - 1 - i);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
  int n;
  cin >> n;
  vint a(n);
  rep(i, n) cin >> a[i];
  map<int, int> ct;
  ct[-a[0]]++;
  int ans = 0;
  REP(i, n) {
    map<int, int> tmp = ct;
    for (P x : ct) {
      if (x.second == 0)
        continue;
      tmp[x.first] -= x.second;
      tmp[x.first + 1] += x.second;
    }
    swap(tmp, ct);
    ct[-a[i]]++;
    // for (auto itr = ct.begin(); itr != ct.end(); itr++) {
    //     cout << itr->first << " " << itr->second << "   ";
    // }
    // cout << endl;
    ans += ct[a[i]];
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define Check system("pause")
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

int a[200005];
map<int, int> now;

int main() {
  ios::sync_with_stdio(false);
  int n, k, w;
  ll ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    ans += now[i - 1 - a[i]];
    now[a[i] + i - 1]++;
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define Check system("pause")
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

int a[200005];

int main() {
  ios::sync_with_stdio(false);
  int n, k;
  ll ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    for (int j = 1; j < i; j++) {
      if (i - j == a[i] + a[j])
        ans++;
    }
  }
  cout << ans;
  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, n) for (int i = 1; i <= (int)(n); i++)
#define ll long long
#define umap unordered_map
using graph = vector<vector<int>>;
using graph2 = vector<vector<pair<int, int>>>;

ll mod(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x % y;
  return y + x % y;
} // mod including minus
ll dv0(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x / y;
  return x / y - 1;
} // rnd down
ll dv1(ll x, ll y) {
  if (x % y == 0)
    return dv0(x, y);
  return dv0(x, y) + 1;
} // rnd up
ll M = 1e9 + 7;
int quick_pow(int x, int p) {
  int a = 1, po = x;
  while (p) {
    if (p & 1)
      a = 1ll * a * po % M;
    po = 1ll * po * po % M;
    p >>= 1;
  }
  return a;
}
// Start from Here
int main() {
  int N;
  cin >> N;
  int a[N];
  int b;
  vector<int> S;
  rep(i, N) {
    cin >> b;
    a[i] = -b - i;
    S.push_back(b - i);
  }
  S.push_back(1e9);
  sort(S.begin(), S.end());
  S.push_back(1e9);
  ll cnt = 0;
  rep(i, N) {
    auto itr1 = lower_bound(S.begin(), S.end(), a[i]);
    auto itr2 = upper_bound(S.begin(), S.end(), a[i]);
    cnt += itr2 - itr1;
  }
  cout << cnt << endl;
}

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, n) for (int i = 1; i <= (int)(n); i++)
#define ll long long
#define umap unordered_map
using graph = vector<vector<int>>;
using graph2 = vector<vector<pair<int, int>>>;

ll mod(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x % y;
  return y + x % y;
} // mod including minus
ll dv0(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x / y;
  return x / y - 1;
} // rnd down
ll dv1(ll x, ll y) {
  if (x % y == 0)
    return dv0(x, y);
  return dv0(x, y) + 1;
} // rnd up
ll M = 1e9 + 7;
int quick_pow(int x, int p) {
  int a = 1, po = x;
  while (p) {
    if (p & 1)
      a = 1ll * a * po % M;
    po = 1ll * po * po % M;
    p >>= 1;
  }
  return a;
}
// Start from Here
int main() {
  int N;
  cin >> N;
  int a[N];
  int b;
  multiset<int> S;
  rep(i, N) {
    cin >> b;
    a[i] = -b - i;
    S.insert(b - i);
  }
  S.insert(1e9);
  int cnt = 0;
  rep(i, N) {
    auto itr1 = S.lower_bound(a[i]);
    if (*itr1 == a[i]) {
      while (*itr1 == a[i]) {
        itr1++;
        cnt++;
      }
    }
  }
  cout << cnt << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

// This Message Will Self-Destruct in 5s

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define rep(i, x, y) for (ll i = x; i < y; i++)
#define nvarep(n, a)                                                           \\
  ll n;                                                                        \\
  cin >> n;                                                                    \\
  vector<ll> a(n);                                                             \\
  rep(i, 0, n) cin >> a[i]
#define vecrep(n, a, type)                                                     \\
  vector<type> a(n);                                                           \\
  rep(i, 0, n) cin >> a[i]
#define lcm(a, b) (a / __gcd(a, b) * b)
#define range(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define nnn "\\n"
#define spa " "
using p = pair<ll, ll>;
using graph = vector<vector<ll>>;
const int inf = 2147483647;         // 2*10^9
const ll INF = 9223372036854775807; // 9*10^18

signed main() {
  nvarep(n, a);
  map<int, int> m{}, l{};
  rep(i, 0, n) {
    m[a[i] - i]++;
    l[a[i] + i]++;
  }
  int ans = 0;
  for (auto i : m) {
    ans += l[-i.first] * i.second;
  }
  cout << ans << nnn;
  return 0;
}

Error: TLE

Fixed submission:

// This Message Will Self-Destruct in 5s

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) a / __gcd(a, b) * b
#define range(a) (a).begin(), (a).end()
const int inf = 2147483647;

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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(v) v.begin(), v.end()
#define ll long long
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;

// snippets:tmp,cl,cvl,cs,co,coy,con,cov

int main() {
  ll n;
  cin >> n;
  queue<ll> q;
  queue<ll> qq;
  vll d(500000, 0);
  rep(i, n) {
    ll a;
    cin >> a;
    if (a > 200000)
      continue;
    q.push(a + i);
    if (a > i)
      continue;
    d[i - a]++;
  }
  ll count = 0;
  ll a = q.size();
  rep(i, a) {
    count += d[q.front()];
    q.pop();
  }
  cout << count << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(v) v.begin(), v.end()
#define ll long long
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;

// snippets:tmp,cl,cvl,cs,co,coy,con,cov

int main() {
  ll n;
  cin >> n;
  queue<ll> q;
  queue<ll> qq;
  vll d(500000, 0);
  rep(i, n) {
    ll a;
    cin >> a;
    q.push(a + i);
    if (a > i)
      continue;
    d[i - a]++;
  }
  ll count = 0;
  ll a = q.size();
  rep(i, a) {
    count += d[q.front()];
    q.pop();
  }
  cout << count << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

bool debug = false;

unordered_map<ll, ll> idPlusHeightCount;
// vector<ll> A;
ll A[200005];

int main() {
  std::ios::sync_with_stdio(false);

  ll n;

  cin >> n;

  ll res = 0;

  for (int i = 1; i <= n; i++) {
    cin >> A[i];

    res += idPlusHeightCount[i - A[i]];

    idPlusHeightCount[i + A[i]]++;

    /*
    for (int j = 1; j < i; j++)
    {
        if (i - j == A[i] + A[j]) // i - A[i] == j + A[j]
            res++;
    }
    */
  }

  cout << res;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

bool debug = false;

unordered_map<ll, ll> heightCount;
// vector<ll> A;
ll A[200005];

int main() {
  std::ios::sync_with_stdio(false);

  ll n;

  cin >> n;

  ll res = 0;

  for (int i = 1; i <= n; i++) {
    cin >> A[i];

    for (int j = 1; j < i; j++) {
      if (i - j == A[i] + A[j])
        res++;
    }
  }

  cout << res;

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

#include <bits/stdc++.h>

using namespace std;

#define intt long long
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);

const int N = 200005;

int h[N];

map<int, int> cnt;

int main() {

  FAST;

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    cnt[i + h[i]]++;
  }
  intt ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += cnt[i - h[i]];
  }
  cout << ans << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define intt long long
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);

const int N = 100005;

int h[N];

map<int, int> cnt;

int main() {

  FAST;

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    cnt[i + h[i]]++;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += cnt[i - h[i]];
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

ll n, a[200001], ans;
map<ll, ll> mp;

int main() {

  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    mp[i + a[i]]++;

    ans += mp[i - a[i]];
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
int main() {
  long long int n, i, j, ans = 0;
  cin >> n;
  long long int h[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> h[i];
  }
  for (i = 1; i <= n; i++) {
    if (h[i] > n)
      continue;
    for (j = i + 1; j <= n; j++) {
      if (j - i == h[j] + h[i])
        ans++;
    }
  }
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll LINF = 1001002003004005006LL;
const int INF = 1001001001;

const int mod = 1000000007;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  map<int, int> mp;
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += (ll)mp[i - a[i]];
    ++mp[a[i] + i];
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll LINF = 1001002003004005006LL;
const int INF = 1001001001;

const int mod = 1000000007;
const int mx = 200005;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  multiset<int> st;
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] > mx)
      continue;
    ans += st.count(i - a[i]);
    st.insert(a[i] + i);
  }
  cout << ans << endl;
  return 0;
}