// 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; int sum(int i) { int 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; } typedef struct { int a, b; } T; T t[100003]; int cmp(const void *a, const void *b) { return ((T *)a)->a - ((T *)b)->a; } int main() { int i, N, M, a, b, ans; N = in(), M = in(); for (i = 0; i < N; i++) { a = in(), b = in(); if (a < b) t[i].a = a, t[i].b = b; else t[i].a = b, t[i].b = a; } qsort(t, N, sizeof(T), cmp); n = M, ans = 0; for (i = 0; i < N; i++) { ans += sum(t[i].b) - sum(t[i].a); add(t[i].b); } printf("%d\n", ans); return 0; }