1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| class RangeModule { map<int, int> m; public: RangeModule() {} void addRange(int left, int right) { for (auto it = m.lower_bound(left); it != m.end() && it->second <= right; m.erase(it++)) { int l = it->second, r = it->first; left = min(left, l), right = max(right, r); } m[right] = left; } bool queryRange(int left, int right) { auto it = m.lower_bound(left); return it != m.end() && it->second <= left && it->first >= right; } void removeRange(int left, int right) { for (auto it = m.lower_bound(left + 1); it != m.end() && it->second <= right;) { if (it->second < left) { m[left] = it->second; } if (it->first > right) { m[it->first] = right; break; } else m.erase(it++); } } };
|