ZOJ 3886 Nico number(线段树)
来源:互联网 发布:2am 2pm 知乎 编辑:程序博客网 时间:2024/04/18 21:35
Kousaka Honoka and Minami Kotori are playing a game about a secret of Yazawa Nico.
When the game starts, Kousaka Honoka will give Minami Kotori an array A of N non-negative integers. There is a special kind of number in the array, which is calledNicoNico-number. We call a integer x is a NicoNico-number, if all integers(no more than x) that is coprime with x could form an Arithmetic Sequence.Then Minami Kotori will choose some consecutive part of the array A, wondering the number of NicoNico-number in this part. What's more, Kousaka Honoka sometimes modify the value of some consecutive elements in the array. Now it is your task to simulate this game!Input
There are multiple test cases in input, you should process to the end of file.
For each case, the first line is an integer N, the number of elements in the array described above. Then second line contains N integers no greater than 107, the elements of the array initially.(1 <= N <= 100,000)
The third line is a integer T, the number of the operations of the game. Each line of the following T lines is in one of the following formats.(1 <= T <= 100,000)
"1 L R" : Minami Kotori will chooses the consecutive part of the array from the Lth to Rth element inclusive. (1 <= L <= R <= N)
"2 L R v" : Kousaka Honoka will change the value of the pth element A[p] in the array to A[p]%v for all L <= p <= R.(1 <= L <= R <= N, 1 <= v <= 107)
"3 p x" : Kousaka Honoka will change the value of the p th element A[p] to x.(1 <= p <= N, 1 <= x <= 107)
Output
Each time when Minami Kotori chooses some part of the array, you should output a line, the number of NicoNico-number in that part.
Sample Input
34 6 961 1 31 3 32 1 1 101 1 33 2 41 1 3
Sample Output
2022
Hint
4 is a NicoNico-number because only 1 and 3 is coprime with 4 among the integers no greater than 4, and could form an Arithmetic Sequence {1,3}.
解题思路:只有6,2的整数次幂和质数满足要求,用线段树维护,区间取模的地方维护一个区间最大值,若区间最值小于mod则直接返回,不然暴力取模。
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>#include <queue>#include <set>#include <stack>#include <algorithm>#define LL long longusing namespace std;const int MAXN = 100000 + 10;const int MAXM = 10000000 + 10;int vis[MAXM], isprime[MAXM], prime[MAXM];void init(){ int m = (int)sqrt(MAXM + 0.5); for(int i=2;i<=m;i++) if(!vis[i]) { for(int j=i*i;j<=MAXM;j+=i) vis[j] = 1; } vis[6] = vis[0] = 0; for(int i=1;i<=MAXM;i<<=1) vis[i] = 0;}struct Tree{ int l, r; int Max, sum;}tree[MAXN<<2];void push_up(int rt){ tree[rt].Max = max(tree[rt<<1].Max, tree[rt<<1|1].Max); tree[rt].sum = tree[rt<<1].sum + tree[rt<<1|1].sum;}void build(int l, int r, int rt){ tree[rt].l = l; tree[rt].r = r; tree[rt].Max = 0; tree[rt].sum = 0; if(l == r) { scanf("%d", &tree[rt].Max); tree[rt].sum = 1 - vis[tree[rt].Max]; return ; } int mid = (l + r) >> 1; build(l, mid, rt<<1); build(mid + 1, r, rt<<1|1); push_up(rt);}void update1(int p, int x, int rt){ if(tree[rt].l == tree[rt].r) { tree[rt].Max = x; tree[rt].sum = 1 - vis[x]; return ; } int mid = (tree[rt].l + tree[rt].r) >> 1; if(p <= mid) update1(p, x, rt<<1); else update1(p, x, rt<<1|1); push_up(rt);}void update2(int l, int r, int x, int rt){ if(tree[rt].l >= l && tree[rt].r <= r) { if(tree[rt].Max < x) return ; } if(tree[rt].l == tree[rt].r) { tree[rt].Max %= x; tree[rt].sum = 1 - vis[tree[rt].Max]; return ; } int mid = (tree[rt].l + tree[rt].r) >> 1; if(l <= mid) update2(l, r, x, rt<<1); if(r > mid) update2(l, r, x, rt<<1|1); push_up(rt);}int query(int l, int r, int rt){ //cout << l << ' ' << r << ' ' << tree[rt].l << ' ' << tree[rt].r << endl; if(tree[rt].l >= l && tree[rt].r <= r) { return tree[rt].sum; } int mid = (tree[rt].l + tree[rt].r) >> 1; int ans = 0; if(l <= mid) ans += query(l, r, rt<<1); if(r > mid) ans += query(l, r, rt<<1|1); return ans;}int main(){ init(); int n, Q; while(scanf("%d", &n)!=EOF) { build(1, n, 1); scanf("%d", &Q); int op, l, r; while(Q--) { scanf("%d", &op); if(op == 1) { scanf("%d%d", &l, &r); printf("%d\n", query(l, r, 1)); } else if(op == 2) { int x; scanf("%d%d%d", &l, &r, &x); update2(l, r, x, 1); } else if(op == 3) { int p, x; scanf("%d%d", &p, &x); update1(p, x, 1); } } } return 0;}
- ZOJ 3886 Nico number(线段树)
- ZOJ 3886 Nico Number (线段树)
- 【ZOJ】3886 Nico Number【线段树】
- ZOJ 3886 Nico Number(素数筛选 + 线段树)
- zoj 3886 Nico Number(线段树,区间取模操作)
- ZOJ 3886 Nico Number(线段树单点更新+找规律)
- ZOJ 3886 Nico Number
- zoj3886--Nico Number(素数筛+线段树)
- ZOJ 3886 Nico Number(筛素数+Love(线)Live(段)树)
- ZOJ3886 2015July月赛 F-Nico Number(线段树)
- ZOJ 1484 Minimum Inversion Number(线段树,数论)
- zoj3886.Nico Number
- zoj2886 Nico Number
- ZOJ3886-Nico Number
- zoj 1484 Minimum Inversion Number(树状数组||线段树)
- ZOJ 3724 Delivery(离线线段树)
- zoj 2900 DP(线段树优化)
- ZOJ 2671 Cryptography(矩阵线段树)
- PIC18F45K80单片机串口调试总结
- 手动计算KMP算法的Next数组与NextVal数组
- Android视频播放器mediaplayer
- java基础笔记
- Java中的异常(1)
- ZOJ 3886 Nico number(线段树)
- 如何拉伸有透明区域的png图片来做UIButton的背景图片
- poll&&epoll实现分析(二)——epoll实现
- poj 1823 Hotel(线段树·区间更新·lazy tag)
- 32位机和64位机编程时应该注意些什么
- HDU2.2.5 三角形
- 不要死性不改
- Django之第一个app<15>
- UVa 10635 - Prince and Princess(LCS转LIS)