#include #define show(x) std::cerr << #x << " = " << x << std::endl using ll = long long; using ull = unsigned long long; using ld = long double; constexpr ll MOD = 1000000007LL; template constexpr T INF = std::numeric_limits::max() / 10; std::mt19937 mt{std::random_device{}()}; constexpr std::size_t PC(const ull v) { ull count = (v & 0x5555555555555555ULL) + ((v >> 1) & 0x5555555555555555ULL); count = (count & 0x3333333333333333ULL) + ((count >> 2) & 0x3333333333333333ULL); count = (count & 0x0f0f0f0f0f0f0f0fULL) + ((count >> 4) & 0x0f0f0f0f0f0f0f0fULL); count = (count & 0x00ff00ff00ff00ffULL) + ((count >> 8) & 0x00ff00ff00ff00ffULL); count = (count & 0x0000ffff0000ffffULL) + ((count >> 16) & 0x0000ffff0000ffffULL); return static_cast((count & 0x00000000ffffffffULL) + ((count >> 32) & 0x00000000ffffffffULL)); } constexpr std::size_t LG(ull v) { return v == 0 ? 0 : (v--, v |= (v >> 1), v |= (v >> 2), v |= (v >> 4), v |= (v >> 8), v |= (v >> 16), v |= (v >> 32), PC(v)); } constexpr ull SZ(const ull v) { return 1ULL << LG(v); } template class SegmentTree { public: using BaseMonoid = Monoid; using T = typename Monoid::T; SegmentTree(const std::size_t n) : data_num(n), half(SZ(n)), value(half << 1, Monoid::id()) {} template SegmentTree(const InIt first, const InIt last) : data_num(distance(first, last)), half(SZ(data_num)), value(half << 1, Monoid::id()) { std::copy(first, last, value.begin() + half); for (std::size_t i = half - 1; i >= 1; i--) { up(i); } } T get(const std::size_t a) const { return value[a + half]; } void set(std::size_t a, const T& val) { value[a += half] = val; while (a >>= 1) { up(a); } } template F query(std::size_t L, std::size_t R, const QueryOp& query_op, const AccOp& acc_op) const { F accl{}, accr{}; for (L += half, R += half; L < R; L >>= 1, R >>= 1) { if (L & 1) { accl = acc_op(accl, query_op(value[L++])); } if (R & 1) { accr = acc_op(query_op(value[--R]), accr); } } return acc_op(accl, accr); } private: void up(const std::size_t i) { value[i] = acc(value[i << 1], value[i << 1 | 1]); } const std::size_t data_num, half; std::vector value; const Monoid acc{}; }; struct MergedSequence { using F = ll; using T = std::vector; T operator()(const T& a, const T& b) const { T c(a.size() + b.size()); std::merge(a.begin(), a.end(), b.begin(), b.end(), c.begin()); return c; } static T id() { return T{}; } }; int main() { int N, M; std::cin >> N >> M; std::vector> v(M + 1, {-1}); using P = std::pair; std::vector

p(N); for (int i = 0; i < N; i++) { int a, b; std::cin >> a >> b; if (a > b) { std::swap(a, b); } v[a] = {b}; p[i] = {a, b}; } SegmentTree seg(v.begin(), v.end()); ll ans = 0; for (int i = 0; i < N; i++) { const int s = p[i].first, t = p[i].second; const auto f1 = [&](const auto& interval) { return interval.end() - upper_bound(interval.begin(), interval.end(), t); }; static const auto f2 = [](const std::size_t x, const std::size_t y) { return x + y; }; const std::size_t val = seg.query(s, t, f1, f2); ans += val; } std::cout << ans << std::endl; return 0; }