set维护区间即可,易得答案为最大区间长度加一

注意用 set.lower_bound() 加速寻找。

#include <bits/stdc++.h>

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int q; std::cin >> q;
    std::set<std::pair<int, int>> set;
    int ans = 1;
    while (q--) {
        int l, r; std::cin >> l >> r;
        if (set.empty()) {
            set.emplace(l, r);
            ans = std::max(ans, r - l + 2);
        } else {
            bool flag = true;
            auto it = set.lower_bound({l, r});
            if (it != set.end()) {
                flag &= r < it->first;
            }
            if (it != set.begin()) {
                flag &= l > (--it)->second;
            }
            if (flag) {
                set.emplace(l, r);
                ans = std::max(ans, r - l + 2);
            }
        }
        std::cout << ans << '\n';
    }
    return 0;
}