您的位置:首页 > 产品设计 > UI/UE

ZOJ 3911 Prime Query

2015-10-12 22:23 573 查看
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
,sum
,val
,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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM算法