#include #include using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; constexpr int th = 1'000'000; vector tb(th, -1), R(th), L(th); atcoder::fenwick_tree fw(th); for(int i = 0; i < n; i++){ int l, r; cin >> l >> r; l--, r--; tb[l] = r; R[r]++; L[l]++; } ll ans = (ll)(n - 1) * n / 2; for(int l = th - 1; l >= 0; l--){ if(tb[l] == -1) continue; ans -= fw.sum(l, tb[l]); fw.add(l, 1); fw.add(tb[l], -1); } int lcnt = n; for(int i = 0; i < th; i++){ lcnt -= L[i]; ans -= (ll)(R[i]) * lcnt; } cout << ans << '\n'; }