ZOJ 3886 Nico number(线段树)

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 called NicoNico-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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值