1. 程式人生 > >zoj 3886 Nico Number(線段樹,區間取模操作)

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