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

#include <algorithm> // count_ifに必要
#include <array>     // sizeはコンパイル時定数で固定
#include <iomanip> // cout << setprecision(15) << x で小数の出力精度を指定
#include <iostream>
#include <map>
#include <numeric> // accumulate, gcd
#include <queue>   // priority_queue も入ってる
#include <set>     // 要素数は size() で取得 multisetもある
#include <stack>
#include <string>
#include <tuple>
#include <utility> //pair
#include <vector>

using namespace std;
// 問題が起こるまでintは使用禁止2020-04-13
using ll = long long;

template <class InputIterator, class T>
bool contains(InputIterator iterator, const T &value) {
  if (find(iterator.begin(), iterator.end(), value) == iterator.end())
    return false;
  else
    return true;
}

template <class InputIterator> auto sum(InputIterator iterator) {
  return accumulate(iterator.begin(), iterator.end(), 0);
}

// endは排他的(exclusive)なので注意
vector<ll> range(ll begin, ll end) {
  vector<ll> result;
  for (ll i = begin; i < end; ++i) {
    result.push_back(i);
  }
  return result;
}

// =========ここまでテンプレ============

int main() {
  ll n;
  cin >> n;
  map<ll, ll> sums;
  ll count = 0;
  for (ll i = 1; i <= n; ++i) {
    ll a;
    cin >> a;
    count += sums[i - a];
    sums[a + i]++;
  }
  cout << count << endl;
}

Error: TLE

Fixed submission:

#include <algorithm> // count_ifに必要
#include <array>     // sizeはコンパイル時定数で固定
#include <iomanip> // cout << setprecision(15) << x で小数の出力精度を指定
#include <iostream>
#include <map>
#include <numeric> // accumulate, gcd
#include <queue>   // priority_queue も入ってる
#include <set>     // 要素数は size() で取得 multisetもある
#include <stack>
#include <string>
#include <tuple>
#include <utility> //pair
#include <vector>

using namespace std;
// 問題が起こるまでintは使用禁止2020-04-13
using ll = long long;

template <class InputIterator, class T>
bool contains(InputIterator iterator, const T &value) {
  if (find(iterator.begin(), iterator.end(), value) == iterator.end())
    return false;
  else
    return true;
}

template <class InputIterator> auto sum(InputIterator iterator) {
  return accumulate(iterator.begin(), iterator.end(), 0);
}

// endは排他的(exclusive)なので注意
vector<ll> range(ll begin, ll end) {
  vector<ll> result;
  for (ll i = begin; i < end; ++i) {
    result.push_back(i);
  }
  return result;
}

// =========ここまでテンプレ============

int main() {
  ll n;
  cin >> n;
  multiset<ll> sums;
  ll count = 0;
  for (ll i = 1; i <= n; ++i) {
    ll a;
    cin >> a;
    count += sums.count(i - a);
    sums.insert(i + a);
  }
  cout << count << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
#include <vector>

using namespace std;
using ll = long long;
using ld = long double;

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

  vector<int> v(200001, 0);
  ll cnt{};
  int a;
  for (auto i = 0; i != n; ++i) {
    cin >> a;
    if (i + a < 200001)
      ++v[i + a];

    if (i - a > 0)
      cnt += v[i - a];
  }

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


Error: TLE

Fixed submission:

#include <iostream>
#include <set>

using namespace std;
using ll = long long;
using ld = long double;

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

  multiset<int> s;
  ll cnt{};
  int a;
  for (auto i = 0; i != n; ++i) {
    cin >> a;
    s.insert(i + a);

    cnt += s.count(i - a);
  }

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

#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

// const i64 mod = 1000000007ll;
const i64 smod = 998244353ll;
const i64 inf = 1'000'000'000'000'000'000ll + 7;
// const i64 inf = 1'000'000'007;

const long double eps = 1e-8;

const i64 MAXN = 2 * 100'000 + 5;

i64 n;
i64 a[MAXN];
map<i64, i64> c;

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed);

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

  for (i64 i = 2; i <= n; i++) {
    c[i - a[i]] += 1;
  }

  i64 R = 0;
  for (i64 i = 1; i < n; i++) {
    R += c[a[i] + i];
    c[i + 1 - a[i + 1]] -= 1;
  }

  cout << R << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

// const i64 mod = 1000000007ll;
const i64 smod = 998244353ll;
const i64 inf = 1'000'000'000'000'000'000ll + 7;
// const i64 inf = 1'000'000'007;

const long double eps = 1e-8;

const i64 MAXN = 2 * 100'000 + 5;

i64 n;
i64 a[MAXN];
i64 c[MAXN];

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed);

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

  for (i64 i = 2; i <= n; i++) {
    c[i - a[i]] += 1;
  }

  i64 R = 0;
  for (i64 i = 1; i < n; i++) {
    R += c[a[i] + i];
    c[i + 1 - a[i + 1]] -= 1;
  }

  cout << R << endl;

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

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

const int maxn = 2e5 + 5;
ll a[maxn];
map<ll, ll> mp;
int main() {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    sum += mp[i - a[i]];
    mp[i + a[i]]++;
  }
  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

const int maxn = 2e5 + 5;
ll a[maxn];
ll t1[maxn], t2[maxn];
int main() {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int c1 = 0, c2 = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    t1[c1++] = a[i] + i;
    t2[c2++] = i - a[i];
  }
  ll sum = 0;
  for (int i = 0; i < c1; i++)
    for (int j = 0; j < c1; j++) {
      if (i != j && t1[i] == t2[j])
        sum++;
    }
  cout << sum << 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 < n; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
typedef long long ll;
double PI = 3.14159265;
using namespace std;
using Graph = vector<vector<int>>;
using Field = vector<string>;
using DGraph = vector<vector<pair<int, int>>>;

const long long INF = 1LL << 60;
//---//
ll a[200010], b[200010];

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

  ll x;
  rep(i, n) a[i] = 0;
  rep(i, n) b[i] = 0;

  rep(i, n) {
    cin >> x;
    if (i + x < n)
      a[i + x]++;
    if (i - x > 0)
      b[i - x]++;
  }

  ll sum = 0;
  rep(i, n) if (a[i] > 0 && b[i] > 0) sum += a[i] * b[i];

  cout << sum << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
typedef long long ll;
double PI = 3.14159265;
using namespace std;
using Graph = vector<vector<int>>;
using Field = vector<string>;
using DGraph = vector<vector<pair<int, int>>>;

const long long INF = 1LL << 60;
//---//
int a[200010] = {};

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

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

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

  cout << sum << 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 reps(i, n, s) for (int i = s; i < (int)(n); i++)
#define twopo(k) 1 << k

typedef vector<int> vec1;
typedef vector<vector<int>> vec2;
typedef vector<vector<char>> cvec2;
typedef pair<int, int> pairi;
typedef map<int, int> mapii;

vec1 vecplus(const vec1 &first, const vec1 &second) {
  int length = first.size();
  vec1 answer(length);
  rep(i, length) { answer.at(i) = first.at(i) + second.at(i); }
  return answer;
}

vec2 listmake(const int &row, const int &col) {
  vec2 list;
  rep(i, row) {
    vec1 v(col);
    rep(j, col) { cin >> v.at(j); }
    list.push_back(v);
  }
  return list;
}

int main() {
  int64_t N;
  cin >> N;
  int64_t ans = 0;
  vector<int64_t> height(N);
  map<int64_t, int64_t> counter;
  for (int64_t i = 0; i < N; i++) {
    int64_t a;
    cin >> a;
    height.at(i) = i - a;
    if (counter.count(i + a)) {
      counter[i + a]++;
    } else {
      counter[i + a] = 1;
    }
  }
  for (int64_t i = 0; i < N; i++) {
    if (counter.count(height.at(i))) {
      ans += counter.at(height.at(i));
    }
  }
  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 reps(i, n, s) for (int i = s; i < (int)(n); i++)
#define twopo(k) 1 << k

typedef vector<int> vec1;
typedef vector<vector<int>> vec2;
typedef vector<vector<char>> cvec2;
typedef pair<int, int> pairi;

vec1 vecplus(const vec1 &first, const vec1 &second) {
  int length = first.size();
  vec1 answer(length);
  rep(i, length) { answer.at(i) = first.at(i) + second.at(i); }
  return answer;
}

vec2 listmake(const int &row, const int &col) {
  vec2 list;
  rep(i, row) {
    vec1 v(col);
    rep(j, col) { cin >> v.at(j); }
    list.push_back(v);
  }
  return list;
}

int main() {
  int N;
  cin >> N;
  int ans = 0;
  vec1 height(N);
  rep(i, N) {
    int a;
    cin >> a;
    height.at(i) = i - a;
  }
  rep(i, N) {
    int sub = 2 * i - height.at(i);
    for (int x : height) {
      if (x == sub) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define pan(i, n) for (int i = 0; i < n; i++)
#define pans(i, n, a) for (int i = a; i < n; i++)
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define INF 1000000007
#define PI 3.141592653
#define inf 100007
#define P pair<ll, ll>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int main() {
  int n;
  cin >> n;
  vl v(n);
  map<int, int> ii;
  ll ans = 0;
  pan(i, n) cin >> v[i];
  pan(i, n) ii[i + v[i]]++;
  pan(j, n) ans += ii[j - v[j]];
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define pan(i, n) for (int i = 0; i < n; i++)
#define pans(i, n, a) for (int i = a; i < n; i++)
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define INF 1000000007
#define PI 3.141592653
#define inf 100007
#define P pair<ll, ll>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int main() {
  int n;
  cin >> n;
  vl v(n);
  ll ans = 0;
  pan(i, n) cin >> v[i];
  ll mx = *max_element(v.begin(), v.end()),
     mn = *min_element(v.begin(), v.end());
  pan(i, n) pans(j, n, i + 1) if ((j - i) == (v[j] + v[i])) ans++;
  cout << ans;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;

// ----------- VECTOR ------------
template <typename T> istream &operator>>(istream &is, vector<T> &vs) {
  for (T &v : vs)
    is >> v;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vs) {
  for (const T &v : vs)
    os << v << " ";
  return os;
}
// ----------- VECTOR ------------

int main() {
  int N;
  cin >> N;
  vector<LL> H(N);
  cin >> H;

  LL cnt = 0;
  map<int, int> L;
  for (int i = 0; i < N; ++i) {
    cnt += L[i - H[i]];
    L[i + H[i]]++;
  }
  cout << cnt << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;

// ----------- VECTOR ------------
template <typename T> istream &operator>>(istream &is, vector<T> &vs) {
  for (T &v : vs)
    is >> v;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vs) {
  for (const T &v : vs)
    os << v << " ";
  return os;
}
// ----------- VECTOR ------------

int main() {
  int N;
  cin >> N;
  vector<int> H(N);
  cin >> H;
  int cnt = 0;
  int kmax = 0;
  for (int x : H)
    kmax = max(x, kmax);

  for (int i = 0; i < N; ++i) {
    for (int j = H[i] + i; j < N; ++j) {
      if ((j - i) > (H[i] + kmax))
        break;
      if ((j - i) == (H[i] + H[j])) {
        //        cout << i << " " << j << endl;
        ++cnt;
      }
    }
  }
  cout << cnt << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>

using namespace std;

#define ll long long
#define rep(i, n) for (i = 0; i < n; i++)

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  unordered_map<int, int> mp;
  ll ans{};
  for (int i = 0; i < n; i++) {
    mp[i - a[i]]++;
  }
  for (int i = 0; i < n; i++) {
    ans += mp[a[i] + i];
  }
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>

using namespace std;

#define ll long long
#define rep(i, n) for (i = 0; i < n; i++)

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  unordered_map<int, int> mp;
  ll ans{};
  for (int i = 0; i < n; i--) {
    mp[i - a[i]]++;
  }
  for (int i = 0; i < n; i--) {
    ans += mp[a[i] + i];
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> P;
#define INF 10000000000000
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i < (ll)(n); i++)
#define repN_1(i, n) for (ll i = 0; i < (ll)(n - 1); i++)
#define repM(i, n) for (ll i = (ll)(n - 1); i >= 0; i--)
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;
}

int main() {
  cout << fixed << setprecision(18);

  ll n;
  cin >> n;
  vector<ll> a(n), p;
  multiset<ll> m;
  map<ll, ll> mp;
  ll ans = 0;

  rep(i, n) {
    cin >> a[i];
    mp[a[i] + i + 1]++;
  }

  rep(i, n) ans += mp[i + 1 - a[i]];

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> P;
#define INF 10000000000000
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i < (ll)(n); i++)
#define repN_1(i, n) for (ll i = 0; i < (ll)(n - 1); i++)
#define repM(i, n) for (ll i = (ll)(n - 1); i >= 0; i--)
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;
}

int main() {
  cout << fixed << setprecision(18);

  ll n;
  cin >> n;
  vector<ll> a(n), p;
  multiset<ll> m;
  ll ans = 0;

  rep(i, n) {
    cin >> a[i];
    p.push_back(a[i] + i + 1);
    m.insert(i + 1 - a[i]);
  }

  rep(i, n) ans += m.count(p[i]);

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define REP(i, m, n) for (int(i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
const int MOD = 1e9 + 7;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<ll> a(n + 1), b(n + 1);
  ll sum = 0;
  REP(i, 1, n + 1) {
    ll x;
    cin >> x;
    a[i] = x + i;
    b[i] = i - x;
    if (a[i] == b[i])
      sum--;
  };
  sort(b.begin(), b.end());

  REP(i, 1, n + 1) {
    int t = upper_bound(b.begin(), b.end(), a[i]) -
            lower_bound(b.begin(), b.end(), a[i]);
    sum += t;
  }

  cout << sum << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, m, n) for (int(i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
const int MOD = 1e9 + 7;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

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

  cout << sum << 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 < (n); ++i)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const ll INF = 1LL << 60;

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

  map<ll, ll> mp;
  rep(j, N) { mp[j + 1 - A[j]]++; }

  // for (auto iter = mp.begin(); iter != mp.end(); iter++) {
  //     cout << iter->first << " " << iter->second << endl;
  // }

  ll ans = 0;
  rep(i, N) {
    ll target = (i + 1) + A[i];
    ans += mp[target];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

const ll INF = 1LL << 60;

vector<ll> A;
int main() {
  long long N;
  scanf("%lld", &N);

  A.resize(N);
  rep(i, N) cin >> A[i];
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      int idx_diff = j - i;

      if (A[i] + A[j] == idx_diff)
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include "math.h"

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i >= (b); --i)
#define SIZE(a) int((a).size())
#define out(a) cout << (a) << endl;
const int INF = INT_MAX;
const int MAX = 510000;
const ll MOD = 1000000007;

int main() {
  ll n, ans = 0;
  cin >> n;
  vll a(n), b(n);
  rep(i, 0, n) {
    ll x;
    cin >> x;
    a[i] = i - x;
    b[i] = i + x;
  }
  sort(b.begin(), b.end());
  rep(i, 0, n) {
    auto it = lower_bound(b.begin(), b.end(), a[i]);
    auto it2 = upper_bound(b.begin(), b.end(), a[i]);
    ans += it2 - it;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include "math.h"

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef vector<pair<ll, ll>> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i > (b); --i)
const int MOD = 1000000007;
const int MAX = 510000;

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

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

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<long long> COUNT(N * 2, 0);
  long long ANS = 0;
  for (int i = 0; i < N; i++) {
    if (i - A[i] >= 0)
      ANS += COUNT[i - A[i]];
    if (i + A[i] < N * 2)
      COUNT[i + A[i]]++;
  }
  cout << ANS << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  multiset<long long> ST;
  long long ANS = 0;
  for (int i = 0; i < N; i++) {
    ANS += ST.count(i - A[i]);
    ST.insert(i + A[i]);
  }
  cout << ANS << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <map>

using namespace std;

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

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


Error: TLE

Fixed submission:

#include <cstdio>
#include <set>

using namespace std;

int n;
int a[200005];
long long ans;
multiset<int> m;

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    m.insert(i - a[i]);
  }
  for (int i = 0; i < n; i++) {
    m.erase(m.find(i - a[i]));
    ans += m.count(i + a[i]);
  }
  printf("%lld\\n", ans);
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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


Error: WA

Fixed submission:

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