Problem p02588 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> vector<T> make_vec(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto make_vec(size_t n, Args... args) {
  return vector<decltype(make_vec<T>(args...))>(n, make_vec<T>(args...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }

template <typename T> struct twodim_fenwick_tree {
  int h, w;
  vector<vector<T>> data;
  twodim_fenwick_tree(int h, int w)
      : h(h), w(w), data(h + 1, vector<T>(w + 1)) {}
  void add(int r, int c, T x = 1) {
    for (int i = r + 1; i <= h; i += i & -i)
      for (int j = c + 1; j <= w; j += j & -j)
        data[i][j] += x;
  }
  T sum(int r, int c) {
    T res = 0;
    for (int i = r; i; i -= i & -i)
      for (int j = c; j; j -= j & -j)
        res += data[i][j];
    return res;
  }
  T sum(int r1, int c1, int r2, int c2) {
    return sum(r2, c2) - sum(r1, c2) - sum(r2, c1) + sum(r1, c1);
  }
};

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

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

  auto f = [](string s) {
    ll ten = 0;
    ll x = 0;
    rep(i, s.size()) {
      if (s[i] == '.')
        ten = 1;
      else {
        if (ten > 0)
          ten *= 10;
        x *= 10;
        x += s[i] - '0';
      }
    }
    if (ten == 0)
      return make_pair(x, 1LL);
    ll g = gcd(x, ten);
    return make_pair(x / g, ten / g);
  };

  vector<pair<ll, ll>> a(n);
  rep(i, n) a[i] = f(s[i]);

  auto g = [](pair<ll, ll> p) {
    int two = 0, five = 0;
    while (p.first % 2 == 0) {
      two++;
      p.first /= 2;
    }
    while (p.first % 5 == 0) {
      five++;
      p.first /= 5;
    }
    while (p.second % 2 == 0) {
      two--;
      p.second /= 2;
    }
    while (p.second % 5 == 0) {
      five--;
      p.second /= 5;
    }
    return make_pair(two, five);
  };

  rep(i, n) a[i] = g(a[i]);

  twodim_fenwick_tree<int> ft(1000, 1000);
  rep(i, n) ft.add(a[i].first + 100, a[i].second + 100);

  ll ans = 0;
  rep(i, n) {
    int x = -a[i].first + 100;
    int y = -a[i].second + 100;
    ans += ft.sum(x, y, 1000, 1000);
    if (a[i].first + 100 >= x && a[i].second + 100 >= y)
      ans--;
  }
  ans /= 2;
  cout << ans << '\\n';
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> vector<T> make_vec(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto make_vec(size_t n, Args... args) {
  return vector<decltype(make_vec<T>(args...))>(n, make_vec<T>(args...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }

template <typename T> struct twodim_fenwick_tree {
  int h, w;
  vector<vector<T>> data;
  twodim_fenwick_tree(int h, int w)
      : h(h), w(w), data(h + 1, vector<T>(w + 1)) {}
  void add(int r, int c, T x = 1) {
    for (int i = r + 1; i <= h; i += i & -i)
      for (int j = c + 1; j <= w; j += j & -j)
        data[i][j] += x;
  }
  T sum(int r, int c) {
    T res = 0;
    for (int i = r; i; i -= i & -i)
      for (int j = c; j; j -= j & -j)
        res += data[i][j];
    return res;
  }
  T sum(int r1, int c1, int r2, int c2) {
    return sum(r2, c2) - sum(r1, c2) - sum(r2, c1) + sum(r1, c1);
  }
};

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

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

  auto f = [](string s) {
    ll ten = 0;
    ll x = 0;
    rep(i, s.size()) {
      if (s[i] == '.')
        ten = 1;
      else {
        if (ten > 0)
          ten *= 10;
        x *= 10;
        x += s[i] - '0';
      }
    }
    if (ten == 0)
      return make_pair(x, 1LL);
    ll g = gcd(x, ten);
    return make_pair(x / g, ten / g);
  };

  vector<pair<ll, ll>> a(n);
  rep(i, n) a[i] = f(s[i]);

  auto g = [](pair<ll, ll> p) {
    int two = 0, five = 0;
    while (p.first % 2 == 0) {
      two++;
      p.first /= 2;
    }
    while (p.first % 5 == 0) {
      five++;
      p.first /= 5;
    }
    while (p.second % 2 == 0) {
      two--;
      p.second /= 2;
    }
    while (p.second % 5 == 0) {
      five--;
      p.second /= 5;
    }
    return make_pair(two, five);
  };

  rep(i, n) a[i] = g(a[i]);

  twodim_fenwick_tree<int> ft(100, 100);
  rep(i, n) ft.add(a[i].first + 10, a[i].second + 10);

  ll ans = 0;
  rep(i, n) {
    int x = -a[i].first + 10;
    int y = -a[i].second + 10;
    ans += ft.sum(x, y, 100, 100);
    if (a[i].first + 10 >= x && a[i].second + 10 >= y)
      ans--;
  }
  ans /= 2;
  cout << ans << '\\n';
}