ZOJ 3886 Nico number(线段树)
2015-08-29 20:48
369 查看
Nico Number
Time Limit: 2 Seconds Memory Limit: 262144 KB
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!
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)
Each time when Minami Kotori chooses some part of the array, you should output a line, the number of NicoNico-number in that part.
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 long
using 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;
}
Time Limit: 2 Seconds Memory Limit: 262144 KB
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
3 4 6 9 6 1 1 3 1 3 3 2 1 1 10 1 1 3 3 2 4 1 1 3
Sample Output
2 0 2 2
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 long
using 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;
}
相关文章推荐
- 线段树题集
- hdu1754
- HDU1394
- 敌兵布阵 (1)
- I Hate It (1)
- LCIS (2)
- A Simple Problem with Integers (2)
- Mayor's posters (3)
- Buy Tickets (3)
- 线段树
- UVA - 12532 Interval Product
- POJ 3264 Balanced Lineup
- hdu 1542 求矩形并的面积
- 关于数据结构之线段树
- poj 3225 关于集合运算
- poj 2352
- hdu1166敌兵布阵(线段树点修改)
- POJ 2352 Stars 线段树 pascal
- hdu 1698 Just A Hook 线段树的一道题
- HDU 1754 I Hate It