Problem p03770 (C++)

Buggy submission (status: Time Limit Exceeded):
/// d.cpp

#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
const pair<int, int> DD[] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                             {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
#define fi cin
#define fo cout
#define x first
#define y second
#define ll long long
#define IOS                                                                    \
  ios_base ::sync_with_stdio(0);                                               \
  cin.tie(0)
#define p(v) cerr << #v << " = " << v << '\n'
#define p2(v) cerr << #v << " = " << (complex<__typeof(v.x)>(v.x, v.y)) << '\n'
#define vi vector<int>
#define vl vector<ll>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp make_pair
#define db long double
#define fail puts("-1")
#define yes puts("YES")
#define no puts("NO")
#define PP puts("Possible")
#define II puts("Impossible")
#define vii vector<pii>
#define vll vector<ll>
#define pb push_back
#define pdd pair<db, db>
template <class T> T smin(T &a, T b) {
  if (a > b)
    a = b;
  return a;
}
template <class T> T smax(T &a, T b) {
  if (a < b)
    a = b;
  return a;
}
int main(void) {
#ifdef CF
  freopen("input", "r", stdin);
#endif // CF
  srand(time(0));
  fo << fixed << setprecision(7);
  cerr << fixed << setprecision(7);
  const int mod = 1e9 + 7;
  int n, X, Y;
  fi >> n >> X >> Y;
  static vi C[1 << 20];
  static int fact[1 << 20];
  static int inv[1 << 20];
  fact[0] = fact[1] = 1;
  int ans = 1;
  for (int i = 1; i <= n + 5; ++i)
    fact[i] = (1ll * fact[i - 1] * i) % mod;
  auto pow = [&](int a, int b, int mod) {
    int ans = 1;
    while (b) {
      if (b & 1)
        ans = (1ll * ans * a) % mod;
      a = (1ll * a * a) % mod;
      b /= 2;
    }
    return ans;
  };
  static int f[1 << 20];
  for (int i = 1; i <= n; ++i)
    f[i] = i;
  function<int(int)> get = [&](int k) {
    return f[k] == k ? k : f[k] = get(f[k]);
  };
  for (int i = 0; i <= n + 5; ++i)
    inv[i] = pow(fact[i], mod - 2, mod);
  static pii s[1 << 20];
  multiset<pii> ss;
  for (int i = 1; i <= n; ++i) {
    fi >> s[i].x >> s[i].y;
    C[s[i].x].pb(i);
    ss.insert(mp(s[i].y, i));
  }
  vi go;
  for (auto it : ss)
    go.pb(it.y);
  int SZ = 0;
  for (auto node : go) {
    const int color = s[node].x;
    vii cur;
    for (auto it : C[color]) {
      if (s[it].y + s[node].y <= X)
        f[get(node)] = get(it);
      else if (ss.count(mp(s[it].y, it)))
        cur.pb(mp(s[it].y, it));
      ss.erase(mp(s[it].y, it));
    }
    while (!ss.empty() && ss.begin()->x + s[node].y <= Y)
      f[get(ss.begin()->y)] = get(node), ss.erase(ss.begin());
    for (auto it : cur)
      ss.insert(it);
  }
  static vi Sets[1 << 20];
  for (int i = 1; i <= n; ++i)
    Sets[get(i)].pb(s[i].x);
  for (int i = 1; i <= n; ++i) {
    static map<int, int> M;
    M.clear();
    ans = (1ll * ans * fact[Sets[i].size()]) % mod;
    for (auto it : Sets[i])
      ++M[it];
    for (auto it : M)
      ans = (1ll * ans * inv[it.y]) % mod;
  }
  fo << ans << '\n';
  cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\n';
  return 0;
}


Fixed submission:
/// d.cpp

#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
const pair<int, int> DD[] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                             {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
#define fi cin
#define fo cout
#define x first
#define y second
#define ll long long
#define IOS                                                                    \
  ios_base ::sync_with_stdio(0);                                               \
  cin.tie(0)
#define p(v) cerr << #v << " = " << v << '\n'
#define p2(v) cerr << #v << " = " << (complex<__typeof(v.x)>(v.x, v.y)) << '\n'
#define vi vector<int>
#define vl vector<ll>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp make_pair
#define db long double
#define fail puts("-1")
#define yes puts("YES")
#define no puts("NO")
#define PP puts("Possible")
#define II puts("Impossible")
#define vii vector<pii>
#define vll vector<ll>
#define pb push_back
#define pdd pair<db, db>
template <class T> T smin(T &a, T b) {
  if (a > b)
    a = b;
  return a;
}
template <class T> T smax(T &a, T b) {
  if (a < b)
    a = b;
  return a;
}
int main(void) {
#ifdef CF
  freopen("input", "r", stdin);
#endif // CF
  srand(time(0));
  fo << fixed << setprecision(7);
  cerr << fixed << setprecision(7);
  const int mod = 1e9 + 7;
  int n, X, Y;
  fi >> n >> X >> Y;
  static vi C[1 << 20];
  static int fact[1 << 20];
  static int inv[1 << 20];
  fact[0] = fact[1] = 1;
  int ans = 1;
  for (int i = 1; i <= n + 5; ++i)
    fact[i] = (1ll * fact[i - 1] * i) % mod;
  auto pow = [&](int a, int b, int mod) {
    int ans = 1;
    while (b) {
      if (b & 1)
        ans = (1ll * ans * a) % mod;
      a = (1ll * a * a) % mod;
      b /= 2;
    }
    return ans;
  };
  static int f[1 << 20];
  for (int i = 1; i <= n; ++i)
    f[i] = i;
  function<int(int)> get = [&](int k) {
    return f[k] == k ? k : f[k] = get(f[k]);
  };
  for (int i = 0; i <= n + 5; ++i)
    inv[i] = pow(fact[i], mod - 2, mod);
  static pii s[1 << 20];
  multiset<pii> ss;
  for (int i = 1; i <= n; ++i) {
    fi >> s[i].x >> s[i].y;
    C[s[i].x].pb(i);
    ss.insert(mp(s[i].y, i));
  }
  vi go;
  for (auto it : ss)
    go.pb(it.y);
  int SZ = 0;
  for (auto node : go) {
    const int color = s[node].x;
    vii cur;
    for (auto it : C[color]) {
      if (s[it].y + s[node].y <= X)
        f[get(node)] = get(it);
      else if (ss.count(mp(s[it].y, it)))
        cur.pb(mp(s[it].y, it));
      ss.erase(mp(s[it].y, it));
    }
    C[color].clear();
    while (!ss.empty() && ss.begin()->x + s[node].y <= Y)
      f[get(ss.begin()->y)] = get(node), ss.erase(ss.begin());
    for (auto it : cur)
      ss.insert(it);
  }
  static vi Sets[1 << 20];
  for (int i = 1; i <= n; ++i)
    Sets[get(i)].pb(s[i].x);
  for (int i = 1; i <= n; ++i) {
    static map<int, int> M;
    M.clear();
    ans = (1ll * ans * fact[Sets[i].size()]) % mod;
    for (auto it : Sets[i])
      ++M[it];
    for (auto it : M)
      ans = (1ll * ans * inv[it.y]) % mod;
  }
  fo << ans << '\n';
  cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\n';
  return 0;
}