Prime number segmented sieve
You are computing your prime sieve multiple times, when you only need to compute it once.  You are told you will have at most 10 test cases.  You could easily store those in an array, compute the maximum upper limit, and run the sieve once.  Then loop through the stored test cases, and generate the output for each.  Could be up to a 10x speed up.
The only even prime is 2.  Treat it as a special case.  Then, when you are running the sieve, you can start with 3 and increment with p += 2 to skip the even numbers.
Since the sieve has already eliminated all multiples of smaller prime numbers, when eliminating the multiples of p, you don’t have to start at p*2; rather you can start at p*p and skip the even multiples using i += p*2.  (Consider storing p2 = p*2 to avoid the multiplication every loop iteration!)
In segmentedsieve(), if the upper limit is less than largest prime your sieve produced, you don’t need to generate the second sieve; just emit the primes in the correct range.
You can apply points #2 &amp; #3 to the secondary sieve, but it will be slightly more complicated as you need to ensure you’ve calculated an odd starting multiple.
To get a 8x reduction in memory usage, use a bit array.  (You could get a 16x reduction, by storing flags for only odd values in the bit array, but your indexing will become more complex.)