1 #include <iostream>
2 #include <stdio.h>
3 #include <string.h>
4 #include <math.h>
5 #include <algorithm>
6 using namespace std;
7 #define ll long long
8 int b[60][100100],a[100100],n,c[60];
9 int lowbit(int x)
10 {
11 return x&(-x);
12 }
13 void update1(int x)
14 {
15 while(x<60)
16 {
17 c[x]++;
18 x+=lowbit(x);
19 }
20 }
21 int query1(int x)
22 {
23 int sum=0;
24 while(x>0)
25 {
26 sum+=c[x];
27 x-=lowbit(x);
28 }
29 return sum;
30 }
31 void update(int x,int y,int z)
32 {
33 for(int i=x; i<=50; i+=lowbit(i))
34 for(int j=y; j<=n; j+=lowbit(j))
35 b[i][j]+=z;
36 }
37 int query(int x,int y,int x1,int y1)
38 {
39 int ans=0,i,j;
40 for(i=y;i>0;i-=lowbit(i))
41 for(j=y1;j>0;j-=lowbit(j))
42 ans+=b[i][j];
43
44 for(i=x-1;i>0;i-=lowbit(i))
45 for(j=y1;j>0;j-=lowbit(j))
46 ans-=b[i][j];
47
48 for(i=y;i>0;i-=lowbit(i))
49 for(j=x1-1;j>0;j-=lowbit(j))
50 ans-=b[i][j];
51
52 for(i=x-1;i>0;i-=lowbit(i))
53 for(j=x1-1;j>0;j-=lowbit(j))
54 ans+=b[i][j];
55 return ans;
56 }
57 int main()
58 {
59 int i,m,x,y;
60 ll ans;
61 while(~scanf("%d",&n))
62 {
63 ans=0;
64 memset(b,0,sizeof(b));
65 memset(c,0,sizeof(c));
66 for(i=1; i<=n; i++)
67 {
68 scanf("%d",&a[i]);
69 update1(a[i]);
70 ans+=i-query1(a[i]);
71 update(a[i],i,1);
72 }
73 printf("%lld\n",ans);
74 scanf("%d",&m);
75 for(i=0; i<m; i++)
76 {
77 scanf("%d%d",&x,&y);
78 if(x>y)swap(x,y);
79 ans-=query(a[y]+1,50,x+1,y);
80 ans+=query(1,a[y]-1,x+1,y);
81 ans+=query(a[x]+1,50,x,y-1);
82 ans-=query(1,a[x]-1,x,y-1);
83 if(a[x]>a[y])ans--;
84 else if(a[x]<a[y])ans++;
85 update(a[x],x,-1);
86 update(a[x],y,1);
87 update(a[y],y,-1);
88 update(a[y],x,1);
89 swap(a[x],a[y]);
90 printf("%lld\n",ans);
91 }
92 }
93 }