package com.dota.treeMap; import java.util.Map; import java.util.TreeMap; public class Solution { } /** * 2276. 统计区间中的整数数目 */ class CountIntervals { TreeMap map = new TreeMap<>(); int cnt = 0; public CountIntervals() { } public void add(int left, int right) { Map.Entry inte = map.floorEntry(right); while (inte != null && inte.getValue() >= left) { int l = inte.getKey(); int r = inte.getValue(); left = Math.min(l, left); right = Math.max(r, right); cnt -= r - l + 1; map.remove(l); inte = map.floorEntry(right); } cnt += (right - left + 1); map.put(left, right); } public int count() { return cnt; } }