#include #include struct Graph { const int n; std::vector> adj, radj; std::vector scc; Graph(int _n) : n(_n), adj(n), radj(n), scc(n, false) {} void add_edge(int src, int dst) { adj[src].push_back(dst); radj[dst].push_back(src); } void Dfs(const int cur, auto &ord) { scc[cur] = true; for (auto dst : adj[cur]) if (!scc[dst]) Dfs(dst, ord); ord.push_back(cur); } void RevDfs(const int id, const int cur) { scc[cur] = id; for (auto dst : radj[cur]) if (scc[dst] == -1) RevDfs(id, dst); } void StronglyConnectedComponents() { std::vector ord; for (int v = 0; v < n; ++v) if (!scc[v]) Dfs(v, ord); std::fill(scc.begin(), scc.end(), -1); for (int i = n - 1, no = 0; 0 <= i; --i) if (scc[ord[i]] == -1) RevDfs(no++, ord[i]); } }; // -------------8<------- start of library -------8<------------------------ struct TwoSat { const int n; std::vector sol; Graph g; TwoSat(int _n) : n(_n), sol(n + 1, true), g(2 * n) {} void add_closure(int x1, bool lt1, int x2, bool lt2) { g.add_edge(x1 + (lt1 ? n : 0), x2 + (lt2 ? 0 : n)); g.add_edge(x2 + (lt2 ? n : 0), x1 + (lt1 ? 0 : n)); } bool Check() const { return sol[n]; } bool Solve() { g.StronglyConnectedComponents(); for (int x = 0; x < n; ++x) { if (g.scc[x] == g.scc[x + n]) return sol[n] = false; sol[x] = (g.scc[x] > g.scc[x + n]); } return sol[n]; } }; // -------------8<------- end of library ---------8------------------------- inline bool Is(const auto &s1, const auto &s2) { return (s1.first <= s2.first && s2.first <= s1.second) || (s1.first <= s2.second && s2.second <= s1.second) || (s2.first <= s1.first && s1.first <= s2.second) || (s2.first <= s1.second && s1.second <= s2.second); } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int n, m; std::cin >> n >> m; std::vector> seg(n), rseg(n); for (int i = 0; i < n; ++i) { std::cin >> seg[i].first >> seg[i].second; rseg[i] = std::make_pair(m - 1 - seg[i].second, m - 1 - seg[i].first); } TwoSat sat(n); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (Is(seg[i], seg[j])) sat.add_closure(i, false, j, false); if (Is(seg[i], rseg[j])) sat.add_closure(i, false, j, true); if (Is(rseg[i], seg[j])) sat.add_closure(i, true, j, false); if (Is(rseg[i], rseg[j])) sat.add_closure(i, true, j, true); } } std::cout << (sat.Solve() ? "YES" : "NO") << std::endl; return 0; }