1 #include<cstdio>
2 #include<cstdlib>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<iostream>
7 #include<vector>
8 #include<map>
9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 50000+5
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll int
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define mod 1000000007
23 using namespace std;
24 inline int read()
25 {
26 int x=0,f=1;char ch=getchar();
27 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
28 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
29 return x*f;
30 }
31 struct rec{int l,r,id;}c[maxn];
32 ll n,m,cnt,tot,a[maxn],b[maxn],s[maxn],v[maxn],ans[maxn];
33 inline bool cmp(rec x,rec y){return b[x.l]==b[y.l]?x.r<y.r:x.l<y.l;}
34 inline bool cmp1(int x,int y){return a[x]<a[y];}
35 inline ll sum(int x){ll t=0;for(;x;x-=x&(-x))t+=s[x];return t;}
36 inline void add(int x,int y){for(;x<=tot;x+=x&(-x))s[x]+=y;}
37 int main()
38 {
39 freopen("input.txt","r",stdin);
40 freopen("output.txt","w",stdout);
41 n=read();
42 for1(i,n)a[i]=read(),b[i]=i;
43 sort(b+1,b+n+1,cmp1);
44 for1(i,n)
45 {
46 if(i==1||(a[b[i]]!=a[b[i-1]]))tot++;
47 v[b[i]]=tot;
48 }
49 m=read();int block=sqrt(n);
50 for1(i,n)b[i]=(i-1)/block+1;
51 for1(i,m)c[i].l=read(),c[i].r=read(),c[i].id=i;
52 sort(c+1,c+m+1,cmp);
53 int l=1,r=0;
54 for1(i,m)
55 {
56 while(r<c[i].r)cnt+=sum(tot)-sum(v[++r]),add(v[r],1);
57 while(r>c[i].r)cnt-=sum(tot)-sum(v[r]),add(v[r--],-1);
58 while(l<c[i].l)cnt-=sum(v[l]-1),add(v[l++],-1);
59 while(l>c[i].l)cnt+=sum(v[--l]-1),add(v[l],1);
60 ans[c[i].id]=cnt;
61 }
62 for1(i,m)printf("%d\n",ans[i]);
63 return 0;
64 }