static void Main() { int n, m; sc.Multi(out n, out m); var a = new P[n]; var b = new P[m]; var all = new List <int>(); for (int i = 0; i < n; i++) { a[i] = sc.Pair <int, int>(); all.Add(a[i].v1); all.Add(a[i].v2); } for (int i = 0; i < m; i++) { b[i] = sc.Pair <int, int>(); all.Add(b[i].v1); all.Add(b[i].v2); } all = all.Distinct().ToList(); all.Sort(); var zip = all.Select((x, i) => new { x, i }).ToDictionary(x => x.x, x => x.i); a = a.Select(x => new P(zip[x.v1], zip[x.v2])).ToArray(); b = b.Select(x => new P(zip[x.v1], zip[x.v2])).ToArray(); Array.Sort(a, (x, y) => y.v2.CompareTo(x.v2)); Array.Sort(b, (x, y) => y.v1.CompareTo(x.v1)); var sg = new Segtree <P>(all.Count, (x, y) => x.v2 > y.v2 ? x.v1 > 0 ? x : y : y.v1 > 0 ? y : x, new P(0, -1)); for (int i = 0; i < all.Count; i++) { sg.assign(i, new P(0, i)); } sg.all_update(); int bi = 0, ans = 0; for (int i = 0; i < n; i++) { while (bi < m && a[i].v2 <= b[bi].v1) { sg.update(b[bi].v2, new P(sg.look(b[bi].v2).v1 + 1, b[bi].v2)); ++bi; } var p = sg.run(0, a[i].v1 + 1); if (p.v1 > 0) { ++ans; sg.update(p.v2, new P(p.v1 - 1, p.v2)); } } Prt(ans); sw.Flush(); }