Segmented Sieve Spoj I tried(trying...) to solve the SPOJ prime number problem PRIME1 so i learned and implemented Segmented Sieve correctly but this program works in time for 10^8 but getting Time limit exceeded (TLE) for 10^9 input integer. can someone help me reduce complexity within my code please. 

/*
 followed this tutorial
 https://medium.com/@agilanbtdw/prime-number-generation-in-java-using-segmented-sieve-of-eratosthenes-187af1dcd051
*/
#include &lt;iostream&gt;
#include &lt;math.h&gt;
#include &lt;vector&gt;
#include &lt;fstream&gt;
using std::cin;
using std::ifstream;
using std::vector;
using std::cout;
using std::ios_base;
int main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 
    //ifstream in("inp.txt");
  short int t;
  unsigned long int n ,q;
  cin &gt;&gt; t; 
  while(t--){
  cin &gt;&gt;q &gt;&gt; n;
  vector&lt;bool&gt; a(n,true);
  unsigned long int m = sqrt(n); 
  a[0] = false;
  a[1] = false;
  // find primes till sqrt
  for(unsigned long int i = 2 ; i &lt;= m ; i++ ){
    if(a[i]){
      for(unsigned long int j = i*i ; j &lt;= m ; j+=i ){
        a[j] = false;
      }
    }
  }
//  store the primes upto m
  vector&lt;unsigned long int&gt; primes;
  for(unsigned long int i = 2 ; i &lt;= m ; i++ ){
      if(a[i]){ primes.push_back(i);}
  }

  unsigned long int temp;
  for (unsigned long int x : primes) {
        temp = q/x;
        temp*=x;
                // from primes arrays increment each prime with that num , set the index as false
        for(unsigned long int y = temp ; y &lt;=n ; y+=x){
        a[y] = false;
        }
  }
// set the falsed indexes in previous primes arrays to true
  for(unsigned long int i = 0 ; i &lt; primes.size() ;++i){
    a[primes[i]] = true;
  }
  for(unsigned long int i =q ; i &lt;= n ; i++ ){
    if(a[i]) {cout &lt;&lt; i &lt;&lt; " ";}
  }
  cout &lt;&lt; "\n";
  }
  return 0;
}



  my repl link - https://repl.it/repls/UnrulyMotherlyTransformation
Reducing Complexity
The first thing to do for reduced complexity is to break up the code into multiple functions. This would allow you to profile the code and see where it spends the most time. This also makes it easier to read, write and debug the code. There are possibly 5 functions in main. As programs get more complex it is generally better to limit the main() function to set up and clean up and do all other processing in sub functions.

This would be applying the Single Responsibility Principle to the code which is the first pillar of SOLID object oriented programming. The Single Responsibility Principle states The single responsibility principle is a computer programming principle that states that every module, class, or function[1] should have responsibility over a single part of the functionality provided by the software ...

Variable Names
Generally single character variable names make code harder to read and debug. It is unclear what the variables t, n, q or m are or do.

Use Explict Casts
The line of code 

unsigned long int m = sqrt(n);


is an implicit cast and generates warning messages in some compilers. C++ attempts to be type safe and casts should be explict, in this particular case it would be better to use a static_case.

Performance
It looks like this code checks all numbers in a range, there is no reason to process even numbers except for 2 since all even numbers greater than 2 can't be prime numbers because they are divisible by 2.

Indentation
The code is improperly indented immediately after the while statement. This makes it much harder to read the code.