// yukicoder: No.743 Segments on a Polygon // 2019.5.15 bal4u #include #include #if 1 #define gc() getchar_unlocked() #else #define gc() getchar() #endif int in() { int n = 0, c = gc(); do n = 10*n + (c & 0xf), c = gc(); while (c >= '0'); return n; } // Bit木 library #define MAX 200005 int bit[MAX]; int n; long long sum(int i) { long long s = 0; while (i > 0) s += bit[i], i -= i & -i; return s; } void add(int i) { while (i <= n) bit[i]++, i += i & -i; } int f[MAX]; int main() { int i, N, M, a, b; long long ans; N = in(), M = in(); for (i = 0; i < N; i++) { a = in(), b = in(); if (a < b) f[a] = b; else f[b] = a; } // bit木で集計 n = M, ans = 0; for (i = 0; i < M; i++) if (f[i]) { ans += sum(f[i]) - sum(i); add(f[i]); } printf("%lld\n", ans); return 0; }