Language: C++

#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define int long long
#define pb push_back
#define x first
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

/// find_by_order()
/// order_of_key()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int randll(int l = INT_MIN, int r = INT_MAX) {
    return uniform_int_distribution<int>(l, r)(rng);
}
const int INF = 1e9, MOD = 1e9 + 7; /// think
const ll LINF = 1e18;


const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
inline bool inside (int x, int y, int n, int m) {
    return 0 <= x && 0 <= y && x < n && y < m;
}

template<class T> bool umin (T &a, T b) {return a > b ? (a = b, true) : false; }
template<class T> bool umax (T &a, T b) {return a < b ? (a = b, true) : false; }

set<pii> cur;
multiset <int> mn;
map <pii, int> to;
inline void add (pii a, int x) {
    cur.insert(a);
    to[a] = x;
    mn.insert(x);
}
inline int get_value (int p) {
    auto it = --cur.upper_bound({p, INF});
    assert(it != cur.end() && it->x <= p && it->y >= p);
    return to[*it] + p - it->x;
}
inline void upd (int l, int r) {
    auto it = --cur.upper_bound({l, INF});
    vector <pair<pii, int> > ad;
    vector <pii> del;
    for (; it != cur.end(); it++) {
        if (it->x > r) break;
        if (it->x < l && r < it->y) {
            ad.pb({{it->x, l - 1}, to[*it]});
            ad.pb({{r + 1, it->y}, to[*it] + r - it->x + 1});
            mn.erase(mn.find(to[*it]));
            del.pb(*it);
            to.erase(*it);
        }
        else if (it->x < l) {
            ad.pb({{it->x, l - 1}, to[*it]});
            mn.erase(mn.find(to[*it]));
            del.pb(*it);
            to.erase(*it);
        }
        else if (r < it->y) {
            ad.pb({{r + 1, it->y}, to[*it] + r - it->x + 1});
            mn.erase(mn.find(to[*it]));
            del.pb(*it);
            to.erase(*it);
        }
        else {
            mn.erase(mn.find(to[*it]));
            del.pb(*it);
            to.erase(*it);
        }
    }
    for (auto &i : del) {
        cur.erase(i);
    }
    for (auto &i : ad) {
        add(i.x, i.y);
    }
}
main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    int a, b;
    add({0, m - 1}, 0);
    for (int i = 0; i < n; i++) {
        cin >> a >> b;
        --a, --b;
        int val = a ? get_value(a - 1) : INF;
        upd (a, b);
        add({a, b}, val + 1);
        if (*mn.begin() >= n + m) {
            for (int j = 0; j < n - i; j++) {
                cout << "-1\n";
            }
            rr;
        }
        cout << *mn.begin() + i + 1 << '\n';
    }
}
#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define int long long
#define pb push_back
#define x first
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

/// find_by_order()
/// order_of_key()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int randll(int l = INT_MIN, int r = INT_MAX) {
    return uniform_int_distribution<int>(l, r)(rng);
}
const int INF = 1e9, MOD = 1e9 + 7; /// think
const ll LINF = 1e18;


const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
inline bool inside (int x, int y, int n, int m) {
    return 0 <= x && 0 <= y && x < n && y < m;
}

template<class T> bool umin (T &a, T b) {return a > b ? (a = b, true) : false; }
template<class T> bool umax (T &a, T b) {return a < b ? (a = b, true) : false; }

set<pii> cur;
multiset <int> mn;
map <pii, int> to;
inline void add (pii a, int x) {
    cur.insert(a);
    to[a] = x;
    mn.insert(x);
}
inline int get_value (int p) {
    auto it = --cur.upper_bound({p, INF});
    assert(it != cur.end() && it->x <= p && it->y >= p);
    return to[*it] + p - it->x;
}
inline void upd (int l, int r) {
    auto it = --cur.upper_bound({l, INF});
    vector <pair<pii, int> > ad;
    vector <pii> del;
    for (; it != cur.end(); it++) {
        if (it->x > r) break;
        if (it->x < l && r < it->y) {
            ad.pb({{it->x, l - 1}, to[*it]});
            ad.pb({{r + 1, it->y}, to[*it] + r - it->x + 1});
            mn.erase(mn.find(to[*it]));
            del.pb(*it);
            to.erase(*it);
        }
        else if (it->x < l) {
            ad.pb({{it->x, l - 1}, to[*it]});
            mn.erase(mn.find(to[*it]));
            del.pb(*it);
            to.erase(*it);
        }
        else if (r < it->y) {
            ad.pb({{r + 1, it->y}, to[*it] + r - it->x + 1});
            mn.erase(mn.find(to[*it]));
            del.pb(*it);
            to.erase(*it);
        }
        else {
            mn.erase(mn.find(to[*it]));
            del.pb(*it);
            to.erase(*it);
        }
    }
    for (auto &i : del) {
        cur.erase(i);
    }
    for (auto &i : ad) {
        add(i.x, i.y);
    }
}
main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    int a, b;
    add({0, m - 1}, 0);
    for (int i = 0; i < n; i++) {
        cin >> a >> b;
        --a, --b;
        int val = a ? get_value(a - 1) : INF;
        upd (a, b);
        add({a, b}, val + 1);
        if (*mn.begin() >= n + m) {
            for (int j = 0; j < n - i; j++) {
                cout << "-1\n";
            }
            rr;
        }
        cout << *mn.begin() + i + 1 << '\n';
    }
}


Compiler output:
a.cc:103:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  103 | main()
      | ^~~~
a.cc:163:9: error: redefinition of 'std::mt19937 rng'
  163 | mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
      |         ^~~
a.cc:36:9: note: 'std::mt19937 rng' previously declared here
   36 | mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
      |         ^~~
a.cc:164:12: error: redefinition of 'int randll(int, int)'
  164 | inline int randll(int l = INT_MIN, int r = INT_MAX) {
      |            ^~~~~~
a.cc:37:12: note: 'int randll(int, int)' previously defined here
   37 | inline int randll(int l = INT_MIN, int r = INT_MAX) {
      |            ^~~~~~
a.cc:167:11: error: redefinition of 'const int INF'
  167 | const int INF = 1e9, MOD = 1e9 + 7; /// think
      |           ^~~
a.cc:40:11: note: 'const int INF' previously defined here
   40 | const int INF = 1e9, MOD = 1e9 + 7; /// think
      |           ^~~
a.cc:167:22: error: redefinition of 'const int MOD'
  167 | const int INF = 1e9, MOD = 1e9 + 7; /// think
      |                      ^~~
a.cc:40:22: note: 'const int MOD' previously defined here
   40 | const int INF = 1e9, MOD = 1e9 + 7; /// think
      |                      ^~~
a.cc:168:10: error: redefinition of 'const ll LINF'
  168 | const ll LINF = 1e18;
      |          ^~~~
a.cc:41:10: note: 'const ll LINF' previously defined here
   41 | const ll LINF = 1e18;
      |          ^~~~
a.cc:171:11: error: redefinition of 'const int dx []'
  171 | const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
      |           ^~
a.cc:44:11: note: 'const int dx [4]' previously defined here
   44 | const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
      |           ^~
a.cc:171:33: error: redefinition of 'const int dy []'
  171 | const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
      |                                 ^~
a.cc:44:33: note: 'const int dy [4]' previously defined here
   44 | const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
      |                                 ^~
a.cc:172:13: error: redefinition of 'bool inside(int, int, int, int)'
  172 | inline bool inside (int x, int y, int n, int m) {
      |             ^~~~~~
a.cc:45:13: note: 'bool inside(int, int, int, int)' previously defined here
   45 | inline bool inside (int x, int y, int n, int m) {
      |             ^~~~~~
a.cc:176:24: error: redefinition of 'template<class T> bool umin(T&, T)'
  176 | template<class T> bool umin (T &a, T b) {return a > b ? (a = b, true) : false; }
      |                        ^~~~
a.cc:49:24: note: 'template<class T> bool umin(T&, T)' previously declared here
   49 | template<class T> bool umin (T &a, T b) {return a > b ? (a = b, true) : false; }
      |                        ^~~~
a.cc:177:24: error: redefinition of 'template<class T> bool umax(T&, T)'
  177 | template<class T> bool umax (T &a, T b) {return a < b ? (a = b, true) : false; }
      |                        ^~~~
a.cc:50:24: note: 'template<class T> bool umax(T&, T)' previously declared here
   50 | template<class T> bool umax (T &a, T b) {return a < b ? (a = b, true) : false; }
      |                        ^~~~
a.cc:179:10: error: redefinition of 'std::set<std::pair<int, int> > cur'
  179 | set<pii> cur;
      |          ^~~
a.cc:52:10: note: 'std::set<std::pair<int, int> > cur' previously declared here
   52 | set<pii> cur;
      |          ^~~
a.cc:180:16: error: redefinition of 'std::multiset<int> mn'
  180 | multiset <int> mn;
      |                ^~
a.cc:53:16: note: 'std::multiset<int> mn' previously declared here
   53 | multiset <int> mn;
      |                ^~
a.cc:181:16: error: redefinition of 'std::map<std::pair<int, int>, int> to'
  181 | map <pii, int> to;
      |                ^~
a.cc:54:16: note: 'std::map<std::pair<int, int>, int> to' previously declared here
   54 | map <pii, int> to;
      |                ^~
a.cc:182:13: error: redefinition of 'void add(pii, int)'
  182 | inline void add (pii a, int x) {
      |             ^~~
a.cc:55:13: note: 'void add(pii, int)' previously defined here
   55 | inline void add (pii a, int x) {
      |             ^~~
a.cc:187:12: error: redefinition of 'int get_value(int)'
  187 | inline int get_value (int p) {
      |            ^~~~~~~~~
a.cc:60:12: note: 'int get_value(int)' previously defined here
   60 | inline int get_value (int p) {
      |            ^~~~~~~~~
a.cc:192:13: error: redefinition of 'void upd(int, int)'
  192 | inline void upd (int l, int r) {
      |             ^~~
a.cc:65:13: note: 'void upd(int, int)' previously defined here
   65 | inline void upd (int l, int r) {
      |             ^~~
a.cc:230:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  230 | main()
      | ^~~~
a.cc:230:1: error: redefinition of 'int main()'
a.cc:103:1: note: 'int main()' previously defined here
  103 | main()
      | ^~~~