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}.


题意:

niconico数:所有比x小的数且与x互质的数,从小到大排列是一个等差数列,则x为niconico数。

长度为10^5的数组。三种操作:

第一:询问区间[l,r] niconico数的个数

第二:将区间 [ l , r ] 的数对v取模

第三:将某一个数更新为x

题解:

容易证明:niconico数只有三种:素数,2^k(k>1),6。

那么剩下的难点就是区间取模操作。

可以证明:一个数x,做多取模logx次就变为0。

因此,我们记录区间的最大值,每次区间取模操作的时候判断最大值如果小于v则不用操作,否则暴力更新到底即可。

复杂度O(nlogn)

代码如下:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<string>
#include<string.h>
#include<stack>
#include<vector>
#include<set>
#include<map>
typedef long long LL;
typedef unsigned long long LLU;
double pi = acos(-1);
const int nn = 110000;
const int inf = 0x3fffffff;
const int inff = 0x3fffffff;
const LL mod = 1000000003;
using namespace std;
bool isnic[nn*100];
void init()
{
    memset(isnic,true,sizeof(isnic));
    LL i,j;
    for(i=2;i<=10000000;i++)
    {
        if(isprime[i])
            for(j=i*i;j<=10000000;j+=i)
            {
                isprime[j]=false;
            }
    }
    for(i=2;i<=10000000;i*=2)
    {
        isnic[i]=true;
    }
    isnic[6]=true;
}
int n;
int a[nn];
int tr[nn*4],mx[nn*4];
void push_up(int id)
{
    tr[id]=tr[2*id]+tr[2*id+1];
    mx[id]=max(mx[2*id],mx[2*id+1]);
}
void build(int id,int l,int r)
{
    if(l==r)
    {
        tr[id]=isnic[a[l]]?1:0;
        mx[id]=a[l];
        return ;
    }
    int mid=(l+r)/2;
    build(2*id,l,mid);
    build(2*id+1,mid+1,r);
    push_up(id);
}
int query(int id,int l,int r,int L,int R)
{
    if(L<=l&&r<=R)
    {
        return tr[id];
    }
    int mid=(l+r)/2;
    int re=0;
    if(L<=mid)
        re+=query(2*id,l,mid,L,R);
    if(R>mid)
        re+=query(2*id+1,mid+1,r,L,R);
    return re;
}
void update(int id,int l,int r,int L,int R,int v)
{
    if(mx[id]<v)
        return ;
    if(l==r)
    {
        tr[id]=isnic[mx[id]%v]?1:0;
        mx[id]=mx[id]%v;
        return ;
    }
    int mid=(l+r)/2;
    if(L<=mid)
        update(2*id,l,mid,L,R,v);
    if(R>mid)
        update(2*id+1,mid+1,r,L,R,v);
    push_up(id);
}
void update1(int id,int l,int r,int p,int x)
{
    if(l==r)
    {
        tr[id]=isnic[x]?1:0;
        mx[id]=x;
        return ;
    }
    int mid=(l+r)/2;
    if(p<=mid)
        update1(2*id,l,mid,p,x);
    else
        update1(2*id+1,mid+1,r,p,x);
    push_up(id);
}
int main()
{
    init();
    int i,m;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        build(1,1,n);
        scanf("%d",&m);
        int id,l,r,v;
        while(m--)
        {
            scanf("%d%d%d",&id,&l,&r);
            if(id==1)
            {
                printf("%d\n",query(1,1,n,l,r));
            }
            else if(id==2)
            {
                scanf("%d",&v);
                update(1,1,n,l,r,v);
            }
            else if(id==3)
                update1(1,1,n,l,r);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值