题意:给出一个区间,求这个区间里面素数的个数
这道题wa了好多次---是因为add操作没有写对
每次更新的时候,应该先判断没有加上y是不是质数,加上了y是不是质数
如果从质数变成不是质数,那么add(x,-1)
如果从不是质数变成是质数,那么add(x,1)
另外还pe了,,printf(" ")就不对,puts("")就对了
---不懂--------
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<algorithm> 11 using namespace std; 12 13 typedef long long LL; 14 const int INF = (1<<30)-1; 15 const int mod=1000000007; 16 const int maxn=1000005; 17 18 int a[maxn]; 19 int c[maxn];//树状数组 20 int C,N,M; 21 22 int is(int x){ 23 if( x <= 1) return 0; 24 if(x == 2) return 1; 25 int tmp = sqrt(x); 26 for(int i=2;i<=tmp;i++) 27 if(x % i == 0) return 0; 28 return 1; 29 } 30 31 int lowbit(int x){ return x & (-x);} 32 33 int sum(int x){ 34 int ret=0; 35 while(x>0){ 36 ret+=c[x];x-=lowbit(x); 37 } 38 return ret; 39 } 40 41 void add(int x,int d){ 42 while(x <= maxn){ 43 c[x]+=d; x+=lowbit(x); 44 } 45 } 46 47 int main(){ 48 int kase=0; 49 while(scanf("%d %d %d",&C,&N,&M) != EOF){ 50 if( C == 0 && N ==0 && M == 0) break; 51 memset(c,0,sizeof(c)); 52 memset(a,0,sizeof(a)); 53 54 int x = is(M); 55 for(int i=1;i<=maxn;i++) a[i] = M,add(i,x); 56 57 printf("CASE #%d: ",++kase); 58 int cmd; 59 while(N--){ 60 scanf("%d",&cmd); 61 if(cmd == 0){ 62 int x,y; 63 scanf("%d %d",&x,&y); 64 int tmp = a[x]; 65 a[x] += y; 66 67 // printf("tmp = %d a[%d] = %d ",tmp,x,a[x]); 68 if(is(a[x])){ 69 if(!is(tmp)) add(x,1); 70 } 71 else { 72 if(is(tmp)) add(x,-1); 73 } 74 } 75 if(cmd == 1){ 76 int l,r; 77 scanf("%d %d",&l,&r); 78 printf("%d ",sum(r) - sum(l-1)); 79 } 80 } 81 puts(""); 82 } 83 return 0; 84 }