HOJ 1867 经理的烦恼 【 树状数组 】
2015-06-23 09:33
393 查看
题意:给出一个区间,求这个区间里面素数的个数
这道题wa了好多次---是因为add操作没有写对
每次更新的时候,应该先判断没有加上y是不是质数,加上了y是不是质数
如果从质数变成不是质数,那么add(x,-1)
如果从不是质数变成是质数,那么add(x,1)
另外还pe了,,printf("\n")就不对,puts("")就对了
---不懂--------
View Code
这道题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
相关文章推荐
- 有了malloc/free为什么还要new/delete ?
- js的flv视频播放器插件使用方法
- linux命令- 每日一练(1):ls命令
- ViewPaper实现滑屏切换页面及动画效果(仿优酷客户端)
- Android定位功能(一)
- 日经社説 20150623 成長力を大きく高める戦略といえるか
- apkplug使用体验
- Happy Number
- Delphi编程获取系统当前进程、窗口句柄、文件属性以(转)
- 梦断代码阅读笔记之三
- C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解
- 【再思考】PATBasic——1014. 福尔摩斯的约会 (20)
- Docker 1.7.0 深度解析
- 排序算法:堆排序
- 使用MediaRecord录制音频
- Android定位功能(二)
- 日经社説 20150623 日韓は関係改善の努力続けよ
- java,android中的Math用法
- JavaScript random方法得到随机整数(转)
- delphi 动态更改屏幕分辨率(转)