您的位置:首页 > 其它

HOJ 1867 经理的烦恼 【 树状数组 】

2015-06-23 09:33 393 查看
题意:给出一个区间,求这个区间里面素数的个数

这道题wa了好多次---是因为add操作没有写对

每次更新的时候,应该先判断没有加上y是不是质数,加上了y是不是质数

如果从质数变成不是质数,那么add(x,-1)

如果从不是质数变成是质数,那么add(x,1)

另外还pe了,,printf("\n")就不对,puts("")就对了

---不懂--------

#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std;

typedef long long LL;
const int INF = (1<<30)-1;
const int mod=1000000007;
const int maxn=1000005;

int a[maxn];
int c[maxn];//树状数组
int C,N,M;

int is(int x){
if( x <= 1) return 0;
if(x == 2) return 1;
int tmp = sqrt(x);
for(int i=2;i<=tmp;i++)
if(x % i == 0) return 0;
return 1;
}

int lowbit(int x){ return x & (-x);}

int sum(int x){
int ret=0;
while(x>0){
ret+=c[x];x-=lowbit(x);
}
return ret;
}

void add(int x,int d){
while(x <= maxn){
c[x]+=d; x+=lowbit(x);
}
}

int main(){
int kase=0;
while(scanf("%d %d %d",&C,&N,&M) != EOF){
if( C == 0 && N ==0 && M == 0) break;
memset(c,0,sizeof(c));
memset(a,0,sizeof(a));

int x = is(M);
for(int i=1;i<=maxn;i++) a[i] = M,add(i,x);

printf("CASE #%d:\n",++kase);
int cmd;
while(N--){
scanf("%d",&cmd);
if(cmd == 0){
int x,y;
scanf("%d %d",&x,&y);
int tmp = a[x];
a[x] += y;

//    printf("tmp = %d  a[%d] = %d\n",tmp,x,a[x]);
if(is(a[x])){
if(!is(tmp)) add(x,1);
}
else {
if(is(tmp)) add(x,-1);
}
}
if(cmd == 1){
int l,r;
scanf("%d %d",&l,&r);
printf("%d\n",sum(r) - sum(l-1));
}
}
puts("");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: