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

#include <cmath>
#include <iostream>
#include <map>

using ll = long long;

ll n;

std::map<ll, ll> mapa;
long heights[2000001];

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define float double
#define pb push_back
#define FF first
#define SS second
#define N 100005
#define MOD 1000000007
#define fn(i, n) for (int i = 0; i < n; i++)
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define pii pair<int, int>
#define mp make_pair
#define INF 1000000000000000000
#define speed                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

signed main() {
  speed;
  // freopen("input.tXt","r",stdin);
  // freopen("output.tXt","w",stdout);

  int n;
  cin >> n;
  int A[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i - A[i] - 1; j++)
      if (A[i] + A[j] == abs(i - j))
        ans++;
    for (int j = A[i] + i + 1; j <= n; j++)
      if (A[i] + A[j] == abs(i - j))
        ans++;
  }
  cout << ans / 2 << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
ll a[maxn], ans[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (i + a[i] < 200000)
      ans[i + a[i]]++;
  }
  ll x = 0;
  for (int i = n; i >= 1; --i) {
    if (i - a[i] >= 200000 || i - a[i] <= 0)
      continue;
    x += ans[i - a[i]];
    if (i + a[i] < 200000)
      ans[i + a[i]]--;
  }
  cout << x << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
ll a[maxn], ans[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    ans[i + a[i]]++;
  }
  ll x = 0;
  for (int i = n; i >= 1; --i) {
    if (i - a[i] >= 200000 || i - a[i] <= 0)
      continue;
    x += ans[i - a[i]];
    if (i + a[i] < 200000)
      ans[i + a[i]]--;
  }
  cout << x << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  ll a[n], ans = 0;
  map<ll, ll> mp;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    ++mp[i - a[i]];
  }
  for (int i = 0; i < n; ++i) {
    --mp[i - a[i]];
    ans += mp[i + a[i]];
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  ll a[n], ans = 0;
  multiset<ll> st;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    st.insert(i - a[i]);
  }
  for (int i = 0; i < n; ++i) {
    st.erase(st.find(i - a[i]));
    ans += st.count(i + a[i]);
  }
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

  ll ans = 0;
  map<ll, int> mp;
  for (int j = 0; j < N; j++) {
    int sa = j - a[j];
    int wa = a[j] + j;

    ans += mp[sa];
    mp[wa]++;
  }

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

Error: TLE

Fixed submission:

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

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

  vector<ll> H;
  vector<ll> H2;
  for (int i = 0; i < N; i++) {
    ll h;
    cin >> h;
    H.push_back(h);
    H2.push_back(h);
  }
  sort(H2.begin(), H2.end());
  ll max_h = H2[N - 1];

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ll lange = H[i] + max_h;
    for (int j = i + 1; j < N && j < i + lange + 1; j++) {
      if (abs((i) - (j)) == H[i] + H[j]) {
        ans++;
      }
    }
  }

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

#pragma GCC optimize("Ofast", 3, "inline")
#include <bits/stdc++.h>
using namespace std;
#define fir first
#define sec second
#define m_p make_pair
#define y1 ygftgfgcdtfgxffgx
#define y2 yfdsesgvtyghftfvv
#define x1 xvyr6cf6fgcfgf676
#define x2 xcr6rfc5r66y6r6fr
#define up_bound upper_bound
#define low_bound lower_bound
#define next_per next_permutation
#define pb push_back
#define i_to_s to_string
typedef priority_queue<int> p_queue;
typedef priority_queue<int, vector<int>, greater<int>> min_p_queue;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int mon[20] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
ll gcd(ll x, ll y) { return ((y == 0) ? x : gcd(y, x % y)); }
ll cnt[200010][5];
int n;
ll a[200010], ans;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  //	freopen(".in","r",stdin);
  //	freopen(".out","w",stdout);
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    ll up = a[i] + i;
    ll dw = i - a[i];
    if (up <= n)
      cnt[up][0]++;
    if (dw >= 1)
      cnt[dw][1]++;
  }
  for (int i = 1; i <= n; i++) {
    ans += cnt[i][0] * cnt[i][1];
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#pragma GCC optimize("Ofast", 3, "inline")
#include <bits/stdc++.h>
using namespace std;
#define fir first
#define sec second
#define m_p make_pair
#define y1 ygftgfgcdtfgxffgx
#define y2 yfdsesgvtyghftfvv
#define x1 xvyr6cf6fgcfgf676
#define x2 xcr6rfc5r66y6r6fr
#define up_bound upper_bound
#define low_bound lower_bound
#define next_per next_permutation
#define pb push_back
#define i_to_s to_string
typedef priority_queue<int> p_queue;
typedef priority_queue<int, vector<int>, greater<int>> min_p_queue;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int mon[20] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
ll gcd(ll x, ll y) { return ((y == 0) ? x : gcd(y, x % y)); }
ll cnt[100010][5];
int n;
ll a[100010], ans;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  //	freopen(".in","r",stdin);
  //	freopen(".out","w",stdout);
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    ll up = a[i] + i;
    ll dw = i - a[i];
    if (up <= n)
      cnt[up][0]++;
    if (dw >= 1)
      cnt[dw][1]++;
  }
  for (int i = 1; i <= n; i++) {
    ans += cnt[i][0] * cnt[i][1];
  }
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N;
  cin >> N;
  ll A[N];
  ll ans = 0;
  map<ll, ll> Mp;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    ans += Mp[(i + 1) - A[i]];
    Mp[A[i] + (i + 1)]++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N;
  cin >> N;
  ll A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll ans = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      if (A[j] + A[i] == j - i) {
        ans++;
      }
    }
  }
  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 < (n); i++)
typedef long long ll;

int main() {
  ll n, ans = 0;
  cin >> n;
  map<ll, ll> x, y;
  for (int i = 1; i <= n; i++) {
    int tmp;
    cin >> tmp;
    x[i + tmp]++;
    y[i - tmp]++;
  }
  for (auto p : x) {
    ll t = p.first;
    ans += p.second * y[t];
  }

  cout << ans;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, ans = 0;
  cin >> n;
  vector<pair<int, int>> p(n);
  REP(i, n) {
    int tmp;
    cin >> tmp;
    p.at(i) = make_pair(tmp, i + 1);
  }
  sort(p.begin(), p.end());

  REP(i, n - 1) {
    int j = i + 1;
    while (j < n && p.at(i).first + p.at(j).first <= n - 1) {
      int ttl = p.at(i).first + p.at(j).first;
      if (abs(p.at(i).second - p.at(j).second) == ttl)
        ans++;
      j++;
    }
  }

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

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

int main() {
  int N;
  cin >> N;
  long long result = 0;
  vector<long long> A(N);
  for (int a = 0; a < N; a++)
    cin >> A[a];
  vector<int> B(200000);
  for (int a = 0; a < 200000; a++)
    B[a] = 0;
  for (int a = 0; a < N; a++) {
    if (200000 > A[a] + a + 1 && A[a] + a + 1 >= 0) {
      B[A[a] + a + 1]++;
    }
    if (200000 > a + 1 - A[a] && a + 1 - A[a] >= 0) {
      result += B[a + 1 - A[a]];
    }
  }
  cout << result << endl;
}

Error: 0

Fixed submission:

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

int main() {
  long long N;
  cin >> N;
  long long result = 0;
  vector<long long> A(N);
  for (int a = 0; a < N; a++)
    cin >> A[a];
  vector<long long> B(200000);
  for (int a = 0; a < 200000; a++)
    B[a] = 0;
  for (int a = 0; a < N; a++) {
    B[A[a] + a + 1]++;
    if (200000 > a + 1 - A[a] && a + 1 - A[a] >= 0) {
      result += B[a + 1 - A[a]];
    }
  }
  cout << result << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdlib.h>
#include <string>
#include <vector>

#define PI 3.14159265358979
#define MOD 1000000007

// fixed << setprecision(15) <<

using namespace std;

int main(void) {
  long long int l, m, n, sum;
  map<long long int, long long int> b, a;
  int i, j, k;
  bool flag = true;
  cin >> n;
  m = 0;
  for (i = 0; i < n; i++) {
    cin >> l;
    a[l + i]++;
    b[i - l]++;
    if (m < i - l)
      m = i - l;
  }
  sum = 0;
  for (i = 1; i <= m; i++) {
    sum += a[i] * b[i];
  }
  cout << sum << endl;
  /*if(flag)cout << "Yes" << endl;
          else cout << "No" << endl;*/
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdlib.h>
#include <string>
#include <vector>

#define PI 3.14159265358979
#define MOD 1000000007

// fixed << setprecision(15) <<

using namespace std;

int main(void) {
  long long int l, m, n, sum;
  map<long long int, long long int> b, a;
  int i, j, k;
  bool flag = true;
  cin >> n;
  m = 0;
  for (i = 0; i < n; i++) {
    cin >> l;
    a[l + i]++;
    b[i - l]++;
    if (m < i + l)
      m = l + i;
  }
  sum = 0;
  for (i = 1; i < m; i++) {
    sum += a[i] * b[i];
  }
  cout << sum << endl;
  /*if(flag)cout << "Yes" << endl;
          else cout << "No" << endl;*/
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

signed main() {
  int n;
  cin >> n;

  map<int, int> mp;

  int a;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a;
    ans += mp[i - a];
    mp[i + a]++;
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

signed main() {
  int n;
  cin >> n;

  int a[n + 1];
  map<int, bool> flag;
  map<pair<int, int>, bool> ff;

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

    if (i - a[i] >= 0) {
      // diff[a[i]][i] = 1;
      flag[i - a[i]] = true;
      ff[pair<int, int>(i - a[i], i)] = true;
      diffmax = max(i - a[i], diffmax);
    }
  }

  int diff[diffmax + 1][n + 1];

  for (int i = 1; i <= n; i++) {
    if (i - a[i] >= 0) {
      diff[i - a[i]][i] = 1;
    }
  }

  for (auto itr = flag.begin(); itr != flag.end(); ++itr) {

    for (int j = 1; j <= n; j++) {
      if (!ff[pair<int, int>(itr->first, j)])
        diff[itr->first][j] = 0;
      if (j > 1)
        diff[itr->first][j] += diff[itr->first][j - 1];
    }
  }
  int ans = 0;
  for (int i = 1; i <= n - 2; i++) {
    if (flag[a[i] + i]) {
      ans += diff[a[i] + i][n] - diff[a[i] + i][i + 1];
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// i+ a[i]== j- a[j] なるペアを数える。
// 上が成り立つ場合,制約より
// i- a[i]= j- 2*a[j]≠ j+ a[j]
// なので、各ペアの重複度は1.
int main() {
  int N;
  cin >> N;

  vector<int> data(N);
  vector<int> idx_val(N);
  for (int i = 0; i < N; i++) {
    cin >> data.at(i);
    idx_val.at(i) = i - data.at(i);
  }
  sort(idx_val.begin(), idx_val.end());

  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    auto itr1 = lower_bound(idx_val.begin(), idx_val.end(), i + data.at(i));
    auto itr2 = upper_bound(idx_val.begin(), idx_val.end(), i + data.at(i));

    ans += distance(itr1, itr2);
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

// i+ a[i]== j- a[j] なるペアを数える。
// 上が成り立つ場合,制約より
// i- a[i]= j- 2*a[j]≠ j+ a[j]
// なので、各ペアの重複度は1.
int main() {
  int N;
  cin >> N;

  vector<int> data(N);
  multiset<int> idx_val;
  for (int i = 0; i < N; i++) {
    cin >> data.at(i);
    idx_val.insert(i - data.at(i));
  }

  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    ans += idx_val.count(i + data.at(i));
  }

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

#include <bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
using namespace std;
int static fast = []() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  return 0;
}();
// freopen("input.txt", "r", stdin);

int main() {
  int n;
  cin >> n;
  vector<int> arr(n, 0);
  for (int &val : arr)
    cin >> val;
  //  Ai + Aj = (i - j) or (j - i)
  // Ai - i == -j - Aj
  // Ai + i == j - Aj
  unordered_map<int, ll> target;
  unordered_map<int, ll> target2;
  map<pair<int, int>, ll> join;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (target.count(arr[i] - i) > 0) {
      ans += target[arr[i] - i];
    }
    if (target2.count(arr[i] + i) > 0) {
      ans += target2[arr[i] + i];
    }
    if (join.count({arr[i] - i, arr[i] + i}) > 0)
      ans -= join[{arr[i] - i, arr[i] + i}];
    target[-i - arr[i]] += 1;
    target2[i - arr[i]] += 1;
    join[{-i - arr[i], i - arr[i]}] += 1;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
using namespace std;
int static fast = []() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  return 0;
}();
// freopen("input.txt", "r", stdin);

int main() {
  int n;
  cin >> n;
  vector<int> arr(n, 0);
  for (int &val : arr)
    cin >> val;
  //  Ai + Aj = (i - j) or (j - i)
  // Ai - i == -j - Aj
  // Ai + i == j - Aj
  unordered_map<int, unordered_set<int>> target;
  unordered_map<int, unordered_set<int>> target2;
  map<pair<int, int>, int> join;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    unordered_set<int> collect;
    if (target.count(arr[i] - i) > 0) {
      collect = target[arr[i] - i];
    }
    if (target2.count(arr[i] + i) > 0) {
      for (int val : target2[arr[i] + i])
        collect.insert(val);
    }
    ans += collect.size();
    target[-i - arr[i]].insert(i);
    target2[i - arr[i]].insert(i);
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define se second
#define fi first
#define endl "\\n"
#define pi 3.1415926535897932384626433832795028841971
#define MAX 9223372036854775807
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  //	cout<<fixed<<setprecision(10);
  ll n;
  cin >> n;
  ll arr[n + 1];
  map<ll, ll> mp1, mp2;
  for (int a = 1; a <= n; a++) {
    cin >> arr[a];
    mp1[arr[a] + a]++;
    mp2[a - arr[a]]++;
  }
  map<ll, ll>::iterator it;
  ll ans = 0;
  for (it = mp1.begin(); it != mp1.end(); it++) {
    ans += (it->se) * mp2[it->fi];
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define se second
#define fi first
#define endl "\\n"
#define pi 3.1415926535897932384626433832795028841971
#define MAX 9223372036854775807
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  //	cout<<fixed<<setprecision(10);
  ll n;
  cin >> n;
  ll arr[n];
  for (int a = 0; a < n; a++)
    cin >> arr[a];
  ll x, y, z, ans = 0;
  for (int a = 0; a < n; a++) {
    x = a + arr[a] + 1;
    y = 1;
    while (x < n) {
      if (arr[x] == y) {
        ans++;
      }
      y++;
      x++;
    }
  }
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int64_t MOD = 1000000007;
double PI = 3.141592653589793;

int main() {
  int N, tmp, p, q;
  map<int64_t, pair<int64_t, int64_t>> list;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> tmp;
    p = i + tmp, q = i - tmp;
    list[p].first++;
    list[q].second++;
  }
  int64_t ans = 0;
  for (auto x : list)
    ans += x.second.first * x.second.second;
  cout << ans;
}

Error: TLE

Fixed submission:

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

int mod = 1000000007;

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

  vector<int> list(N);
  for (int i = 0; i < N; i++)
    cin >> list[i];

  int count = 0;
  for (int i = 0; i < N; i++) {
    if (i + list[i] < N) {
      for (int j = i + list[i]; j < N; j++) {
        if (j - i == list[i] + list[j])
          count++;
      }
    }
  }
  cout << count;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef double dd;
const int N = 2e5 + 5;
const dd eps = 1e-8;
const int mod = 1e9 + 7;

ll vis[N], now, ans, qu[N], a[N];
int n;

int main() {
  // freopen("input.txt","r",stdin);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &qu[i]);
    a[i] = qu[i];
    qu[i] += i;
  }
  sort(qu + 1, qu + 1 + n);
  for (int i = 1; i <= n; i++) {
    int inx = lower_bound(qu + 1, qu + 1 + n, i - a[i]) - qu;
    int inx2 = upper_bound(qu + 1, qu + 1 + n, i - a[i]) - qu;
    if (inx == n + 1)
      continue;
    if (qu[inx] == i - a[i]) {
      ans += inx2 - inx;
    }
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef double dd;
const int N = 2e5 + 5;
const dd eps = 1e-8;
const int mod = 1e9 + 7;

ll vis[N], now, ans, qu[N], a[N];
int n;

int main() {
  // freopen("input.txt","r",stdin);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &qu[i]);
    a[i] = qu[i];
    qu[i] += i;
  }
  sort(qu + 1, qu + 1 + n);
  for (int i = 1; i <= n; i++) {
    int inx = lower_bound(qu + 1, qu + 1 + n, i - a[i]) - qu;
    if (inx == n + 1)
      continue;
    while (qu[inx] == i - a[i] and inx <= n) {
      ans += 2;
      inx++;
    }
  }
  printf("%lld\\n", ans / 2);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  map<int64_t, int> m;
  int64_t temp;
  for (int i = 0; i < N; i++) {
    temp = i + 1 + A.at(i);
    if (m.count(temp)) {
      m.at(temp)++;
    } else {
      m[temp] = 1;
    }
  }
  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    temp = i + 1 - A.at(i);
    if (m.count(temp)) {
      ans += m.at(temp);
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + A.at(i); j < N; j++) {
      if (j - i - A.at(i) == A.at(j)) {
        ans++;
      }
    }
  }
  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 < (n); i++)

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }

  map<int, int> m;

  long int ans = 0;
  rep(i, n) {
    ans += m[i - a[i]];
    m[i + a[i]]++;
  }

  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 < (n); i++)

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }

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

  cout << cnt << endl;

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

/*
useful stl
array<ll,size> ar;         //size(),front(),back(),fill(),empty()
deque<ll> d                //push_back(),push_front(),pop_front(),pop_back()
vector<ll> v(size,val);
//push_back(),*max_element(v.begin(),v.end()),accumulate(v.begin(),v.endaa(),0),pop_back()
stack<ll> g;               //push(),pop(),top(),size(),empty()         LIFO
queue<ll> q;               //push(),size(),front(),back(),empty()      FIFO
priority_queue<ll> pq
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  long long n, ans = 0;
  cin >> n;
  vector<long long> v(n + 1);
  map<long long, long long> mp;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    ans += mp[v[i] - i];
    mp[-i - v[i]]++;
  }
  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

/*
useful stl
array<ll,size> ar;         //size(),front(),back(),fill(),empty()
deque<ll> d                //push_back(),push_front(),pop_front(),pop_back()
vector<ll> v(size,val);
//push_back(),*max_element(v.begin(),v.end()),accumulate(v.begin(),v.endaa(),0),pop_back()
stack<ll> g;               //push(),pop(),top(),size(),empty()         LIFO
queue<ll> q;               //push(),size(),front(),back(),empty()      FIFO
priority_queue<ll> pq
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

const int N = 1e5 + 5;
vector<int> g[N];

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;
  vector<long long> v(n + 1);

  for (int i = 1; i <= n; i++)
    cin >> v[i];
  int c = 0;
  for (int i = 1; i <= n - 1; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (j - i == v[i] + v[j]) {
        c++;
      }
    }
  }
  cout << c;

  return 0;
}