-
Notifications
You must be signed in to change notification settings - Fork 9
/
CNTPRIME - Counting Primes.cpp
111 lines (111 loc) · 2.37 KB
/
CNTPRIME - Counting Primes.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<bits/stdc++.h>
using namespace std;
#define mxx 1000006
#define mx 10004
int prime[mxx] = {0};
void sieve()
{
prime[0] = prime[1] = 1;
for(int i=4; i<mxx; i+=2) prime[i] = 1;
for(long long j=3; j<mxx; j+=2){
if(prime[j]==0){
for(long long i = j*j; i<mxx; i+=j) prime[i] = 1;
}
}
}
int tree[mx*4], lazy[mx*4],arr[mx];
void build(int pos, int b, int e)
{
if(b>e) return;
if(b==e){
tree[pos] = arr[b];
return;
}
int m = (b+e)/2;
int l = pos*2;
int r = l+1;
build(l,b,m);
build(r,m+1,e);
tree[pos] = tree[l] + tree[r];
}
void update(int pos, int b, int e, int i, int j, int p)
{
int m = (b+e)/2;
int l = pos*2;
int r = l+1;
if(lazy[pos]!=-1){
tree[pos] = (e-b+1)*lazy[pos];
if(b!=e){
lazy[l] = lazy[pos];
lazy[r] = lazy[pos];
}
lazy[pos] = -1;
}
if(b>e || b>j || e<i) return;
if(b>=i && e<=j){
tree[pos] = p*(e-b+1);
if(b!=e){
lazy[l] = p;
lazy[r] = p;
}
return;
}
update(l,b,m,i,j,p);
update(r,m+1,e,i,j,p);
tree[pos] = tree[l] + tree[r];
}
int query(int pos, int b, int e, int i, int j)
{
int m = (b+e)/2;
int l = pos*2;
int r = l+1;
if(lazy[pos]!=-1){
tree[pos] = (e-b+1)*lazy[pos];
if(b!=e){
lazy[l] = lazy[pos];
lazy[r] = lazy[pos];
}
lazy[pos] = -1;
}
if(b>e || b>j || e<i) return 0;
if(b>=i && e<=j){
return tree[pos];
}
return query(l,b,m,i,j) + query(r,m+1,e,i,j);
}
int main()
{
sieve();
int tc,n,q,a,b,c,d,cs=1;
scanf("%d",&tc);
while(tc--){
memset(lazy,-1,sizeof lazy);
scanf("%d%d",&n,&q);
for(int i=1; i<=n; i++){
scanf("%d",&a);
arr[i] = !prime[a];
}
build(1,1,n);
printf("Case %d:\n\n",cs++);
while(q--){
scanf("%d",&d);
if(d==0){
scanf("%d%d%d",&a,&b,&c);
update(1,1,n,a,b,!prime[c]);
}
else{
scanf("%d%d",&a,&b);
printf("%d\n\n",query(1,1,n,a,b));
}
}
}
return 0;
}
/*
1
5 3
78 2 13 12 3
1 1 2
0 4 4 5
1 1 5
*/