Given integer sequences

Given three integer sequences A=(a1,a2...ana) ,B=(b1,b2...bnb) , C=(c1,c2...cnc)
Find the numbers of tuple (i,j,k) such that ai>bj and bj<ck

The first line contains three integers na,nb ,nc .
The next three lines contains sequence A , B,C .


Below is my code, it passes the first few test cases but shows wrong answer at the last two test cases. Not sure where has been wrong.

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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int na,nb,nc;
    cin >> na>>nb>>nc;
    
    vector<uint64_t> A(na);
    for (auto& A_i : A) { cin >> A_i; }
    
    vector<uint64_t> B(nb);
    for (auto& B_i : B) { cin >> B_i; }
    
    vector<uint64_t> C(nc);
    for (auto& C_i : C) { cin >> C_i; }
    
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    sort(C.begin(), C.end());
    vector<uint64_t> v1(A.size() + B.size()+1);
    vector<uint64_t> v2(v1.size() + C.size()+1);
    
    merge(A.begin(), A.end(), B.begin(), B.end(), v1.begin());
    merge(v1.begin(), v1.end(), C.begin(), C.end(), v2.begin());
    
    int cnt1,cnt2,sum=0;
    
    for(int i=0;i<B.size();i++)
    {
        vector<uint64_t>::iterator a=upper_bound(A.begin(),A.end(),B[i]);
          cnt1=distance(a,A.end());
        
        vector<uint64_t>::iterator c=upper_bound(C.begin(),C.end(),B[i]);
         cnt2=distance(c,C.end());
        
         sum+=cnt1*cnt2;
    }

    cout<<sum;
    return 0;
}
Last edited on
No idea why your answers would be wrong, but you could vastly improve the run time.

- no idea why you create vectors v1 and v2 - you don't use them;
- you have sorted all arrays; so
--- you don't have to search all of A and C for each B[i]: just where you left off last time;
--- you can stop looking once either cnt1 or cnt2 is 0
- is there any reason why you are using uint64_t? and, if so, are you sure that sum shouldn't also be of this type.
Last edited on
Does this work? (You've removed your model answer, BTW. That is a tad unhelpful. I don't believe you are giving the whole question, either.)

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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

using INT = unsigned long long;

int main()
{
   INT na, nb, nc;
   cin >> na >> nb >> nc;
    
   vector<INT> A(na), B(nb), C(nc);
   for ( auto &e : A ) cin >> e;
   for ( auto &e : B ) cin >> e;
   for ( auto &e : C ) cin >> e;
    
   sort( A.begin(), A.end() );
   sort( B.begin(), B.end() );
   sort( C.begin(), C.end() );

   INT sum = 0;
   auto a = A.begin(), c = C.begin();
   for ( auto e : B )
   {
      a = upper_bound( a, A.end(), e );   if ( a == A.end() ) break;
      c = upper_bound( c, C.end(), e );   if ( c == C.end() ) break;
      sum += distance( a, A.end() ) * distance( c, C.end() );
   }

   cout << sum;
}

Last edited on
Yes, it worked. Turned out the data type should be uint64_t to hold the test cases. Thanks a lot
Topic archived. No new replies allowed.