#include<bits/stdc++.h> #define MAX_N 20000 #define MAGIC ((int)sqrt(n)) usingnamespace std; template <classT> inlinevoidread(T &x){ x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } int n, m, cnt, tot, a[MAX_N+5], b[MAX_N+5]; int BIT[150][MAX_N+5]; map <int, int> h; intid(int p){return (p-1)/MAGIC+1;} voidinc(int tr[], int p){for (; p <= n; p += (p&-p)) tr[p]++;} voiddec(int tr[], int p){for (; p <= n; p += (p&-p)) tr[p]--;} intsum(int tr[], int p){int ret = 0; for (; p; p -= (p&-p)) ret += tr[p]; return ret;} intmain(){ read(n); for (int i = 1; i <= n; i++) read(a[i]), b[i] = a[i]; sort(b+1, b+n+1); for (int i = 1; i <= n; i++) if (!i || (b[i]^b[i-1])) h[b[i]] = ++cnt; for (int i = 1; i <= n; i++) inc(BIT[0], a[i] = h[a[i]]), tot += i-sum(BIT[0], a[i]); for (int i = 1; i <= n; i++) inc(BIT[id(i)], a[i]); printf("%d\n", tot); read(m); while (m--) { int x, y; read(x), read(y); if (x > y) swap(x, y); if (id(x) < id(y)) { for (int i = id(x)+1; i <= id(y)-1; i++) tot -= sum(BIT[i], a[x]-1), tot += sum(BIT[i], n)-sum(BIT[i], a[x]), tot += sum(BIT[i], a[y]-1), tot -= sum(BIT[i], n)-sum(BIT[i], a[y]); for (int i = x+1; i <= id(x)*MAGIC; i++) tot -= (a[i] < a[x]), tot += (a[i] > a[x]), tot += (a[i] < a[y]), tot -= (a[i] > a[y]); for (int i = y-1; i >= (id(y)-1)*MAGIC+1; i--) tot -= (a[i] < a[x]), tot += (a[i] > a[x]), tot += (a[i] < a[y]), tot -= (a[i] > a[y]); } else for (int i = x+1; i <= y-1; i++) tot -= (a[i] < a[x]), tot += (a[i] > a[x]), tot += (a[i] < a[y]), tot -= (a[i] > a[y]); tot += (a[x] < a[y]), tot -= (a[x] > a[y]); dec(BIT[id(x)], a[x]), dec(BIT[id(y)], a[y]); inc(BIT[id(x)], a[y]), inc(BIT[id(y)], a[x]); swap(a[x], a[y]), printf("%d\n", tot); } return0; }