1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 #include<cstring>
5 #include<cmath>
6 #include<cstdlib>
7 #include<vector>
8 using namespace std;
9 typedef long long ll;
10 typedef long double ld;
11 typedef pair<int,int> pr;
12 const double pi=acos(-1);
13 #define rep(i,a,n) for(int i=a;i<=n;i++)
14 #define per(i,n,a) for(int i=n;i>=a;i--)
15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
16 #define clr(a) memset(a,0,sizeof(a))
17 #define pb push_back
18 #define mp make_pair
19 #define fi first
20 #define sc second
21 #define pq priority_queue
22 #define pqb priority_queue <int, vector<int>, less<int> >
23 #define pqs priority_queue <int, vector<int>, greater<int> >
24 #define vec vector
25 ld eps=1e-9;
26 ll pp=1000000007;
27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
32 ll read(){ ll ans=0; char last=‘ ‘,ch=getchar();
33 while(ch<‘0‘ || ch>‘9‘)last=ch,ch=getchar();
34 while(ch>=‘0‘ && ch<=‘9‘)ans=ans*10+ch-‘0‘,ch=getchar();
35 if(last==‘-‘)ans=-ans; return ans;
36 }
37 #define N 50005
38 #define lowbit(i) (i&(-i))
39 int n,num,ans,T[N],pos[N];
40 struct node_{
41 int i,id;
42 }nu[N];
43 struct node{
44 int l,r,id,ans;
45 }a[N];
46 int cmp_nu(node_ a,node_ b){
47 return a.i<b.i;
48 }
49 int cmp_nu_(node_ a,node_ b){
50 return a.id<b.id;
51 }
52 int cmp (node a,node b){
53 return pos[a.l]<pos[b.l]||pos[a.l]==pos[b.l]&&a.r<b.r;
54 }
55 int cmp_(node a,node b){
56 return a.id<b.id;
57 }
58 void add(int x,int v){
59 num=v>0?num+1:num-1;
60 for (int i=x;i<=n;i+=lowbit(i)) T[i]+=v;
61 }
62 int sum(int x){
63 int Sum=0;
64 for (int i=x;i>0;i-=lowbit(i)) Sum+=T[i];
65 return Sum;
66 }
67 void Update(int x,int v,int f){
68 ans+=(f?(num-sum(nu[x].i)):(sum(nu[x].i-1)))*v; add(nu[x].i,v);
69 }
70 int main()
71 {
72 n=read();
73 for (int i=1;i<=n;i++) nu[i].i=read(),nu[i].id=i;
74 sort(nu+1,nu+n+1,cmp_nu);
75 for (int i=1,e=0;i<=n;i++)
76 if (nu[i].i!=nu[i-1].i) nu[i].i=++e;
77 else nu[i].i=e;
78 sort(nu+1,nu+n+1,cmp_nu_);
79 int q=read();
80 for (int i=1;i<=q;i++) a[i].l=read(),a[i].r=read(),a[i].id=i;
81 int block=(int)(sqrt(n));
82 for (int i=1;i<=n;i++)
83 pos[i]=(i-1)/block+1;
84 sort(a+1,a+q+1,cmp);
85 for (int i=1,l=1,r=0;i<=q;i++){
86 for (;r<a[i].r;r++) Update(r+1,1,1);
87 for (;r>a[i].r;r--) Update(r,-1,1);
88 for (;l>a[i].l;l--) Update(l-1,1,0);
89 for (;l<a[i].l;l++) Update(l,-1,0);
90 a[i].ans=ans;
91 }
92 sort(a+1,a+q+1,cmp_);
93 for (int i=1;i<=q;i++)
94 printf("%d\n",a[i].ans);
95 return 0;
96 }