Prime number segmented sieve I tried to solve the SPOJ prime number problem below. The code produced correct results, but Time limit exceeded and also memory need around 970M which is not efficient(?)

How can I optimize this code under a time constraint of around few seconds?


  Problem
  
  Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!
  
  Input
  
  The input begins with the number t of test cases in a single line (t&lt;=10). In each of the next t lines there are two numbers m and n (1 &lt;= m &lt;= n &lt;= 1000000000, n-m&lt;=100000) separated by a space.
  
  Output
  
  For every test case print all prime numbers p such that m &lt;= p &lt;= n, one number per line, test cases separated by an empty line.


#include&lt;string.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;stdio.h&gt;
#include &lt;cmath&gt;
#include &lt;vector&gt;
#include&lt;iostream&gt;
using namespace std;

void simplesieve(int limit, vector&lt;int&gt;&amp;prime);


void segmentedsieve() {
    int T, M, K;

    cin &gt;&gt; T;


    for (int t = 0; t &lt; T; t++) {
        cin &gt;&gt; M &gt;&gt; K;
        const int limit = floor(sqrt(K)) + 1;
        vector&lt;int&gt; prime;
        simplesieve(K, prime);

        bool *mark = new bool[K - M];
        memset(mark, true, sizeof(bool)*(K-M));

        vector&lt;int&gt;::iterator it;

        for (it = prime.begin(); *it&lt;limit; it++) {

            int lolim = floor(M / (*it))*(*it);
            if (lolim &lt; M)
                lolim = lolim + (*it);

            for (int j = lolim; j &lt; K; j += (*it))
            {   if(j!=*it)
                mark[j - M] = false;
            }


        }

        for (int i = M; i &lt; K; i++) {
            if (mark[i - M] == true)
                cout &lt;&lt; i &lt;&lt; "\n";
        }

        delete[] mark;
    }


}



void simplesieve( int limit, vector&lt;int&gt;&amp;prime) {

    bool *mark=new bool[limit + 1];

    memset(mark, true, sizeof(bool)*(limit+1));
    for (int p = 2; p*p &lt; limit; p++) {
        if (mark[p] == true) {
            for (int i = p * 2; i &lt; limit; i += p)
                mark[i] = false;
        }
    }
    for (int p = 2; p &lt; limit; p++) {
        if (mark[p] == true)
        {
            prime.push_back(p);

        }

    }

}


int main()
{
    int n = 100;
    segmentedsieve();
    return 0;
}
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.)