-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoOfPairs.cpp
More file actions
61 lines (45 loc) · 935 Bytes
/
noOfPairs.cpp
File metadata and controls
61 lines (45 loc) · 935 Bytes
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
#include<bits/stdc++.h>
using namespace std;
int count(int x, int Y[], int n, int NoOfY[])
{
if (x == 0) return 0;
if (x == 1) return NoOfY[0];
int* idx = upper_bound(Y, Y + n, x);
int ans = (Y + n) - idx;
ans += (NoOfY[0] + NoOfY[1]);
if (x == 2) ans -= (NoOfY[3] + NoOfY[4]);
if (x == 3) ans += NoOfY[2];
return ans;
}
long long countPairs(int X[], int Y[], int m, int n)
{
int NoOfY[5] = {0};
for (int i = 0; i < n; i++)
if (Y[i] < 5)
NoOfY[Y[i]]++;
sort(Y, Y + n);
long long totalPairs = 0;
for (int i=0; i<m; i++)
totalPairs += count(X[i], Y, n, NoOfY);
return totalPairs;
}
int main()
{
int test;
cin>>test;
while(test--)
{
int m,n;
cin>>m>>n;
int i,a[m],b[n];
for(i=0;i<m;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
cin>>b[i];
}
cout<<countPairs(a, b, m, n)<<"\n";
}
}