alexjercan/bugnet huggingface dataset:
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define eb emplace_back
// #define st first
#define sc second

using namespace std;

const ll mod = 1e9 + 7;
const int e = 2 * 1e5 + 69;
const int base = 311;

// dung inverse module de tinh nCk
// beginer 78
// mbeginer
// cach tao hoan vi tu n so : n*(n+1)/2;

// vector<pair<ll,ll>>adj[e];
// vector<ll>adj[e];

/*ll Pow(ll n, ll dem)
{
    if(dem == 0) return 1;
    if(dem == 1) return n ;
    ll t = Pow(n, dem/2);
    if(dem % 2 == 0) return (t*t);
    else return ((t * t) * n);
}*/

/*ll C(ll k, ll n)
{
    if(k == 0) return 1;
    if(n < k) return 0;
    return (gt[n]%mod * (igt[k]%mod * igt[n-k]%mod)%mod)%mod;
}*/

ll n, m, k;
ll h[e];

int main() {
  cin >> n;
  map<ll, ll> kt;
  map<ll, ll> kt1;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    kt[h[i] + i]++;
    kt1[i - h[i]]++;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += kt1[h[i] + i];
    // ans += kt[i - h[i]];
  }
  cout << ans;
}
/*

*/


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define eb emplace_back
// #define st first
#define sc second

using namespace std;

const ll mod = 1e9 + 7;
const int e = 1e5 + 69;
const int base = 311;

// dung inverse module de tinh nCk
// beginer 78
// mbeginer
// cach tao hoan vi tu n so : n*(n+1)/2;

// vector<pair<ll,ll>>adj[e];
// vector<ll>adj[e];

/*ll Pow(ll n, ll dem)
{
    if(dem == 0) return 1;
    if(dem == 1) return n ;
    ll t = Pow(n, dem/2);
    if(dem % 2 == 0) return (t*t);
    else return ((t * t) * n);
}*/

/*ll C(ll k, ll n)
{
    if(k == 0) return 1;
    if(n < k) return 0;
    return (gt[n]%mod * (igt[k]%mod * igt[n-k]%mod)%mod)%mod;
}*/

ll n, m, k;
ll h[e];

int main() {
  cin >> n;
  map<ll, ll> kt;
  map<ll, ll> kt1;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    kt[h[i] + i]++;
    kt1[i - h[i]]++;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += kt1[h[i] + i];
  }
  cout << ans;
}
/*

*/
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N;
  cin >> N;
  vector<ll> T(N);
  for (int i = 0; i < N; i++)
    cin >> T[i];
  map<ll, ll> mp;
  for (ll i = 0; i < N; i++)
    mp[i + T[i]]++;
  ll res = 0;
  for (ll i = 0; i < N; i++)
    res += mp[i - T[i]];
  cout << res << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N;
  cin >> N;
  vector<ll> data(N);
  for (ll i = 0; i < N; i++) {
    cin >> data[i];
  }
  vector<vector<int>> fi(N, vector<int>(N, 0));
  ll count = 0;
  for (ll i = 0; i < N; i++) {
    for (ll j = i + 1; j < N; j++) {
      if (data[i] + data[j] == abs(i - j))
        count++;
    }
  }
  cout << count << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll MOD = (ll)(1e9 + 7);
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int N;
int A[200000];

unordered_map<int, int> st;

signed main() {
  cin >> N;
  rep(i, N) cin >> A[i];

  ll ans = 0;
  rep(i, N) {
    if (st.count(i - A[i]))
      ans += st[i - A[i]];
    if (st.count(i + A[i]) == 0)
      st[i + A[i]] = 0;
    st[i + A[i]]++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll MOD = (ll)(1e9 + 7);
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int N;
int A[200000];

unordered_multiset<int> st;

signed main() {
  cin >> N;
  rep(i, N) cin >> A[i];

  ll ans = 0;
  rep(i, N) {
    ans += st.count(i - A[i]);
    st.insert(i + 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;

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

  for (int i = 0; i < n; i++) {
    m[a[i] + i]++;
  }
  for (int i = 0; i < n; i++) {
    ans += m[i - a[i]];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

bool isOK(int index, int key, vector<ll> a) {
  if (a[index] >= key)
    return true;
  else
    return false;
}

// 汎用的な二分探索のテンプレ
int binary_search(int key, vector<ll> a) {
  int left = -1; // 「index = 0」が条件を満たすこともあるので、初期値は -1
  int right = (int)a.size(); // 「index =
                             // a.size()-1」が条件を満たさないこともあるので、初期値は
                             // a.size()

  /* どんな二分探索でもここの書き方を変えずにできる！ */
  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (isOK(mid, key, a))
      right = mid;
    else
      left = mid;
  }

  /* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている
   */
  return right;
}

int main() {
  int n;
  cin >> n;
  vector<ll> p(n);
  vector<ll> m(n);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    p[i] = i + a;
    m[i] = i - a;
  }
  sort(p.begin(), p.end());
  sort(m.begin(), m.end());
  for (int i = 0; i < n; i++) {
    ans += binary_search(p[i] + 1, m) - binary_search(p[i], m);
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define CFS                                                                    \\
  cin.tie(nullptr);                                                            \\
  ios::sync_with_stdio(false);
#define CST(x) cout << fixed << setprecision(x);
using namespace std;
constexpr int IINF = numeric_limits<int>::max();
constexpr long LINF = numeric_limits<long>::max();
constexpr int MOD = (int)1e9 + 7;
// constexpr int MOD = 998244353;

int main() {
  int n;
  cin >> n;
  map<int, long> m1;
  map<int, long> m2;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    m1[i - a]++;
    m2[i + a]++;
  }
  long ans = 0;
  for (const auto p : m1) {
    ans += p.second * m2[p.first];
  }
  cout << ans << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define CFS                                                                    \\
  cin.tie(nullptr);                                                            \\
  ios::sync_with_stdio(false);
#define CST(x) cout << fixed << setprecision(x);
using namespace std;
constexpr int IINF = numeric_limits<int>::max();
constexpr long LINF = numeric_limits<long>::max();
constexpr int MOD = (int)1e9 + 7;
// constexpr int MOD = 998244353;

int main() {
  int n;
  cin >> n;
  vector<int> v(n + 1);
  for (size_t i = 1; i <= n; i++) {
    cin >> v[i];
  }
  int ans = 0;
  for (int w = 1; w < n; w++) {
    for (int i = 1; i <= n - w; i++) {
      int j = i + w;
      if (j - i == v[i] + v[j]) {
        ans++;
      }
    }
  }
  cout << ans << "\\n";
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define loop(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define dump(a) cerr << #a << " = " << (a) << "(L:" << __LINE__ << ")" << endl

void solve(long long N, std::vector<long long> A) {
  vector<long long> X(N), Y(N);
  rep(i, N) X[i] = i + A[i], Y[i] = i - A[i];
  map<long long, int> cnt;
  long long ans = 0;
  rep(i, N) {
    ans += cnt[Y[i]];
    cnt[X[i]]++;
  }
  cout << ans << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}


Error: TLE

Fixed submission:

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

#define loop(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define dump(a) cerr << #a << " = " << (a) << "(L:" << __LINE__ << ")" << endl

void solve(long long N, std::vector<long long> A) {
  int ans = 0;
  rep(i, N) loop(j, i + 1, N) ans += j - i == A[i] + A[j];
  cout << ans << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long n;
  map<long long, long long> dl;
  map<long long, long long> dr;

  cin >> n;

  long long a[n + 1];

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

  long long l[n + 1], r[n + 1];

  long long maxv = 0;

  for (long long i = 1; i <= n; i++) {
    l[i] = i + a[i];
    r[i] = i - a[i];
    maxv = max(r[i], maxv);
  }

  // printf("maxv %lld\\n", maxv);

  for (long long i = 1; i <= n; i++) {
    dl[l[i]] = 0;
    dr[r[i]] = 0;
  }

  for (long long i = 1; i <= n; i++) {
    dl[l[i]]++;
    dr[r[i]]++;
  }

  /*for(long long i = 1; i <= n; i++){
      printf("dl %lld l %lld \\n", dl[l[i]], l[i]);
      printf("dr %lld r %lld\\n", dr[r[i]], r[i]);
  }*/

  long long count = 0;

  long long c, d;

  for (long long x = 2; x <= maxv; x++) {
    c = dl[x];
    d = dr[x];
    if (c > 0 and d > 0) {
      count = count + c * (d);
    }
  }

  printf("%lld\\n", count);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

int main() {
  long long n;

  cin >> n;

  long long a[n + 1];

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

  long long count = 0;

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

  printf("%lld\\n", count);

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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#define mem(x) memset(x, 0, sizeof(x))
#define sca(x) scanf("%d", &x)
#define scaa(x, y) scanf("%d%d", &x, &y)
#define inf 0x7f7f7f7f
#define io ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#pragma GCC optimize(2)
using namespace std;
const double pi = acos(-1);
const double eps = 1e-6;
typedef pair<long, long> pll;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<char, char> pcc;
int a[200005];
int main() {
  int n;
  cin >> n;
  map<int, int> m;
  for (int i = 0; i < n; i++)
    cin >> a[i], m[i - a[i]]++;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += (ll)m[i + a[i]];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#define mem(x) memset(x, 0, sizeof(x))
#define sca(x) scanf("%d", &x)
#define scaa(x, y) scanf("%d%d", &x, &y)
#define inf 0x7f7f7f7f
#define io ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#pragma GCC optimize(2)
using namespace std;
const double pi = acos(-1);
const double eps = 1e-6;
typedef pair<long, long> pll;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<char, char> pcc;
struct cc {
  int x, in;
} p[200005];
bool cmp(cc a, cc b) { return a.x < b.x; }
int main() {
  int n;
  sca(n);
  for (int i = 1; i <= n; i++) {
    sca(p[i].x);
    p[i].in = i;
  }
  sort(p + 1, p + 1 + n, cmp);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int y = p[i].x + n - 1;
    for (int j = i + 1; j <= n; j++) {
      if (p[j].x > y)
        break;
      if (abs(p[i].in - p[j].in) == p[i].x + p[j].x)
        ans++;
    }
  }
  printf("%d\\n", ans);
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int N;
  cin >> N;
  ll ans = 0;
  vector<pair<ll, ll>> A(N);
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    A[i].first = i + 1 + a;
    A[i].second = i + 1 - a;
  }
  vector<int> B(N + 1, 0);
  for (int i = N - 1; i >= 0; i--) {
    if (A[i].first <= N) {
      ans += B[A[i].first];
    }
    if (A[i].second >= 0) {
      B[A[i].second]++;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int N;
  cin >> N;
  ll ans = 0;
  vector<pair<ll, ll>> A(N);
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    A[i].first = i + 1 + a;
    A[i].second = i + 1 - a;
  }
  vector<int> B(N + 1, 0);
  for (int i = N - 1; i >= 0; i--) {
    ans += B[A[i].first];
    if (A[i].second >= 0) {
      B[A[i].second]++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, l;
  long long cnt = 0;
  cin >> n;
  vector<int> wa(n);
  vector<int> table(3000000, 0);

  for (int i = 1; i < n + 1; i++) {
    cin >> l;
    wa.at(i - 1) = l + i;

    if (i - l >= 0) {
      table.at(i - l)++;
    }
  }

  for (int i = 0; i < n; i++) {
    if (wa.at(i) <= 2100000) {
      cnt += table.at(wa.at(i));
    }
  }
  cout << cnt << endl;
}


Error: 0

Fixed submission:

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

int main() {
  int64_t n, l, cnt = 0;
  cin >> n;
  vector<int64_t> wa(n);
  vector<int64_t> table(200010, 0);

  for (int64_t i = 1; i < n + 1; i++) {
    cin >> l;
    wa.at(i - 1) = l + i;

    if (i - l >= 0) {
      table.at(i - l)++;
    }
  }
  for (int64_t i = 0; i < n; i++) {
    cnt += table.at(wa.at(i));
  }
  cout << cnt << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
#define _USE_MATH_DEFINES
#include <algorithm>
#include <math.h>
#include <memory>
#include <string>
#define Rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
void swap(int &a, int &b) {
  int temp = a;
  a = b;
  b = temp;
  return;
}
int ncr(int n, int r) {
  int a = 1;
  Rep(i, r) a = (a * (n - i)) / (i + 1);
  return a;
}
int npr(int n, int r) {
  int a = 1;
  Rep(i, r) a *= n - i;
  return a;
}
int mod(int a, int b) { return a % b; }

class Human {
public:
  ll number;
  ll tall;
  ll R, L;
  Human(int _i, ll _a) : tall(_a), number(_i) {
    R = number - tall;
    L = number + tall;
  }
};

int main() {
  ll n;
  ll ans = 0;
  std::vector<Human> A;
  std::cin >> n;
  Rep(i, n) {
    ll a;
    std::cin >> a;

    A.emplace_back(i + 1, a);
  }
  std::vector<ll> boxR(250000, 0);
  std::vector<ll> boxL(250000, 0);
  for (auto iter = A.begin(); iter != A.end(); iter++) {
    if (iter->R >= 0) {
      boxR[iter->R]++;
    }
    if (iter->L <= 200000) {
      boxL[iter->L]++;
    }
  }
  Rep(i, 250000) { ans += boxR[i] * boxL[i]; }
  std::cout << ans << std::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>
#define _USE_MATH_DEFINES
#include <algorithm>
#include <math.h>
#include <memory>
#include <string>
#define Rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
void swap(int &a, int &b) {
  int temp = a;
  a = b;
  b = temp;
  return;
}
int ncr(int n, int r) {
  int a = 1;
  Rep(i, r) a = (a * (n - i)) / (i + 1);
  return a;
}
int npr(int n, int r) {
  int a = 1;
  Rep(i, r) a *= n - i;
  return a;
}
int mod(int a, int b) { return a % b; }

class Human {
public:
  int number;
  int tall;
  ll R, L;
  Human(int _i, ll _a) : tall(_a), number(_i) {
    R = number - tall;
    L = number + tall;
  }
};

int main() {
  int n;
  int ans = 0;
  std::vector<Human> A;
  std::cin >> n;
  Rep(i, n) {
    ll a;
    std::cin >> a;

    A.emplace_back(i + 1, a);
  }
  std::vector<int> boxR(100000000, 0);
  std::vector<int> boxL(100000000, 0);
  for (auto iter = A.begin(); iter != A.end(); iter++) {
    if (iter->R >= 0) {
      boxR[iter->R]++;
    }
    boxL[iter->L]++;
  }
  Rep(i, 100000000) { ans += boxR[i] * boxL[i]; }
  std::cout << ans << std::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 rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; (i)--)
#define rrep1(i, n) for (int i = ((int)(n)); i > 0; (i)--)
#define sz(c) ((int)(c).size())
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;
const int MAX = 2e5 + 10;

int N;
int A[MAX];

void input() {
  cin >> N;
  rep1(i, N) { cin >> A[i]; }
}

void solve() {
  vector<int> v(N);
  ;
  rep1(i, N) v[i - 1] = i + A[i];
  sort(all(v));
  ll ans = 0;
  rep1(i, N) {
    ans += ll(upper_bound(all(v), i - A[i]) - lower_bound(all(v), i - A[i]));
  }
  cout << ans;
}

void output() {}

int main() {
  input();
  solve();
  output();
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; (i)--)
#define rrep1(i, n) for (int i = ((int)(n)); i > 0; (i)--)
#define sz(c) ((int)(c).size())
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;
const int MAX = 2e5 + 10;

int N;
int A[MAX];

void input() {
  cin >> N;
  rep1(i, N) { cin >> A[i]; }
}

void solve() {
  multiset<int> st;
  rep1(i, N) st.insert(i + A[i]);
  ll ans = 0;
  rep1(i, N) { ans += ll(st.count(i - A[i])); }
  cout << ans;
}

void output() {}

int main() {
  input();
  solve();
  output();
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(a) (long long)((a).size())

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

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(a) (long long)((a).size())

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

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

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

int main() {
  ll N;
  cin >> N;
  map<ll, ll> L;
  map<ll, ll> R;
  for (ll i = 0; i < N; i++) {
    ll A;
    cin >> A;
    L[i + A]++;
    R[i - A]++;
  }
  ll ans = 0;
  for (ll i = 1; i < 2 * N; i++) {
    ans += L[i] * R[i];
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll MOD = 1000000007;
int main() {
  ll N;
  cin >> N;
  vector<ll> A(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll ans = 0;
  for (ll i = 0; i < N - 1; i++) {
    ll count = 1;
    for (ll j = A[i] + i + 1; j < N; j++) {
      if (A[j] == count)
        ans++;
      count++;
    }
  }
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x6fffffff
#define INFL 0x6fffffffffffffffLL

int main() {
  ll b, c, i, j, k, n, m, x, y, ans = 0;

  cin >> n;
  vector<ll> a(n + 1);
  map<ll, ll> ii, jj;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    ii[i + a[i]]++;
    jj[i - a[i]]++;
  }

  for (i = 1; i < n; i++) {
    ans += ii[i] * jj[i];
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x6fffffff
#define INFL 0x6fffffffffffffffLL

int main() {
  ll b, c, i, j, k, n, m, x, y, ans = 0;
  string s;

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

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

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

map<int, int> a;
long long b[200005] = {0};

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


Error: -11

Fixed submission:

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

long long a[2000005] = {0}, b[2000005] = {0}, c[20000005] = {0};

int main() {
  long long n, i, cnt = 0;
  cin >> n;
  for (i = 2e5; i < n + 2e5; i++) {
    cin >> a[i];
    b[a[i] + i]++;
    c[i - a[i]]++;
  }
  for (i = 2e5; i <= n + 2e5; i++) {
    cnt += b[i] * c[i];
  }
  cout << cnt << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

// g++ -std=gnu++1y -O2 -I/opt/boost/gcc/include -L/opt/boost/gcc/lib -o ./a.out
// ./X.cpp & a.out //
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define ull unsigned long long

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n;
  cin >> n;

  vector<int> h;

  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;

    h.pb(x);
  }

  map<int, int> m;
  ll ans = 0;

  for (int i = 0; i < n; i++) {
    m[i + h[i]]++;
    ans += m[i - h[i]];
  }

  cout << ans << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

// g++ -std=gnu++1y -O2 -I/opt/boost/gcc/include -L/opt/boost/gcc/lib -o ./a.out
// ./X.cpp & a.out //
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define ull unsigned long long

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n;
  cin >> n;

  vector<ll> pref, suf;

  set<int> sufs;

  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;

    pref.pb(i - x);
    suf.pb(i + x);
    sufs.insert(x + i);
  }

  int ans = 0;

  map<int, vector<int>> cnt;

  for (int y : sufs) {
    vector<int> vec(n, 0);
    cnt[y] = vec;
    cnt[y][0] = (suf[0] == y ? 1 : 0);
    for (int i = 1; i < n; i++) {
      cnt[y][i] = cnt[y][i - 1];
      if (suf[i] == y)
        cnt[y][i] += 1;
    }
  }

  for (int a = 1; a < n; a++) {
    if (!cnt[pref[a]].empty())
      ans += cnt[pref[a]][a];
  }

  cout << ans << '\\n';

  return 0;
}