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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define repk(i, k, n) for (ll i = k; i <= (ll)(n); ++i)
#define MAX 500010
#define MOD 1000000007
typedef long long ll;

using namespace std;

ll ans;

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

  ll N;
  cin >> N;

  vector<ll> A(N);
  map<ll, vector<ll>> R, L;
  rep(i, N) {
    cin >> A[i];
    R[-A[i] + i + 1].push_back(i);
    L[A[i] + i + 1].push_back(i);
  }

  for (auto pl : L) {
    if (R.count(pl.first)) {
      ans += pl.second.size() * R[pl.first].size();
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define repk(i, k, n) for (ll i = k; i <= (ll)(n); ++i)
#define MAX 500010
#define MOD 1000000007
typedef long long ll;

using namespace std;

ll ans;

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

  ll N;
  cin >> N;

  vector<ll> A(N);
  map<ll, vector<ll>> R, L;
  rep(i, N) {
    cin >> A[i];
    R[-A[i] + i].push_back(i);
    L[A[i] + i].push_back(i);
  }

  for (auto pl : L) {
    rep(i, pl.second.size()) {
      ll search_index = pl.second[i];
      if (R.count(pl.first)) {
        rep(j, R[pl.first].size()) {
          if (search_index < R[pl.first][j]) {
            // cout << search_index << " "  << R[pl.first][j] <<endl;
            ans++;
          }
        }
      }
    }
  }

  cout << ans << endl;

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


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

using namespace std;

typedef long long ll;

ll N;

set<ll> check;
multiset<ll> check_i;
multiset<ll> check_j;

int main() {
  ll i, j;

  cin >> N;

  for (i = 0; i < N; i++) {
    ll a;
    cin >> a;
    check.insert(i + a);
    check_i.insert(i + a);
    check_j.insert(i - a);
  }

  ll n = 0;
  for (auto itr : check) {
    n += check_i.count(itr) * check_j.count(itr);
  }
  cout << n << endl;
}

Error: TLE

Fixed submission:


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

using namespace std;

typedef long long ll;

ll N;

vector<ll> check;
vector<ll> check_set;

int main() {
  ll i, j;

  cin >> N;

  for (i = 0; i < N; i++) {
    ll a;
    cin >> a;
    check.push_back(i + a);
    check_set.push_back(i - a);

    // cout << check[i] << " " << check_set[i] << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)                                                              \\
  ;                                                                            \\
  for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
int main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, n) cin >> a[i];
  ll ans = 0;
  map<int, int> data;
  rep(i, n) {
    int sa = i - a[i];
    ans += data[sa];
    int wa = i + a[i];
    data[wa]++;
  }
  cout << ans << endl;
  return 0;
}

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(x) (x).begin(), (x).end()
typedef long long ll;
int main() {
  int n;
  cin >> n;
  int 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[i] + a[j] == abs(i - j))
        ans++;
    }
    for (int j = i - a[i]; j >= 0; j--) {
      if (a[i] + a[j] == abs(i - j))
        ans++;
    }
  }
  cout << ans / 2 << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
 * author: calgagi
 *         Calvin Gagliano
 */
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define ii pair<int, int>
#define ar array
#define f first
#define s second
#define dd long double

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  srand(chrono::steady_clock::now().time_since_epoch().count());
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

  int n;
  cin >> n;
  vector<ll> A(n);
  for (auto &i : A) {
    cin >> i;
  }

  map<ll, ll> past;
  ll ans = 0;
  for (int j = 0; j < n; j++) {
    ll search = (j + 1) - A[j], x = A[j] + (j + 1);
    ans += past[search];
    past[x]++;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

/*
 * author: calgagi
 *         Calvin Gagliano
 */
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define ii pair<int, int>
#define ar array
#define f first
#define s second
#define dd long double

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  srand(chrono::steady_clock::now().time_since_epoch().count());
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &i : a) {
    cin >> i;
  }

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

  cout << ans << endl;

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

#include <cmath>
#include <iostream>
#include <map>
using namespace std;
// typedef pair<int, int> P;
typedef long long ll;

long long *L = new long long[200001];
long long *R = new long long[200001];

int main() {
  int N;
  cin >> N;
  map<long long, long long> mp;
  for (int i = 1; i < N + 1; i++) {
    long long a;
    cin >> a;
    L[i] = i + a;
    R[i] = i - a;
  }
  ll cnt = 0;
  for (int i = 1; i < N + 1; i++) {
    // auto itr = mp.find(L[i]);
    // if( itr != mp.end() ) {
    //     mp[L[i]] = itr->second + 1;
    // }else{
    //     mp[L[i]] = 1;
    // }
    mp[L[i]]++;
    // itr = mp.find(R[i]);
    // if( itr != mp.end() ) {
    //     cnt += itr->second;
    // }
    cnt += mp[R[i]];
  }
  // for(int i=1;i<N+1;i++){
  //     auto itr = mp.find(R[i]);
  //     if( itr != mp.end() ) {
  //         cnt += itr->second;
  //     }
  // }

  cout << cnt << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <list>
#include <vector>
using namespace std;
// typedef pair<int, int> P;

long long *H = new long long[200001];

int main() {
  int N;
  cin >> N;
  long long max = 0;
  long long min = 1000000000;
  // vector<pair<int, int> > pairs(N);
  // list<P> list;
  for (int i = 1; i < N + 1; i++) {
    cin >> H[i];
    if (max < H[i]) {
      max = H[i];
    }
    if (min > H[i]) {
      min = H[i];
    }
    // list.push_front(P(i,H))
  }
  int cnt = 0;
  for (int i = 1; i < N + 1; i++) {

    for (int j = i + H[i] + 1; j < N + 1; j++) {
      if ((int)abs(i - j) == H[i] + H[j]) {
        cnt++;
      }
    }
  }
  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;
#define rep(i, start, n) for (int i = (int)(start); i < (int)(n); ++i)
int N, M;
void func() {
  cin >> N;
  ll ans(0), A[N];
  map<ll, int> B;
  rep(i, 1, N + 1) {
    cin >> A[i];
    ll k = i - A[i];
    if (B.count(k))
      ++B[k];
    else
      B[k] = 1;
  }
  rep(i, 1, N + 1) {
    ll k = i + A[i];
    if (B.count(k))
      ans += B.find(k)->second;
  }
  cout << ans << endl;
}
int main() { func(); }


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, start, n) for (int i = (int)(start); i < (int)(n); ++i)
int N, M;
void func() {
  cin >> N;
  ll ans(0);
  map<int, int> A, B;
  int tmp;
  rep(i, 1, N + 1) {
    cin >> tmp;
    A.emplace(i, i + tmp);
    B.emplace(i, i - tmp);
  }
  rep(i, 1, N) {
    rep(j, i + 1, N + 1) {
      if (A[i] == B[j]) {
        ++ans;
      }
    }
  }
  cout << ans << endl;
}
int main() { func(); }
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main(void) {
  long long int n;
  cin >> n;
  vector<long long int> a(n);
  map<long long int, long long int> left;
  map<long long int, long long int> right;
  set<long long int> all;
  for (long long int i = 0; i < n; i++) {
    cin >> a.at(i);
    left[i + 1 + a.at(i)]++;
    right[i + 1 - a.at(i)]++;
    all.insert(i + 1 + a.at(i));
    all.insert(i + 1 - a.at(i));
  }

  long long int ans = 0;
  for (long long int i : all) {
    ans += left[i] * right[i];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main(void) {
  long long int n;
  cin >> n;
  vector<pair<long long int, long long int>> a;
  for (long long int i = 0; i < n; i++) {
    long long int tall;
    cin >> tall;
    if (tall <= n - 1) {
      a.push_back(make_pair(tall, i + 1));
    }
  }

  long long int size = a.size();
  long long int ans = 0;
  for (long long int i = 0; i < size; i++) {
    for (long long int j = i + 1; j < size; j++) {
      if (a.at(i).first + a.at(j).first == a.at(j).second - a.at(i).second) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

//    int n=132;
//    bitset<16>binar(n);
//    int a = bitset<16>(binar).to_ulong();
//    cout<<a;

#define int long long
#define pb push_back
#define rep(i, b) for (int i = 0; i < b; i++)
#define all(c) (c).begin(), (c).end()

using namespace std;

signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  map<int, int> m;
  int val = 0;
  rep(i, n) {
    val = val + m[i - a[i]];
    m[i + a[i]]++;
  }
  cout << val;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

//    int n=132;
//    bitset<16>binar(n);
//    int a = bitset<16>(binar).to_ulong();
//    cout<<a;

#define int long long
#define pb push_back
#define rep(i, b) for (int i = 0; i < b; i++)
#define all(c) (c).begin(), (c).end()

using namespace std;

signed main() {
  int n;
  cin >> n;
  vector<int> a;
  rep(i, n) { cin >> a[i]; }
  map<int, int> m;
  int val = 0;
  rep(i, n) {
    val = val + m[i - a[i]];
    m[i + a[i]]++;
  }
  cout << val;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <typeinfo>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
map<ll, ll> m;

int main() {
  ll n, res = 0;

  cin >> n;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    if (m.find(i + 1 - a[i]) != m.end()) {
      res += m[i + 1 - a[i]];
    }
    if (m.find(i + 1 + a[i]) == m.end()) {
      m.insert(make_pair(i + 1 + a[i], 1));
    } else {
      m[i + 1 + a[i]]++;
    }

    // for (int j = 0; j < i - a[i]; j++)
    // {
    //     if (i - j == a[i] + a[j])
    //     {
    //         res++;
    //     }
    // }
  }
  cout << res << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <typeinfo>
#include <vector>
#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() {
  ll n, res = 0;

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

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

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

Error: TLE

Fixed submission:

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

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

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

int main() {
  long long n;
  cin >> n;
  long long count = 0;
  vector<long long> K(n + 1);
  vector<long long> L(n + 1);
  for (long long i = 1; i <= n; i++) {
    long long a = 0;
    cin >> a;
    if (a + i <= n) {
      K.at(a + i)++;
    }
    if (0 <= i - a) {
      L.at(i - a)++;
    }
  }
  for (long long i = 0; i <= n; i++) {
    count += K.at(i) * L.at(i);
  }

  cout << count << endl;
}

Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define YN(x) cout << (bool x ? "Yes" : "No") << endl;
#define out(s) cout << s << endl;
#define pb push_back;
#define sp " ";
void vout(vector<int> v) {
  for (int i = 0; i < v.size(); i++)
    cout << v.at(i) << endl;
}
int main() {
  map<lli, lli> plus, mins;
  int n;
  cin >> n;
  lli ans = 0;
  rep(i, n) {
    int ai;
    cin >> ai;
    (plus.count(ai + i + 1) ? plus.at(ai + i + 1) += 1 : plus[ai + i + 1] = 1);
    (mins.count(i + 1 - ai) ? mins.at(i + 1 - ai) += 1 : mins[i + 1 - ai] = 1);
  }
  for (auto v : mins) {
    if (v.first < 0)
      continue;
    if (plus.count(v.first)) {
      ans += v.second * plus.at(v.first);
    }
  }
  out(ans);
}

Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, s = 0;
  vector<int> a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    a.push_back(k);
  }
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      (j - i == a.at(j) + a.at(i) ? s += 1 : s += 0);
    }
  }
  cout << s;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)

// #define MAX 1000200

int main() {
  int n;
  cin >> n;
  vector<int> a;
  rep(i, n) {
    int height;
    cin >> height;
    a.push_back(height);
  }
  long cnt = 0;
  map<long, long> l, r;
  // int minim=INT_MAX, maxim=0;
  //  rep(i, MAX+1){
  //      l[i] = 0;
  //      r[i] = 0;
  //  }
  // cout << "OK" << endl;
  rep(i, n) {
    l[a[i] + i + 1]++;
    if (i + 1 - a[i] >= 0) {
      r[i + 1 - a[i]]++;
    }
    // cout << l[a[i]+i+1] << endl;
  }
  map<long, long>::iterator L;
  for (L = l.begin(); L != l.end(); L++) {
    long item = (*L).first;
    // cout << item << " : " << l[item] <<
    //  if(r.find(item)!=r.end()||item==(*r.end()).first){
    //      cnt +=  l[item] * r[item];
    //  }
    cnt += l[item] * r[item];
    // cout << cnt << endl;
  }
  cout << cnt << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)

int main() {
  int n;
  cin >> n;
  vector<int> a;
  rep(i, n) {
    int height;
    cin >> height;
    a.push_back(height);
  }
  int cnt = 0;
  rep(i, n) {
    int h = a[i], dis = h + 1;
    rep2(j, i + dis, n) {
      if (h + a[j] == dis) {
        cnt++;
      }
      dis++;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define f(i, a, n) for (int i = a; i < n; i++)
#define inf 1e17
#define MAX_SIZE 1000005

void pr(int arr[], int size) {
  for (ll i = 0; i < size; i++) {
    printf("%d ", arr[i]);
  }
  cout << endl;
}

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

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

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define f(i, a, n) for (int i = a; i < n; i++)
#define inf 1e17
#define MAX_SIZE 1000005

void pr(int arr[], int size) {
  for (ll i = 0; i < size; i++) {
    printf("%d ", arr[i]);
  }
  cout << endl;
}

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

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

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

#include <cstdio>
#include <iostream>
const long long Maxn = 1000000 + 10;
long long a[Maxn], cnt[Maxn][2];
long long n, ans;
inline long long read() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
  return s * w;
}
int main() {
  n = read();
  for (long long i = 1; i <= n; ++i) {
    a[i] = read();
    if (i - a[i] >= 0)
      ++cnt[i - a[i]][1];
    if (i + a[i] <= n)
      ++cnt[i + a[i]][0];
  }
  for (long long i = 1; i <= n; ++i) {
    if (i - a[i] >= 0)
      ans += cnt[i - a[i]][0];
    if (i + a[i] <= n)
      ans += cnt[i + a[i]][1];
  }
  printf("%lld\\n", ans >> 1);
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
const int Maxn = 1000000 + 10;
int a[Maxn], cnt[Maxn][2];
int n, ans;
inline int read() {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
  return s * w;
}
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    ++cnt[i - a[i]][1], cnt[i + a[i]][0]++;
  }
  for (int i = 1; i <= n; ++i)
    ans += cnt[i - a[i]][0] + cnt[i + a[i]][1];
  printf("%d\\n", ans >> 1);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
vector<ll> V;
int pre[maxn], a[maxn], b[maxn];
map<ll, ll> M;
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> pre[i];
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += M[i - pre[i]];
    M[pre[i] + i]++;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

#include <bits/stdc++.h>
typedef long long int LL;
typedef unsigned long long int ULL;
using namespace std;

// 插入此處

int a[200005];

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long int LL;
typedef unsigned long long int ULL;
using namespace std;

// 插入此處

int a[100005];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  LL ans = 0;
  map<int, int> m;
  m[-a[0] - 0] = 1;
  for (int i = 1; i < n; i++) {
    ans += m[a[i] - i];
    m[-a[i] - i]++;
  }
  printf("%lld\\n", ans);
}
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 all(x) (x).begin(), (x).end()

using ll = long long;
using P = pair<int, int>;

const int MOD = 1e9 + 7;
const int INF = 1001001001;

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

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

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

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

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

  return 0;
}


Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(), (x).end()

using ll = long long;
using P = pair<int, int>;

const int MOD = 1e9 + 7;
const int INF = 1001001001;

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

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

  int ans = 0;
  rep(i, n) {
    for (int j = a[i] + i; j < n; ++j) {
      if (abs(i - j) == a[i] + a[j]) {
        ans++;
      }
    }
  }

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

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

#include <iostream>
#include <map>

using namespace std;
int main() {
  int N;
  int A[200001];
  cin >> N;
  map<int, int> dif;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    if (dif.find(A[i] + i) == dif.end())
      dif[A[i] + i] = 0;
    dif[A[i] + i]++;
  }
  long count = 0;
  for (int j = 1; j <= N; j++) {
    if (dif.find(j - A[j]) != dif.end())
      count += dif[j - A[j]];
  }

  cout << count << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <set>

using namespace std;
int main() {
  int N;
  int A[200001];
  cin >> N;
  multiset<int> dif;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    dif.insert(A[i] + i);
  }
  long count = 0;
  for (int j = 1; j <= N; j++) {
    count += dif.count(j - A[j]);
  }

  cout << count << endl;
  return 0;
}