#include #define show(x) std::cerr << #x << " = " << x << std::endl using ull = unsigned long long; constexpr std::size_t PC(ull v) { return v = (v & 0x5555555555555555ULL) + (v >> 1 & 0x5555555555555555ULL), v = (v & 0x3333333333333333ULL) + (v >> 2 & 0x3333333333333333ULL), v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL, static_cast(v * 0x0101010101010101ULL >> 56 & 0x7f); } class WaveletMatrix { public: static constexpr std::size_t L = 18; using T = uint; template WaveletMatrix(const InIt first, const InIt last) : sz(std::distance(first, last)) { std::vector b(sz); std::vector v(first, last); for (std::size_t i = 0, j = L - 1; i < L; i++, j--) { std::vector z, o; for (std::size_t i = 0; i < sz; i++) { b[i] = (v[i] >> j) & 1, (b[i] ? o : z).push_back(v[i]); } table.push_back(FullyIndexableDictionary{b}), v = z, zero.push_back(z.size()); for (const T e : o) { v.push_back(e); } } } std::size_t noLessThan(std::size_t l, std::size_t r, const T v) const { std::size_t off = 0; for (std::size_t i = 0, j = L - 1; i < L; i++, j--) { const bool b = (v >> j) & 1; const std::size_t rl = table[i].rank(l), rr = table[i].rank(r); l = (b ? rl + zero[i] : l - rl), r = (b ? rr + zero[i] : r - rr), off += (b ? 0 : rr - rl); } return r - l + off; } std::size_t rangeFreq(const std::size_t l, const std::size_t r, const T vinf, const T vsup) const { return noLessThan(l, r, vinf) - noLessThan(l, r, vsup); } std::size_t rank(const std::size_t l, const std::size_t r, const T v) const { return rangeFreq(l, r, v, v + 1); } private: class FullyIndexableDictionary { private: using B = unsigned long long; static constexpr std::size_t BS = sizeof(B) * 8; const std::size_t sz; std::vector data; std::vector large; public: FullyIndexableDictionary(const std::vector& b) : sz(b.size()), data((sz + BS) / BS, 0), large((sz + BS) / BS, 0) { std::size_t one = 0; for (std::size_t i = 0; i < sz; i++) { data[i / BS] |= ((B)b[i] << (i % BS)), one += b[i]; if (i % BS == BS - 1) { large[(i + 1) / BS] = one; } } } bool operator[](const std::size_t n) const { return (data[n / BS] >> (n % BS)) & 1; } std::size_t rank(const std::size_t n) const { return large[n / BS] + (n % BS == 0 ? 0 : PC(data[n / BS] & ((1ULL << (n % BS)) - 1))); } }; const std::size_t sz; std::vector zero; std::vector table; }; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); std::size_t N, M; std::cin >> N >> M; using P = std::pair; std::vector

p(N); for (std::size_t i = 0; i < N; i++) { std::cin >> p[i].first >> p[i].second; if (p[i].first > p[i].second) { std::swap(p[i].first, p[i].second); }; } std::sort(p.begin(), p.end()); std::vector b(N); for (std::size_t i = 0; i < N; i++) { b[i] = p[i].second; } WaveletMatrix wm{b.begin(), b.end()}; ull ans = 0; for (std::size_t i = 0; i < N - 1; i++) { const std::size_t isup = std::upper_bound(p.begin(), p.end(), P{b[i], 0}) - p.begin(); ans += wm.noLessThan(i + 1, isup, b[i]); } std::cout << ans << std::endl; return 0; }