c++ code


  • 0
    L

    // Draft version needs to be improved later on.

    void addRange(int left, int right) {
        auto x = find(left, right);
        m[x.first] = x.second;
    }
    bool queryRange(int left, int right) {
        auto it = m.upper_bound(left);
        return it != m.begin() && (--it)->second >= right;
    }
    void removeRange(int left, int right) {
        auto x = find(left, right);
        m[x.first] = max(x.first, left);
        m[right] = max(x.second, right);
    }
    map<int, int> m;  
    pair<int, int> find(int& left, int& right){
        auto l = m.upper_bound(left), r = m.upper_bound(right); 
        if (l != m.begin() && (--l)->second < left) ++l;
        if (l == r) return {left, right};
        int i = min(left, l->first), j = max(right, (--r)->second);
        m.erase(l, ++r);
        return {i, j};
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.