zoj 3911 Prime Query (ZOJ Monthly, October 2015 - I)

题目链接~~~



You are given a simple task. Given a sequence A[i] with N numbers. You have to perform Q operations on the given sequence.

Here are the operations:

  • A v l, add the value v to element with index l.(1<=V<=1000)
  • R a l r, replace all the elements of sequence with index i(l<=i<= r) with a(1<=a<=10^6) .
  • Q l r, print the number of elements with index i(l<=i<=r) and A[i] is a prime number

Note that no number in sequence ever will exceed 10^7.

Input

The first line is a signer integer T which is the number of test cases.

For each test case, The first line contains two numbers N and Q (1 <= N, Q <= 100000) - the number of elements in sequence and the number of queries.

The second line contains N numbers - the elements of the sequence.

In next Q lines, each line contains an operation to be performed on the sequence.

Output

For each test case and each query,print the answer in one line.

Sample Input
1
5 10
1 2 3 4 5
A 3 1      
Q 1 3
R 5 2 4
A 1 1
Q 1 1
Q 1 2
Q 1 4
A 3 5
Q 5 5
Q 1 5
Sample Output
2
1
2
4
0

4

很久没写线段树了,结果写了一发一直wa原因是pushdown()的root没赋0 附代码

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N = 10000010;
bool isprime[N+5];
void init(){
    isprime[0] = isprime[1] = 1;
    for(long long i=2; i<N; i++)
        if(!isprime[i])
            for(long long j=i*i; j<N; j+=i)
                isprime[j]=1;
    for(int i = 0 ;i<N;i++) isprime[i] = !isprime[i];
//    printf("%d\n",isprime[1]);

}
const int maxn = 100000+10;

int sum[maxn<<2];
int lazy[maxn<<2];
int ad_[maxn<<2];
int num[maxn<<2];
int x[maxn];
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
void pushup(int rt) {
    sum[rt] = sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt) {
    if(l==r) {
        num[rt] = x[l];
        if(isprime[x[l]])
        sum[rt] = 1;
        else sum[rt] = 0;
        return ;
    }
    int mid = (l+r)/2;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushup(rt);
}
void pushdown(int rt,int m) {
    if(num[rt]) {
        if(isprime[num[rt]]) {
            sum[rt<<1] = (m-m/2);
            sum[rt<<1|1] = m/2;
        } else {
            sum[rt<<1] = sum[rt<<1|1] = 0;
        }
        num[rt<<1] = num[rt<<1|1] = num[rt];
        num[rt] = 0;
    }
}
void update(int l,int r,int rt,int L,int R,int val) {
    if(l>=L&&r<=R) {
        if(isprime[val]) sum[rt] = r+1-l;
        else sum[rt] = 0;
        num[rt] = val;
        return ;
    }
    int mid = (l+r)/2;
    pushdown(rt,r+1-l);
    if(L<=mid) {
        update(lson,L,R,val);
    }
    if(R>mid) {
        update(rson,L,R,val);
    }
    pushup(rt);
}
void add(int l,int r,int rt,int xx,int val) {
    if(l==r) {
        num[rt] += val;
        if(isprime[num[rt]]) {
            sum[rt] = 1;
        }
        else sum[rt] = 0;
        return ;
    }
    pushdown(rt,r+1-l);
    int mid = (l+r)/2;

    if(mid<xx) add(rson,xx,val);
    else add(lson,xx,val);
    pushup(rt);
}
int query(int l,int r,int rt,int L,int R) {
    if (L <= l && r <= R) {
        return sum[rt];
    }
    pushdown(rt , r - l + 1);
    int mid = (l + r) >> 1;
    int ret = 0;
    if (L <= mid) ret += query(lson,L , R );
    if (mid < R) ret += query(rson,L , R );
    return ret;
}
void print(int l,int r,int rt) {
    if(l==r) {
        printf("%d %d\n",l,num[rt]);
        return ;
    }
    int mid = (l+r)/2;
    print(l,mid,rt<<1);
    print(mid+1,r,rt<<1|1);
}
int main()
{
//    freopen("in.txt","r",stdin);
    memset(isprime,false,sizeof(isprime));
    init();
    int T;
    scanf("%d",&T);
    while(T--) {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i = 1;i<=n;i++) {
            scanf("%d",&x[i]);
        }
        memset(sum,0,sizeof(sum));
        memset(num,0,sizeof(num));
        build(1,n,1);

        while(m--) {
            char ss[5];int x,y,z;
            scanf("%s",ss);
            if(ss[0]== 'A') {
                scanf("%d%d",&z,&x);
                add(1,n,1,x,z);
            }
            else if(ss[0]=='Q') {
                scanf("%d %d",&x,&y);
                printf("%d\n",query(1,n,1,x,y));
            } else {
                scanf("%d%d %d",&z,&x,&y);
                update(1,n,1,x,y,z);
            }
//            print(1,n,1);
        }
    }
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值