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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> m;

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

  long long cnt = 0;
  for (int i = 0; i < n; i++) {
    int x = i - a[i];
    if (x <= 0)
      continue;
    cnt += m[x];
  }

  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  multimap<int, int> m;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m.insert(make_pair(i - a[i], i));
  }

  long int cnt = 0;
  for (int i = 0; i < n; i++) {
    int x = i + a[i];
    auto ret = m.equal_range(x);

    cnt += distance(ret.first, ret.second);
  }

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

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

using ll = long long;

constexpr ll MOD = 1e9 + 7;
constexpr ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

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

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

  std::cout << res << std::endl;
}


Error: TLE

Fixed submission:

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

int main(int argc, char **argv) {
  ll N;
  cin >> N;
  vector<ll> H(N);
  for (ll i = 0; i < N; ++i)
    cin >> H[i];

  ll res{0};
  for (ll i = 0; i < N; ++i) {
    for (ll j = i + 1; j < N; ++j) {
      if (j - i == H[i] + H[j])
        ++res;
    }
  }
  std::cout << res << std::endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  vector<pair<int64_t, int64_t>> a(n);
  for (int i = 0; i < n; i++) {
    a[i].first = i + 1;
    cin >> a[i].second;
  }

  // j - i = h_i + h_j
  // => i + h_i = j - h_j
  vector<int64_t> l(n), r(n);
  for (int i = 0; i < n; i++) {
    l[i] = a[i].first + a[i].second;
    r[i] = a[i].first - a[i].second;
  }

  map<int64_t, int64_t> m;
  for (int i = 0; i < n; i++) {
    if (m.find(l[i]) != m.end())
      m[l[i]]++;
    else
      m[l[i]] = 1;
  }

  int64_t ans = 0;
  for (int i = 0; i < n; i++) {
    if (m.find(r[i]) != m.end())
      ans += m[r[i]];
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  vector<pair<int64_t, int64_t>> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i + 1;
  }

  int64_t ans = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (abs(a[i].second - a[j].second) == a[i].first + a[j].first)
        ans++;
    }
  }
  cout << ans << endl;

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

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


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 1;
typedef long long LL;
LL x[N], l[N], r[N];

int main() {
  int i, j, n, ans = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    l[i] = 0;
    r[i] = 0;
  }
  for (i = 1; i <= n; i++) {
    cin >> x[i];
  }

  for (i = 1; i <= n; i++) {
    if ((i + x[i]) <= n)
      l[i + x[i]]++;
    if ((i - x[i]) >= 1)
      r[i - x[i]]++;
  }
  for (i = 1; i <= n; i++) {
    int ar1 = l[i - x[i]];
    int ar2 = r[i + x[i]];
    ans = ans + ar1 + ar2;
  }
  ans /= 2;
  /*
  for(i=1;i<=n;i++)
  {
          y=y+l[i]*r[i];
  }
  */
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#define ll long long
using namespace std;
const int maxn = 9e5 + 10;
ll a[maxn];
int main() {
  ll n;
  ll ans = 0;
  cin >> n;
  map<ll, ll> mp;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    mp[i - a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    ans += mp[i + a[i]];
  }
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 2e6 + 20;
const int inf = 0x3f3f3f3f;
using namespace std;
int a[N], f[N];
int tot = 0, n = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    f[i - a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    tot += f[i + a[i]];
  }
  cout << tot;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define ll long long int
const ll mod = 20123;
using namespace std;
ll n, m;
string s;
ll a[200010];
map<ll, ll> p1, p2;
int main() {
  ios::sync_with_stdio(false);
  ll i = 0, j, flat1 = 0, flat2 = 0, x, k, y, z, t;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    p1[a[i] + i]++;
    p2[i - a[i]]++;
  }
  map<ll, ll>::iterator it;
  ll sum = 0;
  for (it = p1.begin(); it != p1.end(); it++) {
    sum += (it->second) * p2[it->first];
  }
  cout << sum;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define ll long long int
const ll mod = 20123;
using namespace std;
ll n, m;
string s;
ll a[100010];
map<ll, ll> p1, p2;
int main() {
  ios::sync_with_stdio(false);
  ll i = 0, j, flat1 = 0, flat2 = 0, x, k, y, z, t;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    p1[a[i] + i]++;
    p2[i - a[i]]++;
  }
  map<ll, ll>::iterator it;
  ll sum = 0;
  for (it = p1.begin(); it != p1.end(); it++) {
    sum += (it->second) * p2[it->first];
  }
  cout << sum;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef unsigned long long ull;
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <typename T> T maxx(T a, T b) {
  if (a >= b)
    return a;
  else
    return b;
}
template <typename T> T minn(T a, T b) {
  if (a <= b)
    return a;
  else
    return b;
}
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1)
      res = ((res % p) * (x % p)) % p;
    y >>= 1;
    x = (x * x) % p;
  }
  return res;
}
ll powerfew(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res *= x;
    y >>= 1;
    x *= x;
  }
  return res;
}
#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define loop(i, n) for (int i = 1; i <= n; ++i)
#define mem(x, y) memset(x, y, sizeof(x))
#define PI 3.141592653589
#define INF 0x3f3f3f3f //(1061109567)
const ll MAX = 2e5 + 10;
ll a[1000000], b[1000000], c[1000000];
int main() {
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (ll i = 1; i <= n; i++) {
    if ((i + a[i]) <= n) {
      b[i + a[i]]++;
    }
  }
  for (ll i = 1; i <= n; i++) {
    if ((i - a[i]) > 0) {
      c[i - a[i]]++;
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans += c[i] * b[i];
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#define endl '\\n'
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;

#define int long long

int a[100010], sum[100010];
signed main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  int ans = 0;
  for (int len = 2; len <= n; ++len) {
    for (int i = 1, j = i + len; j <= n; ++i, ++j) {
      if (len == a[i] + a[j]) {
        ++ans;
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>

#define ll long long

using namespace std;

long long gcd(int x, int y) {
  if (x == 0)
    return y;

  return gcd(y % x, x);
}

long long lcm(int x, int y) {
  if (y == 0) {
    return x;
  }

  return x * y / gcd(x, y);
}

int min(int a, int b) { return a > b ? b : a; }

int max(int a, int b) { return a > b ? a : b; }

int main() {
  cin.sync_with_stdio(false);

  int n;
  cin >> n;

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

  cout << res << endl;
}

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;
  pii A[n + 1];
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    A[i] = {a, i};
  }
  sort(A + 1, A + n + 1);
  int stop = n, ans = 0;
  for (int i = 1; i <= n; i++) {
    if (i > stop)
      break;
    for (int j = i + 1; j <= stop; j++) {
      if (A[i].FF + A[j].FF > n - 1) {
        stop = j;
        break;
      }
      if (A[i].FF + A[j].FF == abs(A[i].SS - A[j].SS))
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define for1(i, n) for (int i = 1; i <= int(n); ++i)
#define ms(a, x) memset(a, x, sizeof(a))
#define F first
#define S second
#define all(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
template <typename... Args> void rd(Args &...args) { ((cin >> args), ...); }
template <typename... Args> void write(Args... args) {
  ((cout << args << " "), ...);
  cout << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for1(i, n) cin >> a[i];
  unordered_map<int, int> cnt;
  ll ans = 0;
  for1(i, n) {
    ans += cnt[-(a[i] - i)];
    cnt[a[i] + i]++;
  }
  cout << ans;
  return 0;
}


Error: 0

Fixed submission:

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

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> l(n), r(n);
  vector<int> d(n, 0);
  rep(i, n) {
    l[i] = i + a[i];
    r[i] = i - a[i];
    if (r[i] > 0)
      d[r[i]] += 1;
  }
  ll ans = 0;
  rep(i, n) {
    if (l[i] < n)
      ans += d[l[i]];
  }
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

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

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

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

const int maxn = 2e5 + 100;
int n;
int a[maxn];
map<int, int> b;
// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  // Failed to predict input format
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    b[i - a[i]]++;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += b[a[i] + i];
    b[i - a[i]]--;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

const int maxn = 1e5 + 100;
int n;
int a[maxn];
map<int, int> b;
// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  // Failed to predict input format
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    b[i - a[i]]++;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += b[a[i] + i];
    b[i - a[i]]--;
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  map<long long, long long> A, B;
  for (int i = 0; i < N; i++) {
    long long h;
    cin >> h;
    A[i + h] += 1;
    B[i - h] += 1;
  }

  long long ans = 0;
  for (int i = 0; i < N; i++)
    ans += A[i] * B[i];

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  long long ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      if (A[i] + A[j] == j - i)
        ans++;
    }
  }

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

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;
}

const int INF = (1 << 30) - 1;
const ll INFLL = (1LL << 61) - 1;
const int MOD = 1000000007;
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) { cin >> A[i]; }
  map<int, int> mp;
  ll ans = 0;
  FOR(i, 1, N) {
    mp[A[i - 1] + i - 1]++;
    ans += mp[i - A[i]];
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

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;
}

const int INF = (1 << 30) - 1;
const ll INFLL = (1LL << 61) - 1;
const int MOD = 1000000007;
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) { cin >> A[i]; }
  multiset<int> se;
  ll ans = 0;
  FOR(i, 1, N) {
    se.insert(A[i - 1] + i - 1);
    ans += se.count(i - A[i]);
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vchar = vector<char>;
using vvchar = vector<vchar>;
using vp = vector<P>;
using vpp = vector<pair<P, P>>;
using vvp = vector<vp>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
istream &operator>>(istream &is, P &a) { return is >> a.first >> a.second; }
ostream &operator<<(ostream &os, const P &a) {
  return os << "(" << a.first << "," << a.second << ")";
}

template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
#pragma endregion

int main() {
  int n;
  cin >> n;
  vint a(n);
  rep(i, n) cin >> a[i];

  map<ll, ll> l, r;
  rep(i, n) {
    l[i + a[i]]++;
    r[i - a[i]]++;
  }

  ll ans = 0;
  rep(i, n) { ans += l[i] * r[i]; }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vchar = vector<char>;
using vvchar = vector<vchar>;
using vp = vector<P>;
using vpp = vector<pair<P, P>>;
using vvp = vector<vp>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
istream &operator>>(istream &is, P &a) { return is >> a.first >> a.second; }
ostream &operator<<(ostream &os, const P &a) {
  return os << "(" << a.first << "," << a.second << ")";
}

template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
#pragma endregion

int main() {
  int n;
  cin >> n;
  vint a(n);
  rep(i, n) cin >> a[i];

  int ans = 0;
  rep(i, n) {
    for (int j = i + a[i]; 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;

void solver() {
  int n;
  cin >> n;
  vector<int> as(n);

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

  vector<int> bs(n);
  vector<int> cs(n);
  map<long long, int> mpb;
  map<long long, int> mpc;
  for (int i = 0; i < n; i++) {
    mpb[(i + 1) + as[i]]++;
    mpc[(i + 1) - as[i]]++;
  }

  long long ans = 0;
  for (auto i = mpb.begin(); i != mpb.end(); ++i) {
    long long b = i->first;
    long long bn = i->second;
    ans += mpc[b] * bn;
  }
  cout << ans << "\\n";
}

int main() {
  solver();
  return 0;
}


Error: TLE

Fixed submission:

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

void solver() {
  int n;
  cin >> n;
  vector<int> as(n);

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

  long long ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if ((j + 1) - (i + 1) == as[i] + as[j])
        ans++;
    }
  }
  cout << ans << "\\n";
}

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

#include <iostream>
#include <unordered_map>
using namespace std;

int n, height;
long long ans;
unordered_map<int, int> pairOne;

int main() {
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> height;
    if (pairOne.count(i - height))
      ans += pairOne[i - height];
    pairOne[i + height]++;
  }

  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;

int n, height, ans;
int pairOne[2000001], pairTwo[2000001];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> height;
    pairOne[i] = i + height;
    pairTwo[i] = i - height;
  }

  for (int i = 1; i <= n; i++) {
    int nr = count(pairOne + 1, pairOne + i, pairTwo[i]);
    ans += nr;
  }
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;
using ll = long long;
#define endl '\\n'

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for (auto &x : a)
    cin >> x;

  unordered_map<ll, ll> cnt;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    cnt[i + a[i]]++;
    ans += cnt[i - a[i]];
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;
using ll = long long;
using indx = size_t;

#define endl '\\n'
#define get(type, args...)                                                     \\
  type args;                                                                   \\
  read(args)
#define all(v) v.begin(), v.end()
#define debug(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
template <typename... T> void read(T &...args) { ((cin >> args), ...); }
template <typename T> void read(vector<T> &v) {
  for (auto &x : v)
    cin >> x;
}
template <typename... T> void write(T &&...args) {
  ((cout << args << " "), ...);
  cout << '\\n';
}
template <typename T> void write(vector<T> &v) {
  for (auto x : v)
    cout << x << " ";
  cout << '\\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  get(ll, n);
  vector<ll> a(n);
  read(a);
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = i + a[i]; j < n; j++) {
      if (a[i] + a[j] == j - i)
        ans++;
    }
  }
  write(ans);
}