import java.util.*; import java.io.*; class Main { static final int ofs = 1; static int N, Q; static int A[]; static long res = 0, pls = 0; static BIT sl, sr; static LazySegmentTree sa; static boolean state[]; public static long calc_inv(int A[]) { int N = A.length; Integer ord[] = new Integer[N]; for(int i=0; i() { public int compare(Integer x, Integer y) { if(A[x] != A[y]) return A[y] - A[x]; return y - x; } }); BIT rec = new BIT(N); long inv = 0; for(int i=0; i() { public int compare(Integer x, Integer y) { Integer bx = L[x] / bucket, by = L[y] / bucket; if(bx != by) return bx - by; return R[x] - R[y]; } }); return I; } public static void add(int idx, boolean left) { if(left) { sl.update(A[idx] + ofs, -1); sa.update(0, A[idx], -1); res += sl.prod(A[idx] + 1 + ofs, N + ofs); res += sr.prod(0 + ofs, A[idx] + ofs); } else { sr.update(A[idx] + ofs, -1); sa.update(A[idx] + 1, N, -1); res += sl.prod(A[idx] + 1 + ofs, N + ofs); res += sr.prod(0 + ofs, A[idx] + ofs); } pls = sa.query(0, N); } public static void del(int idx, boolean left) { if(left) { sl.update(A[idx] + ofs, +1); sa.update(0, A[idx], +1); res -= sl.prod(A[idx] + 1 + ofs, N + ofs); res -= sr.prod(0 + ofs, A[idx] + ofs); } else { sr.update(A[idx] + ofs, +1); sa.update(A[idx] + 1, N, +1); res -= sl.prod(A[idx] + 1 + ofs, N + ofs); res -= sr.prod(0 + ofs, A[idx] + ofs); } pls = sa.query(0, N); } public static void operate(int idx, boolean left) { state[idx] ^= true; if(state[idx]) add(idx, left); else del(idx, left); } public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); N = sc.nextInt(); Q = sc.nextInt(); sl = new BIT(N); sr = new BIT(N); sa = new LazySegmentTree(N); state = new boolean[N]; A = new int[N]; for(int i=0; i L[id]) operate(--l, true); while(r < R[id]) operate(r++, false); while(l < L[id]) operate(l++, true); while(r > R[id]) operate(--r, false); ans[id] = inv - res + 1L * (R[id] - L[id]) * pls; } for(int i=0; i 1) { lazy[2*k+1] += lazy[k]; lazy[2*k+2] += lazy[k]; upd[2*k+1] = upd[2*k+2] = true; } lazy[k] = 0; upd[k] = false; } void update(int a, int b, int x, int l, int r, int k) { eval(k, l, r); if(b <= l || r <= a) return; if(a <= l && r <= b) { lazy[k] += x; upd[k] = true; eval(k, l, r); } else { int mid = (l + r) / 2; update(a, b, x, l, mid, 2*k+1); update(a, b, x, mid, r, 2*k+2); node[k] = Math.min(node[2*k+1], node[2*k+2]); } } int query(int a, int b, int l, int r, int k) { if(b <= l || r <= a) return INF; eval(k, l, r); if(a <= l && r <= b) return node[k]; int mid = (l + r) / 2; int vl = query(a, b, l, mid, 2*k+1); int vr = query(a, b, mid, r, 2*k+2); return Math.min(vl, vr); } void update(int a, int b, int x) { update(a, b, x, 0, n, 0); } int query(int a, int b) { return query(a, b, 0, n, 0); } } class BIT { private int[] node; private int N; public BIT(int N_) { node = new int[N_+1]; N = N_; } int prod(int i) { int res = 0; while(i > 0) { res += node[i]; i -= i & -i; } return res; } int prod(int l, int r) { r--; int ret_l = prod(l-1); int ret_r = prod(r); return ret_r - ret_l; } void update(int i, int x) { while(i <= N) { node[i] += x; i += i & -i; } } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} }