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

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

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

  vector<int> A(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> A[i];

  vector<int> dp(N + 1, 0);

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

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

Error: TLE

Fixed submission:

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

int main(void) {
  int N;
  cin >> N;
  vector<P> A(N);
  for (int i = 0; i < N; i++) {
    int tmp = 0;
    cin >> tmp;
    A.at(i).first = i + tmp;
    A.at(i).second = i - tmp;
  }
  int count = 0;
  for (int i = 0; i < N; i++)
    for (int j = i; j < N; j++)
      if (A.at(i).first == A.at(j).second)
        count++;
  cout << count << 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++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define all(x) (x).begin(), (x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<ll, ll> P;
constexpr auto INF = INT_MAX >> 1;
constexpr auto LINF = 5000000000000000;
constexpr auto MOD = 1000000007;

int main() {

  int n;
  cin >> n;
  V<ll> a(n);
  V<ll> b(n);
  rep(i, n) {
    cin >> a[i];
    b[i] = a[i] + i;
  }

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

  rep(i, n - 1) {
    cnt[b[i]]++;
    ll k = 2 * (i + 1) - b[i + 1];
    ans += cnt[k];
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define all(x) (x).begin(), (x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<ll, ll> P;
constexpr auto INF = INT_MAX >> 1;
constexpr auto LINF = 5000000000000000;
constexpr auto MOD = 1000000007;

int main() {

  int n;
  cin >> n;
  vi a(n);
  vi b(n);
  rep(i, n) {
    cin >> a[i];
    b[i] = a[i] + i;
  }

  multiset<int> se = {};
  int ans = 0;

  rep(i, n - 1) {
    se.insert(b[i]);
    int k = 2 * (i + 1) - b[i + 1];
    ans += count(all(se), k);
  }

  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;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    a[i] = h[i] + i;
    b[i] = h[i] - i;
  }
  unordered_map<int, vector<int>> indices;
  for (int i = 0; i < n; i++) {
    indices[b[i]].push_back(i);
  }

  ll ans = 0LL;
  // a[i] + b[j] = 0 (j > i)
  for (int i = 0; i < n; i++) {
    // index j where b[j] = -a[i]
    vector<int> index = indices[-a[i]];
    auto pointer = upper_bound(index.begin(), index.end(), i);
    ll d = (ll)distance(pointer, index.end());
    ans += d;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

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

  ll ans = 0;
  // a[i] + b[j] = 0 (j > i)
  for (ll i = 0LL; i < n; i++) {
    // index j where b[j] = -a[i]
    vector<ll> index = indices[-a[i]];
    auto pointer = upper_bound(index.begin(), index.end(), i);
    ll d = distance(pointer, index.end());
    ans += d;
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define x first
#define y second
#define pii pair<int, int>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
const int maxn = 3e5 + 35;
map<LL, int> mp;
int n, a[maxn];
int main() {
  cin >> n;
  LL ans = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (mp.count(i - a[i]))
      ans += mp[i - a[i]];
    mp[i + a[i]] += 1;
  }
  cout << ans << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define x first
#define y second
#define pii pair<int, int>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
const int maxn = 5e3 + 35;
map<LL, int> mp;
int n, a[maxn];
int main() {
  cin >> n;
  LL ans = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (mp.count(i - a[i]))
      ans += mp[i - a[i]];
    mp[i + a[i]] += 1;
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define ll long long

int main() {
  ll N;
  scanf("%lld", &N);

  vector<ll> sum(N);
  map<ll, ll> backmap;
  for (ll i = 0; i < N; i++) {
    ll A;
    scanf("%lld", &A);
    if (i - A > 0)
      backmap[i - A]++;
    sum[i] = i + A;
  }

  ll ans = 0;
  for (ll i = 0; i < N; i++)
    ans += backmap[sum[i]];
  printf("%lld\\n", ans);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define ll long long

int main() {
  ll N;
  scanf("%lld", &N);

  vector<ll> A(N);
  vector<ll> pairbackvector;
  vector<ll> sum(N);
  for (ll i = 0; i < N; i++) {
    scanf("%lld", &(A[i]));
    if (i - A[i] > 0)
      pairbackvector.push_back(i - A[i]);
    sum[i] = i + A[i];
  }

  ll ans = 0;
  sort(sum.begin(), sum.end());
  for (auto key : pairbackvector) {
    long left = 0, right = (long)sum.size() - 1; // 配列 a の左端と右端
    while (right >= left) {
      long mid = left + (right - left) / 2; // 区間の真ん中
      if (sum[mid] == key) {
        for (ll fr = mid; sum[fr] == key; fr++)
          ans++;
        for (ll bk = mid; sum[bk] == key; bk--)
          ans++;
        ans--;
        break;
      } else if (sum[mid] > key)
        right = mid - 1;
      else if (sum[mid] < key)
        left = mid + 1;
    }
  }
  printf("%lld\\n", ans);
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>

using namespace std;

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, h;
  cin >> n;
  vector<pair<int, int>> v;
  for (int i = 0; i < n; ++i) {
    cin >> h;
    if (h > n)
      continue;
    v.emplace_back(h, i);
  }
  sort(v.begin(), v.end());
  int cnt = 0;
  int l = 0, r;
  while (l < v.size()) {
    r = l + 1;
    while (r < v.size()) {
      int sum = v[l].first + v[r].first;
      if (sum >= n)
        break;
      else {
        if (sum == abs(v[l].second - v[r].second))
          cnt++;
        r++;
      }
    }
    l++;
  }
  cout << cnt << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 *	created: 23.08.2020 10:55:16
 **/
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using bint = boost::multiprecision::cpp_int;
using namespace std;
using ll = long long;
using P = pair<int, int>;
// #define endl '\\n'
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pcnt(bit) __builtin_popcountll(bit)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long double pi = acos(-1.0);
const int MAX = 1000010;
const int INF = 1ll << 60;
const int MOD = 1000000007;
// const int MOD = 998244353;

signed main() {

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

  int n;
  cin >> n;
  map<int, int> s;
  map<int, int> d;
  rep(i, n) {
    int a;
    cin >> a;
    s[i + 1 + a]++;
    d[i + 1 - a]++;
  }
  // j - a[j] = i + a[i]
  // i - a[i] = j + a[j]
  int ans = 0;
  for (auto p : s)
    ans += d[p.first] * p.second;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

signed main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);

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

  int cnt = 0;
  rep(i, n - 2) rep2(j, i + 2, n) {
    if (j - i == a[i] + a[j])
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>
using namespace std;
#define ll long long
void read(__int128 &x) {
  x = 0;
  char ch;
  int flag = 1;
  while ((ch = getchar())) {
    if (ch == '-')
      flag = -1;
    if (ch >= '0' && ch <= '9')
      break;
  }
  x = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
  }
  x *= flag;
}

void out(__int128 x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10)
    out(x / 10);
  putchar(x % 10 + '0');
}
ll quick(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a;
    a = a * a;
    b >>= 1;
  }
  return ans;
}

map<int, ll> vis;
int x[200005];
int y[200005];
int main() {

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int xx;
    scanf("%d", &xx);
    x[i] = i + xx;
    vis[i - xx]++;
  }

  ll sum = 0;

  for (int i = 1; i <= n; i++) {
    sum += vis[x[i]];
  }
  printf("%lld\\n", sum);
}


Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>
using namespace std;
#define ll long long
void read(__int128 &x) {
  x = 0;
  char ch;
  int flag = 1;
  while ((ch = getchar())) {
    if (ch == '-')
      flag = -1;
    if (ch >= '0' && ch <= '9')
      break;
  }
  x = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
  }
  x *= flag;
}

void out(__int128 x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10)
    out(x / 10);
  putchar(x % 10 + '0');
}
ll quick(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a;
    a = a * a;
    b >>= 1;
  }
  return ans;
}

struct node {
  int v;
  int id;
} a[200005];

int main() {

  int n;
  cin >> n;

  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x <= n) {
      cnt++;
      a[cnt].v = x;
      a[cnt].id = i;
    }
  }

  int sum = 0;

  for (int i = 1; i <= cnt; i++) {

    for (int j = i + 1; j <= cnt; j++) {
      if (a[i].v + a[j].v == a[j].id - a[i].id)
        sum++;
    }
  }
  printf("%d\\n", sum);
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error


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

const int N = 2e5 + 20;

int n, a[N];
long long ans;
map<int, int> m;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    ans += m[a[i] - i];
    m[-a[i] - i]++;
  }

  cout << ans;
}

Error: 0

Fixed submission:

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

const int N = 1e5 + 10;

int a[N];
int n;

int main() {

  cin >> n;
  unordered_map<int, int> m;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

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

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

#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  // vector<int> a(n);
  map<int, int> mp;
  long long ans = 0;

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

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

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

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

  int c = 0;
  for (int j = 1; j < n; j++) {
    c += count(h_plus_id.begin(), h_plus_id.begin() + j - 1, j - height[j]);
  }

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

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

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  unordered_map<ll, ll> mp;
  unordered_map<ll, ll> mp2;
  for (int i = 1; i <= n; i++) {
    ll tmp = a[i] + i;
    if (mp.count(tmp) == 0) {
      mp[tmp] = 1;
    } else {
      mp[tmp] += 1;
    }
    tmp = i - a[i];
    if (mp2.count(tmp) == 0) {
      mp2[tmp] = 1;
    } else {
      mp2[tmp] += 1;
    }
  }

  ll ans = 0;
  for (auto i = mp.begin(); i != mp.end(); i++) {
    if (mp2.count((*i).first) != 0) {
      ans += (*i).second * mp2[(*i).first];
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  unordered_map<ll, int> mp[n + 1];
  for (int i = 1; i <= n; i++) {
    mp[i] = mp[i - 1];
    ll tmp = a[i] + i;
    if (mp[i].count(tmp) == 0) {
      mp[i][tmp] = 1;
    } else {
      mp[i][tmp] += 1;
    }
  }

  ll ans = 0;
  for (int i = n; i > 0; i--) {
    ll tmp = i - a[i];
    if (mp[i].count(tmp) != 0) {
      ans += mp[i][tmp];
    }
  }
  cout << ans << endl;

  return 0;
}
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;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    a.at(i) -= i;
  }
  map<int, int> st;
  st[0 - a.at(0)] = 1;
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    if (st.count(a.at(i)))
      ans += st.at(a.at(i));
    int x = 0 - (a.at(i) + i) - i;
    if (st.count(x))
      st.at(x)++;
    else
      st[x] = 1;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> height(N);
  for (int i = 0; i < N; i++) {
    cin >> height.at(i);
  }
  // 身長の和と番号の差が同じ
  int counter = 0;
  for (int i = 0; i < N; i++) {
    if (height.at(i) > N - i - 2) {
      continue;
    } else {
      for (int j = i + height.at(i) + 1; j < N; j++) {
        int height_sum, num_sub;
        height_sum = height.at(i) + height.at(j);
        num_sub = j - i;
        if (height_sum == num_sub) {
          counter++;
        } else {
        }
      }
    }
  }
  cout << counter << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define LL long long
#define MOD 1000000007
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define print(a, n)                                                            \\
  for (LL e = 0; e < n; e++)                                                   \\
    cout << a[e] << " ";                                                       \\
  cout << endl
#define read(a, n)                                                             \\
  for (LL e = 0; e < n; e++)                                                   \\
    cin >> a[e];
using namespace std;

int main() {
  fastio;
  LL n;
  cin >> n;
  LL a[n];
  read(a, n);

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

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

  cout << ans << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define MOD 1000000007
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define print(a, n)                                                            \\
  for (LL e = 0; e < n; e++)                                                   \\
    cout << a[e] << " ";                                                       \\
  cout << endl
#define read(a, n)                                                             \\
  for (LL e = 0; e < n; e++)                                                   \\
    cin >> a[e];
using namespace std;

int main() {
  fastio;
  LL n;
  cin >> n;
  LL a[n];
  read(a, n);

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

  cout << ans << "\\n";

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

// 5/2

#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>;
using Graph = vector<vector<int>>;

// ll A[100010];

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

  vector<ll> A(N);

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

  map<ll, ll> Lmp, Rmp;

  rep(i, N) {
    ll L = A[i] + i;
    ll R = i - A[i];

    Lmp[L]++;

    if (R >= 0)
      Rmp[R]++;
  }

  ll ans = 0;

  for (auto itr = Lmp.begin(); itr != Lmp.end(); ++itr) {
    // cout << "key = " << itr->first << ", val = " << itr->second << endl;

    ll value = itr->first;

    ans += Lmp[value] * Rmp[value];
  }

  // cout << Lmp[10] << endl;  //
  // なんと初期値はゼロ！便利な...。コンパイラによっても違いそうな気がするが...。

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


Error: 0

Fixed submission:

// 5/2

#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>;
using Graph = vector<vector<int>>;

ll A[100010];

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

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

  map<ll, ll> Lmp, Rmp;

  rep(i, N) {
    ll L = A[i] + i;
    ll R = i - A[i];

    Lmp[L]++;

    if (R >= 0)
      Rmp[R]++;
  }

  ll ans = 0;

  for (auto itr = Lmp.begin(); itr != Lmp.end(); ++itr) {
    // cout << "key = " << itr->first << ", val = " << itr->second << endl;

    ll value = itr->first;

    ans += Lmp[value] * Rmp[value];
  }

  // cout << Lmp[10] << endl;  //
  // なんと初期値はゼロ！便利な...。コンパイラによっても違いそうな気がするが...。

  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 < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define ll int64_t
int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A.at(i);
  map<ll, ll> m;
  ll ans = 0;
  rep(i, N) {
    if (m.count(i - A.at(i) + 1))
      ans += m.at(i - A.at(i) + 1);
    if (!m.count(i + A.at(i) + 1))
      m[i + A.at(i) + 1] = 0;
    ++m[i + A.at(i) + 1];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define ll int64_t
int main() {
  int N;
  cin >> N;
  vector<ll> a(N);
  rep(i, N) cin >> a.at(i);
  ll count = 0;
  rep(i, N) {
    for (int j = i + 1; j < N; j++) {
      if (a.at(j) + a.at(i) == j - i)
        count++;
    }
  }
  cout << count << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

#define int long long
int a[200010];
signed main() {
  int n;
  cin >> n;
  int ans = 0;
  map<int, int> mt;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    int t2 = i + a[i];
    if (t2 <= n && t2 > 0) {
      ++mt[t2];
    }
  }
  for (int i = 1; i <= n; ++i) {
    int t1 = i - a[i];
    if (t1 > 0) {
      ans += mt[t1];
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

#define int long long
int a[100010];
signed main() {
  int n;
  cin >> n;
  int ans = 0;
  map<int, int> mt;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    int t2 = i + a[i];
    if (t2 <= n && t2 > 0) {
      ++mt[t2];
    }
  }
  for (int i = 1; i <= n; ++i) {
    int t1 = i - a[i];
    if (t1 > 0) {
      ans += mt[t1];
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  map<int, int> m1, m2;
  for (int i = 0; i < n; i++) {
    m1[i + v[i]]++;
    m2[i - v[i]]++;
  }
  long long ans = 0;
  for (auto x : m1) {
    if (m2.find(x.first) != m2.end())
      ans += ((long long)(x.second)) * (m2[x.first]);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define ll long long

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

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

#define myfor(i, N) for (i = 0; i < N; i++)
#define myforFL(i, f, l) for (i = f; i < l; i++)
#define myforInv(i, N) for (i = N; i >= 0; i--)
#define myforFLInv(i, f, l) for (i = f; i > l; i--)
using namespace std;

int main() {
  long N, i, j, A[200000], L[200000], R[200000], ans = 0;
  map<long, long> mpL, mpR;
  cin >> N;
  myfor(i, N) {
    cin >> A[i];
    L[i] = A[i] + i;
    R[i] = i - A[i];
    mpL[L[i]]++;
    mpR[R[i]]++;
  }
  for (auto itr = mpL.begin(); itr != mpL.end(); ++itr) {
    ans += mpR[itr->first] * itr->second;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define myfor(i, N) for (i = 0; i < N; i++)
#define myforFL(i, f, l) for (i = f; i < l; i++)
#define myforInv(i, N) for (i = N; i >= 0; i--)
#define myforFLInv(i, f, l) for (i = f; i > l; i--)
using namespace std;
int main() {
  long N, i, j, A[200000], ans = 0;
  cin >> N;
  myfor(i, N) cin >> A[i];
  myfor(i, N) {
    j = i + A[i];
    while (j < N) {
      if (A[j] + A[i] == j - i)
        ans++;
      j++;
    }
  }
  cout << ans << endl;

  return 0;
}