ZOJ3886 2015July月赛 F-Nico Number(线段树)

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

有三种操作:

1.查询 (l,r) 区间内满足条件的 ai 的个数
2. 把 (l,r) 区间内的 ai 都对v取模
3. 把p位置的数换成x

题目的条件就是数x,把 (1,x1) 里与x的最大公约数是1的数放进一个序列(升序),如果是这个序列是等差的,那么就是满足条件的。

可以发现素数是肯定满足条件的!因为素数p跟 1p1 的数都是最大公约数为1的,然后打个表发现除了2的倍数的数是可行的以外,就剩下6是满足的了…于是就试一波….

然后查询操作用线段树来维护, 一个是满足条件的个数, 一个是区间的最大值,因为操作2的v大于最大值就不用向下更新了。其他的都直接暴力更新下去。

#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <iostream>
#define LL long long
#define pb push_back
#define lb lower_bound
#define eps 1e-8
#define INF 0x3f3f3f3f
using namespace std;
const int N = 100005;
const int MAXN=10000010;
bool notprime[MAXN];
int n, m;
int a[N];
int tree[N*4], tm[N*4];
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
void init() {
    memset(notprime, 0, sizeof(notprime));
    for(int i=2; i<MAXN; i++) if(!notprime[i]) {
        if(i>MAXN/i) break;
        for(int j=i*i; j<MAXN; j+=i)
            notprime[j]=1;
    }
    notprime[6] = notprime[0] = notprime[1] = 0;
    for(int i=2; i<=MAXN; i+=i) notprime[i] = 0;
}
inline void pushup(int rt){
    tree[rt] = tree[rt<<1] + tree[rt<<1|1];
    tm[rt] = max(tm[rt<<1], tm[rt<<1|1]);
}
int query(int l, int r, int rt, int ql, int qr){
    if(qr < l || ql > r) return 0;
    if(ql <= l && r <= qr) return tree[rt];
    int mid = (l+r)>>1;
    return query(rson, ql, qr) + query(lson, ql, qr);
}
void update1(int l, int r, int rt, int wh, int x) {
    if(wh < l || wh > r) return ;
    if(l == r) {
        tm[rt] = x;
        tree[rt] = !notprime[tm[rt]]? 1:0;
        return ;
    }
    int mid = (l+r)>>1;
    update1(lson, wh, x);
    update1(rson, wh, x);
    pushup(rt);
}
void update2(int l, int r, int rt, int ul, int ur, int v){
    if(tm[rt] < v || ul > r || ur < l) return ;
    if(l == r) {
        tm[rt] %= v;
        tree[rt] = !notprime[tm[rt]]? 1:0;
        return ;
    }
    int mid = (l+r)>>1;
    update2(lson, ul, ur, v);
    update2(rson, ul, ur, v);
    pushup(rt);
}
void build(int l, int r, int rt){
    if(l == r){
        tm[rt] = a[l];
        tree[rt] = !notprime[tm[rt]]? 1:0;
        return ;
    }
    int mid = (l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void solve(){
    while(~scanf("%d", &n)){
        for(int i=1; i<=n; i++) scanf("%d", &a[i]);
        build(1, n, 1);
        scanf("%d", &m);
        for(int i=0; i<m; i++){
            int op, l, r, v;
            scanf("%d", &op);
            if(op == 1){
                scanf("%d %d", &l, &r);
                printf("%d\n", query(1, n, 1, l, r));
            } else if(op == 2){
                scanf("%d %d %d", &l, &r, &v);
                update2(1, n, 1, l, r, v);
            } else {
                scanf("%d %d", &l, &v);
                update1(1, n, 1, l, v);
            }
        }
    }
}
int main(void)
{
    #ifdef DK1
        freopen("/home/dk/桌面/1.in","r",stdin);
    #endif // DK
    init();
    solve();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值