ll lowbit(ll x){ return x & -x; } ll sum(ll i){ ll res = 0; for (; i > 0; i -= lowbit(i)) res += bit[i]; return res; } voidadd(ll i, ll x){ for (; i <= cnt; i += lowbit(i)) bit[i] += x; }
structP { int x, y; P(int x = 0, int y = 0):x(x), y(y) {} }a[maxn];
int mx[maxn]; bool sc[maxn]; // scope intmain(){ int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d%d", &a[i].x, &a[i].y); sort(a, a + n, cmpx); int val = 1; for (int i = 0; i < n; i++) { int temp = a[i].x; a[i].x = val; if (temp != a[i + 1].x) val++; } sort(a, a + n, cmpy); val = 1; for (int i = 0; i < n; i++) { int temp = a[i].y; a[i].y = val; if (temp != a[i + 1].y) val++; } for (int i = 0; i < n; i++) Maxi(mx[a[i].x], a[i].y); ll ans = 0; for (int i = 0; i < n - 1; i++) { int x = a[i].x, y = a[i].y, nx = a[i + 1].x, ny = a[i + 1].y; if (!sc[x] && y < mx[x]) { sc[x] = true; add(x, 1); } if (y == ny && nx > x + 1) { ans += sum(nx - 1) - sum(x); } if (sc[x] && y == mx[x]) { sc[x] = false; add(x, -1); } } ans += n; printf("%lld\n", ans); }