ZOJ 3911 Prime Query

Prime Query

Time Limit: 1 Second      Memory Limit: 196608 KB

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

212404




#include<cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=2650000;
int i,j,tot,prime[6700000];
bool v[10000010];
int Case,n,m,A[N],sum[N],val[N],rt,y,z;
char sss[5];
void check(int rt,int a,int b,int p)
{
    sum[rt]=p;
    if(v[p])
        val[rt]=b-a+1;
    else
        val[rt]=0;
}
void gao(int rt,int a,int b)
{
    if(sum[rt]==0)
        return;
    int mid=(a+b)>>1;
    check(rt<<1,a,mid,sum[rt]);
    check(rt<<1|1,mid+1,b,sum[rt]);
    sum[rt]=0;
}
void pushup(int rt)
{
    val[rt]=val[rt<<1]+val[rt<<1|1];
}
void build(int rt,int a,int b)
{
    sum[rt]=0;
    if(a==b)
    {
        sum[rt]=A[a];
        val[rt]=v[A[a]];
        return;
    }
    int mid=(a+b)>>1;
    build(rt<<1,a,mid);
    build(rt<<1|1,mid+1,b);
    pushup(rt);

}
void update(int rt,int a,int b,int c,int p)
{
    if(a==b)
    {
        sum[rt]+=p;
        val[rt]=v[sum[rt]];
        return;
    }
    gao(rt,a,b);
    int mid=(a+b)>>1;
    if(c<=mid)
        update(rt<<1,a,mid,c,p);
    else
        update(rt<<1|1,mid+1,b,c,p);
    pushup(rt);
}
void replace(int rt,int a,int b,int c,int d,int p)
{
    if(c<=a&&b<=d)
    {
        sum[rt]=p;
        if(v[p])
            val[rt]=b-a+1;
        else
            val[rt]=0;
        return;
    }
    gao(rt,a,b);
    int mid=(a+b)>>1;
    if(c<=mid)
        replace(rt<<1,a,mid,c,d,p);
    if(d>mid)
        replace(rt<<1|1,mid+1,b,c,d,p);
    pushup(rt);
}
int query(int rt,int a,int b,int c,int d)
{
    if(c<=a&&b<=d)
        return val[rt];
    gao(rt,a,b);
    int mid=(a+b)>>1;
    int t=0;
    if(c<=mid)
        t=query(rt<<1,a,mid,c,d);
    if(d>mid)
        t+=query(rt<<1|1,mid+1,b,c,d);
    pushup(rt);
    return t;
}
int main()
{
    for(i=2; i<=10000000; i++)
    {
        if(!v[i])
            prime[tot++]=i;
        for(j=0; j<tot; j++)
        {
            if(i*prime[j]>10000000)break;
            v[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
    for(i=2; i<=10000000; i++)
    {
        v[i]^=1;
        //printf("%d\n",v[i]);
    }
    scanf("%d",&Case);
    while(Case--)
    {
        scanf("%d%d",&n,&m);
        for(i=1; i<=n; i++)
            scanf("%d",&A[i]);
        build(1,1,n);
        while(m--)
        {
            scanf("%s%d%d",sss,&rt,&y);
            if(sss[0]=='A')
                update(1,1,n,y,rt);
            if(sss[0]=='R')
                scanf("%d",&z),replace(1,1,n,y,z,rt);
            if(sss[0]=='Q')
                printf("%d\n",query(1,1,n,rt,y));
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值