#include #define rep(i,a,b) for(int i=a;i=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() #pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b struct LazySegTree { // [L,R) vector dat, lazy; LazySegTree() { dat.resize(NV * 2, def); lazy.resize(NV * 2, ldef); } void update(int a, int b, V v, int k, int l, int r) { push(k, l, r); if (r <= a || b <= l) return; if (a <= l && r <= b) { setLazy(k, v); push(k, l, r); } else { update(a, b, v, k * 2 + 1, l, (l + r) / 2); update(a, b, v, k * 2 + 2, (l + r) / 2, r); dat[k] = comp(dat[k * 2 + 1], dat[k * 2 + 2]);}} V get(int a, int b, int k, int l, int r) { push(k, l, r); if (r <= a || b <= l) return def; if (a <= l && r <= b) return dat[k]; auto x = get(a, b, k * 2 + 1, l, (l + r) / 2); auto y = get(a, b, k * 2 + 2, (l + r) / 2, r); return comp(x, y);} void update(int a, int b, V v) { update(a, b, v, 0, 0, NV); } V get(int a, int b) { return get(a, b, 0, 0, NV); } // ---- Template --------------------------------------------------------------------------------- const V def = 0, ldef = -1; V comp(V l, V r) { return l + r; } void setLazy(int i, V v) { lazy[i] = v; } void push(int k, int l, int r) { if (lazy[k] != ldef) { // modify------------------------------ dat[k] = lazy[k] * (r - l); // ------------------------------------ if (r - l > 1) { setLazy(k * 2 + 1, lazy[k]); setLazy(k * 2 + 2, lazy[k]); } lazy[k] = ldef; } } }; /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ ll D; int Q; ll A[30101], B[30101]; LazySegTree st; //--------------------------------------------------------------------------------------------------- void _main() { cin >> D >> Q; rep(i, 0, Q) cin >> A[i] >> B[i]; rep(i, 0, Q) B[i]++; vector dic; rep(i, 0, Q) dic.push_back(A[i]), dic.push_back(B[i]); sort(all(dic)); dic.erase(unique(all(dic)), dic.end()); ll ans = 0; rep(q, 0, Q) { int a = lower_bound(all(dic), A[q]) - dic.begin(); int b = lower_bound(all(dic), B[q]) - dic.begin(); st.update(a, b, 1); int L, R; { // for L int ng = -1, ok = a; while (ng + 1 != ok) { int md = (ng + ok) / 2; if (st.get(md, a + 1) == a + 1 - md) ok = md; else ng = md; } L = ok; } { // for R if (st.get(b, b + 1) == 0) { R = b; } else { int ok = b, ng = 1 << 17; while (ok + 1 != ng) { int md = (ok + ng) / 2; if (st.get(b, md + 1) == md - b + 1) ok = md; else ng = md; } R = ng; } } chmax(ans, dic[R] - dic[L]); printf("%lld\n", ans); } }