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

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n;
  int i, j;
  long ans = 0;

  cin >> n;
  long *a = new long[n + 2];
  long *ab = new long[n + 2];
  long *ab2 = new long[n + 2];

  for (i = 1; i <= n; i++) {
    cin >> a[i];
    ab2[i] = i - a[i];
    ab[i] = i + a[i];
  }

  sort(&ab[1], &ab[n + 1]);
  sort(&ab2[1], &ab2[n + 1]);

  j = 1;
  long cnt, cnt2;
  for (i = 1; i <= n; i++) {
    cnt = 1;
    for (i = i + 1; i <= n; i++) {
      if (ab[i] == ab[i - 1]) {
        cnt++;
      } else {
        i--;
        break;
      }
    }
    if (i > n)
      i--;

    cnt2 = 0;
    //		cout << "i = " << i << endl;
    for (; ab[i] >= ab2[j] && j <= n; j++) {
      //			cout << ab[i] << " / " << ab2[j] << endl;
      if (ab[i] == ab2[j]) {
        //				cout << i << " / " << j << endl;
        cnt2++;
      }
    }
    ans += cnt * cnt2;
  }

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


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int main() {
  int n;
  int i, j;
  long ans = 0;

  cin >> n;
  long *a = new long[n + 2];

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

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

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

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

  int n;
  cin >> n;

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

  ll r = 0;
  map<int, int> mp;

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

  cout << r << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

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

  int n;
  cin >> n;

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

  ll r = 0;
  multiset<int> st;

  for (int i = n - 1; i >= 0; i--) {
    r += st.count(-a[i] - i);
    st.insert(a[i] - i);
  }

  cout << r << endl;

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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  vector<int> As(N);
  map<int, int> diff2NumMap;
  for (int i = 1; i <= N; ++i) {
    int Ai;
    cin >> Ai;
    As[i - 1] = Ai;
    ++diff2NumMap[i - Ai];
  }
  LLONG ans = 0;
  for (int a = 1; a < N; ++a) {
    int Aa = As[a - 1];
    int diff = a - Aa;
    --diff2NumMap[diff];
    int key = a + Aa;
    if (diff2NumMap.find(key) != diff2NumMap.end()) {
      ans += diff2NumMap[key];
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  vector<int> As(N);
  multiset<int> diffs;
  for (int i = 1; i <= N; ++i) {
    int Ai;
    cin >> Ai;
    As[i - 1] = Ai;
    diffs.insert(i - Ai);
  }
  LLONG ans = 0;
  for (int a = 1; a < N; ++a) {
    int Aa = As[a - 1];
    diffs.erase(diffs.find(a - Aa));
    ans += diffs.count(a + Aa);
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ok() puts(ok ? "Yes" : "No");
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef set<int> si;
typedef map<string, int> msi;
typedef greater<int> gt;
typedef priority_queue<int, vector<int>, gt> minq;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll LINF = 1e18L + 1;
const int INF = 1e9 + 1;
// clang++ -std=c++11 -stdlib=libc++

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  map<int, int> mp;
  ll ans = 0;
  rep(i, n) {
    int dif = i - a[i];
    int sum = i + a[i];
    ans += mp[dif];
    mp[sum]++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ok() puts(ok ? "Yes" : "No");
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef set<int> si;
typedef map<string, int> msi;
typedef greater<int> gt;
typedef priority_queue<int, vector<int>, gt> minq;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll LINF = 1e18L + 1;
const int INF = 1e9 + 1;
// clang++ -std=c++11 -stdlib=libc++

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  // input
  int64_t N;
  cin >> N;

  map<int64_t, int64_t> Ap;
  map<int64_t, int64_t> Am;

  for (int64_t i = 0; i < N; i++) {
    int64_t temp;
    cin >> temp;
    if (Ap.count(i + temp)) {
      Ap[i + temp]++;
    } else {
      Ap[i + temp] = 1;
    }
    if (Am.count(i - temp)) {
      Am[i - temp]++;
    } else {
      Am[i - temp] = 1;
    }
  }

  // calculation
  int64_t output = 0;
  for (auto p : Ap) {
    auto key = p.first;
    auto value = p.second;
    if (Am.count(key)) {
      output += value * Am[key];
    }
  }

  // output
  cout << output << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  // input
  int N;
  cin >> N;

  vector<int> Ap(N);
  vector<int> Am(N);
  for (int i = 0; i < N; i++) {
    int temp;
    cin >> temp;
    Ap[i] = i + temp;
    Am[i] = i - temp;
  }

  // calculation
  int output = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      if (Ap[i] == Am[j]) {
        output++;
      }
    }
  }

  // output
  cout << output << endl;

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

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int N;
  cin >> N;
  vector<int> A(N), B(N + 1, 0);
  ll i;
  for (i = 0; i < N; i++) {
    cin >> A[i];
    if (i + 1 + A[i] <= N) {
      B[i + 1 + A[i]]++;
    }
  }
  ll ans = 0;
  for (i = 0; i < N; i++) {
    if (i + 1 >= A[i]) {
      ans += B[i + 1 - A[i]];
    }
  }
  cout << ans << endl;
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

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

#pragma warning(disable : 4996)
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-12;
#define For(i, n, m) for (int i = n; i <= m; ++i)
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define per(i, l, r) for (int i = l; i >= r; --i)
#define clr(a, b) memset(a, b, sizeof(a))

ll n, cnt;
ll a[200100];
ll x[200100], y[200100];
map<ll, vector<int>> s;
int main() {
  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  cin >> n;
  rep(i, 1, n) {
    cin >> a[i];
    x[i] = a[i] + i;
    if (i - a[i] > 0)
      s[i - a[i]].push_back(i);
  }
  rep(i, 1, n) {
    if (s[x[i]].size()) {
      //		cout << i << " " << x[i] << " " << s[x[i]].size() <<
      //"\\n";
      cnt += s[x[i]].size();
    }
  }
  cout << cnt << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#pragma warning(disable : 4996)
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-12;
#define For(i, n, m) for (int i = n; i <= m; ++i)
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define per(i, l, r) for (int i = l; i >= r; --i)
#define clr(a, b) memset(a, b, sizeof(a))

ll n, cnt;
ll a[200100];
int main() {
  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  for (ll i = 1; i <= n; ++i) {
    ll k = i + a[i];
    for (ll j = k; j <= n; ++j)
      if (j - i == a[i] + a[j])
        ++cnt;
  }
  cout << cnt << "\\n";

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdlib.h>
#include <vector>
using namespace std;

int main() {
  long long int n;
  cin >> n;
  std::vector<int> v;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    v.push_back(tmp);
  }
  long long int cnt = 0;
  std::map<long long int, int> m;
  for (int i = 0; i < n; i++) {
    int a = v[i];
    int index = i + a;
    m[index]++;
  }

  for (int i = 0; i < n; i++) {
    int a = v[i];
    int index = i - a;
    if (index >= 0) {
      cnt += m[index];
    }
  }
  cout << cnt << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdlib.h>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  std::vector<int> v;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    v.push_back(tmp);
  }
  long long int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (abs(j - i) == v[i] + v[j]) {
        cnt++;
      }
    }
  }
  cout << cnt << endl;
}
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##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int vector_finder(std::vector<int> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}

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

  map<ll, int> m;
  REP(i, N) {
    cnt += m[i - A[i]];
    m[i + A[i]]++;
  }

  REP(i, N) {}

  cout << cnt << endl;
}


Error: TLE

Fixed submission:

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

  ll cnt = 0;
  REP(i, N) {
    for (int k = i + 1; k < N; ++k) {
      if (abs((i + 1) - (k + 1)) == A[i] + A[k])
        cnt++;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  long N;
  cin >> N;
  long A_plus[N];
  map<long, long> A_minus_count;
  for (long i = 0; i < N; i++) {
    long A;
    cin >> A;
    A_plus[i] = A + (i + 1);
    long A_minus = -A + (i + 1);
    if (A_minus_count.count(A_minus) == 0) {
      A_minus_count[A_minus] = 0;
    }
    A_minus_count[A_minus]++;
  }
  long count = 0;
  for (long i = 0; i < N; i++) {
    if (A_minus_count.count(A_plus[i]) != 0) {
      count += A_minus_count[A_plus[i]];
    }
  }
  cout << count << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int A_plus[N], A_minus[N];
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    A_plus[i] = A + (i + 1);
    A_minus[i] = -A + (i + 1);
  }
  sort(A_plus, A_plus + N);
  sort(A_minus, A_minus + N);

  int count = 0;
  int prev = 0;
  for (int i = 0; i < N - 1; i++) {
    bool prev_set = false;
    for (int j = prev; j < N; j++) {
      if (A_plus[i] == A_minus[j]) {
        if (!prev_set) {
          prev = i;
          prev_set = true;
        }
        count++;
      }
    }
  }
  cout << count << endl;
}
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++)
#define COUT(x) cout << (x) << endl
#define dump(x) cout << #x << " = " << (x) << endl;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
using M = map<int, int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

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

  map<ll, int> m;
  ll ans = 0;
  REP(i, n) {
    ans += m[i - a[i]];
    m[i + a[i]]++;
  }

  COUT(ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define COUT(x) cout << (x) << endl
#define dump(x) cout << #x << " = " << (x) << endl;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
using M = map<int, int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int n;
ll ans = 0;

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

  REP(i, n) {
    for (int j = max(i + 1, a[i]); j < n; j++) {
      ll num = abs(i - j);
      ll h = a[i] + a[j];
      if (num == h)
        ans++;
    }
  }
  COUT(ans);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  map<int, int> x;
  ll ans = 0;
  rep(i, n) {
    int m = i - a[i];
    ans += x[m];
    int s = a[i] + i;
    x[s]++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int i, j, n, x, m, a, b, c, k;
int main() {
  map<int, int> x;
  for (cin >> n, i = 1; i <= n; i++)
    cin >> a, x[i] = a;
  for (i = 1; i < n; i++) {
    for (j = i + 1; j <= n; j++) {
      k = x[i] + x[j];
      if (k == j - i)
        c++;
    }
  }
  cout << c;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
using namespace std;
int main() {
  long long n;
  cin >> n;
  map<long long, long long> m;
  long long c = 0;
  for (long long i = 0; i < n; i++) {
    long long ai;
    cin >> ai;
    c += m[i - ai];
    m[i + ai]++;
  }
  cout << c << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int *a = new int[n];
  int c = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    int ai = a[i] + i;
    for (int j = i + 1; j < n; j++) {
      if (j == ai + a[j])
        c++;
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
#define LL long long

const int MAXN = 2e5 + 10;
const int MAXNUM = 1e9 + 10;
LL N;
LL A[MAXN];
map<LL, LL> mii;

int main() {
  cin >> N;

  for (LL i = 1; i <= N; i++) {
    cin >> A[i];
  }

  for (LL i = 1; i <= N; i++) {
    LL t = A[i] + i;
    mii[t]++;
  }

  LL ans = 0;
  for (LL j = 1; j <= N; j++) {
    LL t = j - A[j];
    ans += mii[t];
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define LL long long

const int MAXN = 2e5 + 10;
int N;
int A[MAXN];
int main() {
  cin >> N;

  int ma1, ma2;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  ma1 = A[0];
  ma2 = 0;
  for (int i = 1; i < N; i++) {
    if (A[i] > ma1) {
      ma2 = ma1;
      ma1 = A[i];
    } else {
      if (A[i] > ma2) {
        ma2 = A[i];
      }
    }
  }
  //    cout << ma1 << " : " << ma2 << endl;

  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < min(N, i + ma1 + ma2 + 1); j++) {
      if (j - i == A[i] + A[j]) {
        ++ans;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

Error: WA

Fixed submission:

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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

typedef unsigned long long ull;
typedef long long ll;

ll DENOM = 1000000000LL + 7LL;
ll D2 = 998244353LL;
using namespace std;

ll n, m, k;

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

  for (int i = 0; i < n; ++i) {
    ctr += dfs[a[i] + i];
  }
  cout << ctr << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

typedef unsigned long long ull;
typedef long long ll;

ll DENOM = 1000000000LL + 7LL;
ll D2 = 998244353LL;
using namespace std;

ll n, m, k;

int main() {
  cin >> n;
  vector<ll> a(n);
  unordered_map<ll, vector<ll>> dfs;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    dfs[i - a[i]].push_back(i);
  }
  ll ctr = 0;

  for (int i = 0; i < n; ++i) {
    auto v = dfs[a[i] + i];
    ctr += v.size();
  }
  cout << ctr << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

/*** 𝕒𝕜𝕤𝕙𝕒𝕪𝕜𝕦𝕞𝕒𝕣𝟡𝟡 ***/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using id_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define f(i, x, n) for (i = x; i < n; i++)
#define fr(i, x, n) for (i = x; i >= n; i--)
#define pu push_back
#define pp pop_back
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SP(x) fixed << setprecision(x)
#define IOS()                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long INF = INT_MAX;
const long long LINF = LLONG_MAX;
const long long N = 1e5 + 1;

typedef long long ll;
typedef long double ld;

void test_case() {
  ll n, i, j;
  cin >> n;
  vector<ll> a(n), v(n);
  // multiset<ll> s;
  map<ll, ll> m;
  f(i, 0, n) {
    cin >> a[i];
    v[i] = a[i];
    a[i] = i - a[i];
    m[a[i]]++;
    // s.insert(a[i]);
  }
  ll ans = 0;
  f(i, 0, n) {
    ll key = v[i] + i;
    ans += m[key];
    m[a[i]]--;
    // ans+=s.count(key);
    // s.erase((a[i]));
  }
  cout << ans << '\\n';
}

int32_t main() {
  IOS();

  int tc = 1;
  // cin>>tc;
  while (tc--)
    test_case();
  return 0;
}

Error: TLE

Fixed submission:

/*** 𝕒𝕜𝕤𝕙𝕒𝕪𝕜𝕦𝕞𝕒𝕣𝟡𝟡 ***/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using id_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define f(i, x, n) for (i = x; i < n; i++)
#define fr(i, x, n) for (i = x; i >= n; i--)
#define pu push_back
#define pp pop_back
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SP(x) fixed << setprecision(x)
#define IOS()                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long INF = INT_MAX;
const long long LINF = LLONG_MAX;
const long long N = 1e5 + 1;

typedef long long ll;
typedef long double ld;

void test_case() {
  ll n, i, j;
  cin >> n;
  vector<ll> a(n), v(n);
  multiset<ll> s;
  f(i, 0, n) {
    cin >> a[i];
    v[i] = a[i];
    a[i] = i - a[i];
    s.insert(a[i]);
  }
  ll ans = 0;
  f(i, 0, n) {
    ll key = v[i] + i;
    ans += s.count(key);
    s.erase((a[i]));
  }
  cout << ans << '\\n';
}

int32_t main() {
  IOS();

  int tc = 1;
  // cin>>tc;
  while (tc--)
    test_case();
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ma 2e5

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

  vector<int> aj(n + 1);
  for (int i = 1; i <= n; i++)
    aj[i] = i - a[i];
  vector<int> ai(n + 1);
  for (int i = 1; i <= n; i++)
    ai[i] = i + a[i];

  int ans = 0;
  for (int i = 1; i < n; i++)
    for (int j = i + 1; j <= n; j++)
      if (aj[j] == ai[i])
        ans++;

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