#include #include #include using namespace std; const int N = 2 << 17; vector seg[N * 2]; int lt(vector &a, int x) { return lower_bound(a.begin(), a.end(), x) - a.begin(); } int gt(vector &a, int x) { return a.end() - upper_bound(a.begin(), a.end(), x); } int lt(int l, int r, int x) { l += N; r += N; int res = 0; while (l < r) { if (l & 1) res += lt(seg[l++], x); if (r & 1) res += lt(seg[--r], x); l >>= 1; r >>= 1; } return res; } int gt(int l, int r, int x) { l += N; r += N; int res = 0; while (l < r) { if (l & 1) res += gt(seg[l++], x); if (r & 1) res += gt(seg[--r], x); l >>= 1; r >>= 1; } return res; } int main() { int n, m; scanf("%d %d", &n, &m); long long ans = 0; vector a(m), b(m); for (int i = 0; i < n; i++) { scanf("%d %d", &a[i], &b[i]); if (a[i] > b[i]) { swap(a[i], b[i]); } seg[a[i] + N].push_back(b[i]); seg[b[i] + N].push_back(a[i]); } for (int i = N - 1; i >= 1; i--) { auto &x = seg[i * 2]; auto &y = seg[i * 2 + 1]; seg[i].resize(x.size() + y.size()); merge(x.begin(), x.end(), y.begin(), y.end(), seg[i].begin()); } for (int i = 0; i < m; i++) { ans += lt(a[i], b[i], a[i]); ans += gt(a[i], b[i], b[i]); } printf("%lld\n", ans / 2); return 0; }