Language: C++

#pragma region Header
#ifdef DEBUG
#include "debug.hpp"
#else
#define NDEBUG
#define debug(...)
#define IFD(...)
#endif
#include <utility>
#include <cstdint>
#include <cmath>
#include <vector>
#include <array>
#include <deque>
#include <queue>
#include <stack>
#include <tuple>
#include <set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <string>
#include <functional>
#include <algorithm>
#include <numeric>
#include <stdexcept>
#include <iostream>
#include <iomanip>
using namespace std;
using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
#define rep(i, n) for (int i = 0; i < static_cast<int>(n); ++i)
#define repd(i, n) for (int i = static_cast<int>(n) - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = static_cast<int>(a); i <= static_cast<int>(b); ++i)
#define FORD(i, b, a) for (int i = static_cast<int>(b); i >= static_cast<int>(a); --i)
template <typename T>
int SZ(T const &x)
{
    return static_cast<int>(x.size());
}
template <class T>
bool chmax(T &a, T const &b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>
bool chmin(T &a, T const &b)
{
    if (b < a)
    {
        a = b;
        return true;
    }
    return false;
}
#pragma endregion

struct intvl
{
    int l;
    int r;
};

template <typename M>
struct seg_tree
{
    int n;
    using T = typename M::T;
    using T_act = typename M::T_act;

private:
    vector<T> data;
    vector<T_act> data_act;

    // ノードから区間への対応
    vector<intvl> intvl_s;

    // ノードの端点を得る
    intvl const &intvl_of_node(int const i)
    {
        return intvl_s[i];
    }

    // (2ベキでないときに出てくる)不要なノードならfalse
    bool is_valid_node(int const i)
    {
        intvl lr = intvl_of_node(i);
        return lr.l < lr.r;
    }

    // ノードiの値
    T get(int const i)
    {
        intvl lr = intvl_of_node(i);
        return M::act(data[i], data_act[i], lr);
    }

    // ノードiの親すべてから伝播
    void propagate(int const i)
    {
        for (int e = __builtin_ctz(i); e >= 1; e--)
        {
            int pa = i >> e;
            if (not is_valid_node(pa))
            {
                continue;
            }

            assert(data[pa] == M::op(get(2 * pa), get(2 * pa + 1)));

            for (int ch = 2 * pa; ch <= 2 * pa + 1; ch++)
            {
                data_act[ch] = M::op_act(data_act[ch], data_act[pa]);
            }
            data[pa] = get(pa);
            data_act[pa] = M::id_act;

            assert(data[pa] == M::op(get(2 * pa), get(2 * pa + 1)));
        }
    }

    // ノードiの親すべてに変更を反映
    void reflect_change(int i)
    {
        while (i > 1)
        {
            i /= 2;
            if (not is_valid_node(i))
            {
                break;
            }
            data[i] = M::op(get(2 * i), get(2 * i + 1));
        }
    }

    // ノードから区間の端点への対応を前計算
    void init_intvl_s()
    {
        for (int i = 0; i < n; i++)
        {
            intvl_s[n + i] = {i, i + 1};
        }
        for (int i = n - 1; i >= 1; i--)
        {
            intvl_s[i] = {intvl_s[2 * i].l, intvl_s[2 * i + 1].r};
        }
    }

public:
    // idで初期化
    explicit seg_tree(int const size)
        : n(size), data(2 * size, M::id), data_act(2 * size, M::id_act), intvl_s(2 * size)
    {
        init_intvl_s();
    }

    // a_sで初期化
    seg_tree(int const size, vector<T> const &a_s)
        : n(size), data(2 * size), data_act(2 * size, M::id_act), intvl_s(2 * size)
    {
        for (int i = 0; i < n; i++)
        {
            data[n + i] = a_s[i];
        }
        for (int i = n - 1; i >= 1; i--)
        {
            if (not is_valid_node(i))
            {
                continue;
            }
            data[i] = M::op(data[2 * i], data[2 * i + 1]);
        }

        init_intvl_s();
    }

    void act(T_act const u, intvl const lr)
    {
        assert(0 <= lr.l && lr.l < lr.r && lr.r <= n);

        propagate(lr.l + n);
        propagate(lr.r - 1 + n);

        int l = lr.l + n, r = lr.r + n;
        while (l < r)
        {
            if (l & 1)
            {
                data_act[l] = M::op_act(data_act[l], u);
                l++;
            }
            if (r & 1)
            {
                r--;
                data_act[r] = M::op_act(data_act[r], u);
            }
            l /= 2;
            r /= 2;
        }

        reflect_change(lr.l + n);
        reflect_change(lr.r - 1 + n);
    }

    // 区間和を返す
    T query(intvl const lr)
    {
        assert(0 <= lr.l && lr.l < lr.r && lr.r <= n);

        propagate(lr.l + n);
        propagate(lr.r - 1 + n);

        int l = lr.l + n, r = lr.r + n;
        T ret_l = M::id, ret_r = M::id;
        while (l < r)
        {
            if (l & 1)
            {
                ret_l = M::op(ret_l, get(l));
                l++;
            }
            if (r & 1)
            {
                r--;
                ret_r = M::op(get(r), ret_r);
            }
            l /= 2;
            r /= 2;
        }

        return M::op(ret_l, ret_r);
    }
};

struct mon
{
    using T = int;
    using T_act = optional<int>;

    static inline vector<T> vec;

    static T op(T a, T b)
    {
        return min(a, b);
    }
    static constexpr inline T id = 2'00'001;

    static T_act op_act(T_act u, T_act v)
    {
        if (not v)
        {
            return u;
        }
        return v.value();
    }
    static constexpr inline T_act id_act = nullopt;

    static T act(T a, T_act u, intvl lr)
    {
        if (not u)
        {
            return a;
        }
        return u.value() + lr.l;
    }
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int h, w;
    cin >> h >> w;
    // 0-based, [a,b)
    vector ab_s(h, pair<int, int>());
    rep(y, h)
    {
        int a, b;
        cin >> a >> b;
        --a;
        ab_s[y] = {a, b};
    }

    vector init(w + 1, 0);
    init[0] = mon::id;
    seg_tree<mon> seg(w + 1, init);
    rep(y, ab_s.size())
    {
        auto &[l, r] = ab_s[y];
        auto s = seg.query({l, l + 1});
        seg.act(make_optional(s - l), {l + 1, r + 1});
        IFD(rep(x, w + 1) { debug(x, seg.query({x, x + 1})); })
        auto res = seg.query({0, w + 1});
        if (res >= mon::id)
        {
            res = -1;
        }
        else
        {
            res += y + 1;
        }
        cout << res << '\n';
    }

    return 0;
}


Compiler output:
a.cc: In member function 'void seg_tree<M>::act(T_act, intvl)':
a.cc:186:9: error: there are no arguments to 'assert' that depend on a template parameter, so a declaration of 'assert' must be available [-fpermissive]
  186 |         assert(0 <= lr.l && lr.l < lr.r && lr.r <= n);
      |         ^~~~~~
a.cc:186:9: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc: In member function 'seg_tree<M>::T seg_tree<M>::query(intvl)':
a.cc:215:9: error: there are no arguments to 'assert' that depend on a template parameter, so a declaration of 'assert' must be available [-fpermissive]
  215 |         assert(0 <= lr.l && lr.l < lr.r && lr.r <= n);
      |         ^~~~~~
a.cc: At global scope:
a.cc:245:19: error: 'optional' does not name a type
  245 |     using T_act = optional<int>;
      |                   ^~~~~~~~
a.cc:255:12: error: 'T_act' does not name a type
  255 |     static T_act op_act(T_act u, T_act v)
      |            ^~~~~
a.cc:263:29: error: 'T_act' does not name a type
  263 |     static constexpr inline T_act id_act = nullopt;
      |                             ^~~~~
a.cc:265:23: error: 'T_act' has not been declared
  265 |     static T act(T a, T_act u, intvl lr)
      |                       ^~~~~
a.cc: In static member function 'static mon::T mon::act(T, int, intvl)':
a.cc:271:18: error: request for member 'value' in 'u', which is of non-class type 'int'
  271 |         return u.value() + lr.l;
      |                  ^~~~~
a.cc: In instantiation of 'struct seg_tree<mon>':
a.cc:295:23:   required from here
  295 |     seg_tree<mon> seg(w + 1, init);
      |                       ^
a.cc:76:11: error: no type named 'T_act' in 'struct mon'
   76 |     using T_act = typename M::T_act;
      |           ^~~~~
a.cc:80:19: error: no type named 'T_act' in 'struct mon'
   80 |     vector<T_act> data_act;
      |                   ^~~~~~~~
a.cc: In function 'int main()':
a.cc:300:13: error: 'struct seg_tree<mon>' has no member named 'act'
  300 |         seg.act(make_optional(s - l), {l + 1, r + 1});
      |             ^~~
a.cc:300:17: error: 'make_optional' was not declared in this scope
  300 |         seg.act(make_optional(s - l), {l + 1, r + 1});
      |                 ^~~~~~~~~~~~~
a.cc:29:1: note: 'std::make_optional' is defined in header '<optional>'; this is probably fixable by adding '#include <optional>'
   28 | #include <iomanip>
  +++ |+#include <optional>
   29 | using namespace std;