#include<bits/stdc++.h> #define MAX_N 50000 usingnamespace std; constint MAGIC = 230; 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, q, a[MAX_N+5], b[MAX_N+5], c[MAX_N+5], tr[MAX_N+5]; structquery {int l, r, lb, rb, id;} Q[MAX_N+5]; int ans[MAX_N+5]; boolcmpn(constint &x, constint &y){return c[x] < c[y];} boolcmpq(const query &x, const query &y){return x.lb == y.lb ? x.rb < y.rb : x.lb < y.lb;} voidinc(int p){for (; p <= m; p += (p&-p)) tr[p]++;} voiddec(int p){for (; p <= m; p += (p&-p)) tr[p]--;} intsum(int p){int s = 0; for (; p; p -= (p&-p)) s += tr[p]; return s;} intget_bef(int p){returnsum(p-1);} intget_aft(int p){returnsum(m)-sum(p);} intmain(){ read(n); for (int i = 1; i <= n; i++) read(c[i]), b[i] = i; sort(b+1, b+n+1, cmpn); for (int i = 1; i <= n; a[b[i++]] = m) if (c[b[i]]^c[b[i-1]]) m++; read(q); for (int i = 1; i <= q; i++) read(Q[i].l), read(Q[i].r), Q[i].id = i, Q[i].lb = Q[i].l/MAGIC, Q[i].rb = Q[i].r/MAGIC; sort(Q+1, Q+q+1, cmpq); int tot = 0; for (int i = 1, l = 1, r = 0; i <= q; i++) { for (; l > Q[i].l; l--) tot += get_bef(a[l-1]), inc(a[l-1]); for (; r < Q[i].r; r++) tot += get_aft(a[r+1]), inc(a[r+1]); for (; l < Q[i].l; l++) tot -= get_bef(a[l]), dec(a[l]); for (; r > Q[i].r; r--) tot -= get_aft(a[r]), dec(a[r]); ans[Q[i].id] = tot; } for (int i = 1; i <= q; i++) printf("%d\n", ans[i]); return0; }