2057 The manager's worry(树状数组)
2015-11-03 11:03
441 查看
@(K ACMer)
题意:
单点更新,区间查寻素数个数.
分析:
构造一个树状数组就可以了,定义sum[i]为[1,i]的素数个数.
题意:
单点更新,区间查寻素数个数.
分析:
构造一个树状数组就可以了,定义sum[i]为[1,i]的素数个数.
#include <iostream> #include <cstdio> #include <cstring> #include <set> #include <map> #include <stack> #include <vector> #include <string> #include <queue> #include <cstdlib> #include <cmath> #include <algorithm> using namespace std; const int mod = int(1e9) + 7, INF = 0x3fffffff, maxn = 1e6 + 40; int bit[maxn], n, k, m, a[maxn]; bool isprime(int x) { if (x <= 1) return false; for (int i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return true; } void add(int i, int x) { while (i <= n) { bit[i] += x; i += i & -i; } } int sum(int i) { int sum = 0; while (i > 0) { sum += bit[i]; i -= i & -i; } return sum; } int main(void) { int T = 1; while (scanf("%d%d%d", &n, &k, &m), n * n + k * k + m * m) { printf("CASE #%d:\n", T++); int t = isprime(m) ? 1 : 0; memset(bit, 0, sizeof(bit)); for (int i = 1; i <= n; i++) { a[i] = m; if (t) add(i, 1); } while (k--) { int x, y, z; scanf("%d%d%d", &x, &y, &z); if (!x) { bool f = isprime(a[y]), s = isprime(a[y] + z); a[y] += z; if (f && !s) add(y, -1); if (!f && s) add(y, 1); } else { printf("%d\n", sum(z) - sum(y - 1)); } } puts(""); } return 0; }
相关文章推荐
- 除法求逆元(扩展欧几里德和费马小定理)
- 【翻译】利用加速度求解位置的算法——三轴传感器
- PJSIP 添加视频的思路和想法
- 素数求和问题
- Daily Scrum 11.3
- mysql 5.5 master slave搭建
- spring data jpa 小结
- PHP中的魔术方法总结
- 【iOS】汉字数组,按首字母排序
- Android 自动弹出\关闭软键盘
- heartbeat理论介绍
- 【SPFA】想越狱的小杉
- RS485接口自动换向电路仿真
- codeforces 592 D. Super M
- c# 内存溢出错误
- LeetCode--Maximum Depth of Binary Tree
- mysql innobackupex备份恢复
- 阶乘的0
- CPU核的理解
- Serializable,脱离对DB的束缚