import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons; import std.math, std.numeric; alias Seg = Tuple!(long, "l", long, "r"); void main() { long d; int q; scan(d, q); long[] a; Seg[] ss = new Seg[](q); foreach (i ; 0 .. q) { long li, ri; scan(li, ri); ri++; ss[i] = Seg(li, ri); a ~= li; a ~= ri; } a = a.sort().uniq().array; debug { writeln("a:", a); } int[long] map; long[] rmap = new long[](2*q); foreach (int i, ai; a) { map[ai] = i; rmap[i] = ai; } debug { writeln("map:", map); writeln("rmap:", rmap); } auto sd = SqrtDecomp(2*q); long ans; foreach (i ; 0 .. q) { int l = min(map[ss[i].l], sd.query(map[ss[i].l]).l.to!int); int r = max(map[ss[i].r], sd.query(map[ss[i].r]).r.to!int); ans = max(ans, rmap[r] - rmap[l]); sd.update(l, r + 1, Seg(l, r)); writeln(ans); debug { writefln("(%d, %d)", l, r); writeln(sd.bucket); writeln(sd.data); } } } struct SqrtDecomp { private { enum root = 512; enum inf = 10^^9; Seg[] data; int N; Seg[] bucket; int K; } this(int n) { K = (n + root - 1) / root; N = K * root; data = new Seg[](N); data[] = Seg(inf, -inf); bucket = new Seg[](K); bucket[] = Seg(inf, -inf); } Seg query(int x) { int k = x / root; if (bucket[k].l == inf) { return data[x]; } else { foreach (i ; k * root .. (k + 1) * root) { data[i] = bucket[k]; } bucket[k] = Seg(inf, -inf); return data[x]; } } void update(int l, int r, Seg s) { foreach (k ; 0 .. K) { int lb = k * root, rb = (k + 1) * root; if (r <= lb || rb <= l) continue; if (l <= lb && rb <= r) { bucket[k] = s; } else { if (bucket[k].l != inf) { foreach (i ; k*root .. (k+1)*root) { data[i] = bucket[k]; } bucket[k] = Seg(inf, -inf); } foreach (i ; max(l, lb) .. min(r, rb)) { data[i] = s; } } } } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }