CNTPRIME - Counting Primes

Tortoise and Achilles are playing the Counting the Primes game. Achilles will give Tortoise some numbers, and some intervals, and then Tortoise needs to count the primes on those intervals. It is an easy game, but Tortoise is doing the counting slowly. Achilles is pissed off, so he has given you the task as you are a good programmer. For a twist, he has changed the game a little bit, that is he will give some intervals for counting the prime as well as he will give some intervals to change the numbers in that interval.

You are given an array of n elements. After that you will be given M commands. They are -

  • 0 x y v - you have to change all numbers in the range of x to y (inclusive) to v, where x and y are two indexes of the array.
  • 1 x y - output a line containing a single integer which is the number of primes between x and y (inclusive).

The array is indexed from 1 to n.


Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case contains two integers n (1 ≤ n ≤ 104) and q (1 ≤ q ≤ 2*104). Then next line, you will be given N integers. After that each of the next q lines will contain a task in one of the following form:

  • 0 x y v (1 ≤ x ≤ y ≤ n, 2 ≤ v ≤ 106)
  • 1 x y (1 ≤ x ≤ y ≤ n)

And the numbers will be in range of [2, 106].


For each case, print the case number first. Then for each query '1 x y', print the number of primes between x and y [inclusively].

Sample Input

Output for Sample Input


5 3

78 2 13 12 3

1 1 2

0 4 4 5

1 1 5

Case 1:




  • Use Faster IO like scanf,printf
  • A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first prime numbers are 2,3,5,7,11....2,3,5,7,11,


hide comments
faraday_vij: 2020-06-24 07:33:46

after each line you have to print one endl just do cout<<5<< endl; no need of cout<<5<<endl;cout<<endl;
Spoj please take care of the input output routine plz..

killer_knight: 2020-03-29 16:51:21

@Sigma Kappa you have to print only 1 '\n' after printing each line..
make sure that you initialize your lazy[] array with any other non zero value(say -1)..

Sigma Kappa: 2018-06-12 00:15:57

So, do we do "printf("Case %d:\n\n",++cs)" or "printf("Case %d:\n",++cs)"? For each query, do we print "\n\n" or just a single "\n"?

kaushalag29: 2018-06-11 19:10:02

cin and cout with ios_base leads to TLE.Use scanf and printf.

lamia2658: 2018-05-31 21:17:32

lazy segtree + sieve = AC

sam28: 2018-04-02 15:40:00

Take care, got TLE using bitset for primes

sherlock11: 2018-03-29 20:21:47

don't forget "Case :" while printing the answer!!!

losmi247: 2018-03-29 19:22:54

Precalc the primes,then lazy segtree, don't forget about fast I/O

shiv2111: 2018-01-14 17:04:37

cakewalk st+lazy

siddharth_0196: 2017-06-10 13:13:48

Changing array size from 10^5 to 4*10^4 changed TLE to AC! -_-

Added by:Faiyaz
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Own Problem