-
Notifications
You must be signed in to change notification settings - Fork 13
/
bit.cpp
executable file
·113 lines (105 loc) · 2 KB
/
bit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <complex>
#pragma comment(linker, "/STACK:266777216")
using namespace std;
#define assert(f) { if(!(f)) { fprintf(stderr,"Assertion failed: "); fprintf(stderr,#f); fprintf(stderr,"\n"); exit(1); } }
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<double> VD;
typedef pair<double,double> PDD;
const int inf=1000000000;
const LL INF=LL(inf)*inf;
const double eps=1e-9;
const double PI=2*acos(0.0);
#define bit(n) (1<<(n))
#define bit64(n) ((LL(1))<<(n))
#define pb push_back
#define sz size()
#define mp make_pair
#define cl clear()
#define all(a) (a).begin(),(a).end()
#define fill(ar,val) memset((ar),(val),sizeof (ar))
#define MIN(a,b) {if((a)>(b)) (a)=(b);}
#define MAX(a,b) {if((a)<(b)) (a)=(b);}
#define sqr(x) ((x)*(x))
#define X first
#define Y second
//clock_t start=clock();
//fprintf(stderr,"time=%.3lfsec\n",0.001*(clock()-start));
#define N 101010
int n;
int a[N],b[N];
int f[N];
void add(int x)
{
// for(;x<=n;x+=x&-x) f[x]++;
for(;x<=n;x+=x&-x) {
// cout<<x<<"x";
f[x]++;
}
// cout<<endl<<"f";
// for(int i =1;i<=n;i++) {
// cout<<f[i]<<" ";
// }
// cout<<endl;
}
int sum(int x)
{
int s=0;
// for(;x>0;x-=x&-x) s+=f[x];
for(;x>0;x-=x&-x) {
s+=f[x];
// cout<<s<<"s"<<x<<"x";
}
// cout<<endl;
return s;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
int T;
for(scanf("%d",&T);T--;)
{
int i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&j);
a[j]=i;
}
fill(f,0);
LL ans=0;
for(i=1;i<=n;i++)
{
scanf("%d",&j);
b[a[j]]=i;
}
for(i=1;i<=n;i++)
{
// cout<<b[i]<<"b[i]";
add(b[i]);
ans+=i-sum(b[i]);
}
printf("%lld\n",ans);
}
return 0;
}