Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

/*ITS FUCKING O(N) now
GET ACCEPTED FOR FUCK'S SAKE*/
#define _USE_MATH_DEFINES

#include <cmath>

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

long long powerexp(long long base, long long exp, int mod) {
  long long res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base) % mod;
    exp = exp >> 1;
    base = (base * base) % mod;
  }
  return res;
}
int gcd(int a, int b) {
  int c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}
void PrintVector(vector<long long int> v, long long int n) {

  for (int i = v.size() - 1; i >= 0; i--) {
    cout << v[i] << " ";
  }
  cout << endl;
}
void Takevector(vector<long long int> &v, long long int n) {
  for (long long int i = 0; i < n; i++) {
    long long int number;
    cin >> number;
    v.push_back(number);
  }
}
typedef long long int ll;
typedef vector<long long int> vi;

#define sci(x)                                                                 \\
  int x;                                                                       \\
  scanf("%d", &x);
#define scii(x, y)                                                             \\
  int x, y;                                                                    \\
  scanf("%d %d", &x, &y);
#define sciii(x, y, z)                                                         \\
  int x, y, z;                                                                 \\
  scanf("%d %d %d", &x, &y, &z);
#define scl(x)                                                                 \\
  ll x;                                                                        \\
  scanf("%lld", &x);
#define scll(x, y)                                                             \\
  ll x, y;                                                                     \\
  scanf("%lld %lld", &x, &y);
#define sclll(x, y, z)                                                         \\
  ll x, y, z;                                                                  \\
  scanf("%lld %lld %lld", &x, &y, &z);
#define TC(x)                                                                  \\
  sci(x);                                                                      \\
  while (x--)
#define rep(i, x, y) for (__typeof(x) i = x; i <= y; i++)
#define repi(i, x, y) for (__typeof(x) i = x; i >= y; i--)
#define fore(itr, x)                                                           \\
  for (__typeof(x.begin()) itr = x.begin(); itr != x.end(); itr++)
#define forei(itr, x)                                                          \\
  for (__typeof(x.end()) itr = x.end() - 1; itr != x.begin() - 1; itr--)
#define nl cout << "\\n";
int min(int x, int y) { return (x < y) ? x : y; }

int main() {

  ll ans = 0;

  map<ll, ll> mp1, mp2;

  sci(n);
  vi v(n);
  ll a;
  int cnt = 1;
  rep(i, 0, n - 1) {
    cin >> v[i];
    mp1[i + v[i]]++;
    mp2[i - v[i]]++;
  }
  rep(i, 0, n - 1) {
    ans += mp1[i - v[i]];
    ans += mp2[i + v[i]];
  }

  cout << ans / 2;

  return 0;
}


Error: TLE

Fixed submission:

/*ITS FUCKING O(N) now
GET ACCEPTED FOR FUCK'S SAKE*/
#define _USE_MATH_DEFINES

#include <cmath>

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

long long powerexp(long long base, long long exp, int mod) {
  long long res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base) % mod;
    exp = exp >> 1;
    base = (base * base) % mod;
  }
  return res;
}
int gcd(int a, int b) {
  int c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}
void PrintVector(vector<long long int> v, long long int n) {

  for (int i = v.size() - 1; i >= 0; i--) {
    cout << v[i] << " ";
  }
  cout << endl;
}
void Takevector(vector<long long int> &v, long long int n) {
  for (long long int i = 0; i < n; i++) {
    long long int number;
    cin >> number;
    v.push_back(number);
  }
}
typedef long long int ll;
typedef vector<long long int> vi;

#define sci(x)                                                                 \\
  int x;                                                                       \\
  scanf("%d", &x);
#define scii(x, y)                                                             \\
  int x, y;                                                                    \\
  scanf("%d %d", &x, &y);
#define sciii(x, y, z)                                                         \\
  int x, y, z;                                                                 \\
  scanf("%d %d %d", &x, &y, &z);
#define scl(x)                                                                 \\
  ll x;                                                                        \\
  scanf("%lld", &x);
#define scll(x, y)                                                             \\
  ll x, y;                                                                     \\
  scanf("%lld %lld", &x, &y);
#define sclll(x, y, z)                                                         \\
  ll x, y, z;                                                                  \\
  scanf("%lld %lld %lld", &x, &y, &z);
#define TC(x)                                                                  \\
  sci(x);                                                                      \\
  while (x--)
#define rep(i, x, y) for (__typeof(x) i = x; i <= y; i++)
#define repi(i, x, y) for (__typeof(x) i = x; i >= y; i--)
#define fore(itr, x)                                                           \\
  for (__typeof(x.begin()) itr = x.begin(); itr != x.end(); itr++)
#define forei(itr, x)                                                          \\
  for (__typeof(x.end()) itr = x.end() - 1; itr != x.begin() - 1; itr--)
#define nl cout << "\\n";
int min(int x, int y) { return (x < y) ? x : y; }

int main() {

  ll ans = 0;
  vi v;
  multiset<ll> st1;
  multiset<ll> st2;
  sci(n);
  ll a;
  int cnt = 1;
  rep(i, 0, n - 1) {
    cin >> a;
    st1.insert(i + a);
    st2.insert(i - a);
  }
  for (auto it = st1.begin(); it != st1.end(); it++) {
    if (st2.count(*it) > 0) {
      ans += st2.count(*it);
    }
  }
  cout << ans;

  return 0;
}