import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.Scanner; import java.util.TreeSet; public class Main { public static class LazySetSumSegmentTree { int n; boolean[] dat, lazy; ArrayList dist; long[] l_max, c_max, r_max; boolean[] push; public LazySetSumSegmentTree(int n_, ArrayList dist) { int n = 1; while(n < n_){ n *= 2;} this.n = n; l_max = new long[this.n * 2 - 1]; c_max = new long[this.n * 2 - 1]; r_max = new long[this.n * 2 - 1]; this.dist = dist; dat = new boolean[this.n * 2 - 1]; lazy = new boolean[this.n * 2 - 1]; push = new boolean[this.n * 2 - 1]; } private void evaluate_lazy(int k, int l, int r){ if(!push[k]){ return; } dat[k] = lazy[k]; if(dat[k]){ //System.out.println(r + " " + l + " -> " + dist.get(r - 1) + " " + dist.get(l)); c_max[k] = l_max[k] = r_max[k] = (dist.get(r - 1) - dist.get(l) + 1); }else{ l_max[k] = c_max[k] = r_max[k] = 0; } if(k < n - 1){ lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k]; push[k * 2 + 1] = push[k * 2 + 2] = true; } lazy[k] = false; push[k] = false; } private void update_node(int k){ final int l = k * 2 + 1; final int r = k * 2 + 2; final long l_all_max = Math.max(Math.max(l_max[l], r_max[l]), c_max[l]); final long r_all_max = Math.max(Math.max(l_max[r], r_max[r]), c_max[r]); final long children_max = Math.max(l_all_max, r_all_max); c_max[k] = Math.max(children_max, (l_max[r] + r_max[l])); l_max[k] = l_max[l] + (dat[l] ? l_max[r] : 0); r_max[k] = r_max[r] + (dat[r] ? r_max[l] : 0); //System.out.println("l -> " + l_max[l] + " " + c_max[l] + " " + r_max[l]); //System.out.println("r -> " + l_max[r] + " " + c_max[r] + " " + r_max[r]); //System.out.println("k -> " + l_max[k] + " " + c_max[k] + " " + r_max[k]); dat[k] = dat[k*2+1] && dat[k*2+2]; } public void set(boolean v, int a, int b){ set(v, a, b, 0, 0, this.n); } public void set(boolean v, int a, int b, int k, int l, int r){ evaluate_lazy(k, l, r); if(r <= a || b <= l){ return; }else if(a <= l && r <= b){ lazy[k] = v; push[k] = true; evaluate_lazy(k, l, r); }else{ set(v, a, b, k * 2 + 1, l, (l + r) / 2); set(v, a, b, k * 2 + 2, (l + r) / 2, r); update_node(k); } } public long[] max(int a, int b){ return max(a, b, 0, 0, this.n); } public long[] max(int a, int b, int k, int l, int r){ evaluate_lazy(k, l, r); if(r <= a || b <= l){ /* 通らない予定 */ return new long[]{0, 0, 0}; }else if(a <= l && r <= b){ return new long[]{l_max[k], c_max[k], r_max[k]}; }else{ final long[] l_max_ret = max(a, b, k * 2 + 1, l, (l + r) / 2); final long[] r_max_ret = max(a, b, k * 2 + 2, (l + r) / 2, r); final long l_all_max = Math.max(Math.max(l_max_ret[0], l_max_ret[1]), l_max_ret[2]); final long r_all_max = Math.max(Math.max(r_max_ret[0], r_max_ret[1]), r_max_ret[2]); final long children_max = Math.max(l_all_max, r_all_max); final boolean l_cont_all = l_max_ret[1] == (dist.get((l + r) / 2 - 1) - dist.get(l) + 1); final boolean r_cont_all = r_max_ret[1] == (dist.get(r - 1) - dist.get((l + r) / 2) + 1); final long fst = l_max_ret[0] + Math.max(0, l_cont_all ? r_max_ret[0]: 0); final long snd = Math.max(children_max, (l_max_ret[2] + r_max_ret[0])); final long thd = r_max_ret[2] + Math.max(0, r_cont_all ? l_max_ret[2]: 0); update_node(k); return new long[]{fst, snd, thd}; } } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); final long D = sc.nextLong(); final int Q = sc.nextInt(); long[] as = new long[Q]; long[] bs = new long[Q]; TreeSet coord = new TreeSet(); for(int i = 0; i < Q; i++){ as[i] = sc.nextLong(); bs[i] = sc.nextLong(); coord.add(as[i]); coord.add(bs[i] + 0); coord.add(bs[i] + 1); } ArrayList coord_list = new ArrayList(coord); final int size = coord_list.size(); for(int i = 0; i < size; i++){ coord_list.add(Long.MAX_VALUE); } LazySetSumSegmentTree seg = new LazySetSumSegmentTree(size, coord_list); for(int q = 0; q < Q; q++){ final int a_index = Collections.binarySearch(coord_list, as[q]); final int b_index = Collections.binarySearch(coord_list, bs[q] + 1); //System.out.println(a_index + " " + b_index); seg.set(true, a_index, b_index); System.out.println(Arrays.stream(seg.max(0, size)).max().getAsLong()); } } }