Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- //Initial template for C++
- #include <bits/stdc++.h>
- using namespace std;
- // } Driver Code Ends
- //User function template for C++
- class Solution{
- public:
- 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[] so that we can do binary search in it
- sort(Y, Y + n);
- long long total_pairs = 0; // Initialize result
- // Take every element of X and count pairs with it
- for (int i = 0; i < m; i++)
- total_pairs += count(X[i], Y, n, NoOfY);
- return total_pairs;
- // code here
- }
- };
- //{ Driver Code Starts.
- int main() {
- int t;
- cin >> t;
- while (t--) {
- int m, n;
- cin >> m >> n;
- int X[m], Y[n];
- for (int i = 0; i < m; i++) {
- cin >> X[i];
- }
- for (int i = 0; i < n; i++) {
- cin >> Y[i];
- }
- Solution ob;
- auto ans = ob.countPairs(X, Y, m, n);
- cout << ans << "\n";
- }
- return 0;
- }
- // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment