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

2

1

2

4

0

4

#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <string>

#include <algorithm>

#include <set>

#include <map>

#include <vector>

#include <cmath>

#define VI vector<int>

#define VP vector<Point>

#define pr pair<int,int>

#define LL long long

#define fread() freopen("../in.in","r",stdin)

#define fwrite() freopen("out.out","w",stdout)

using namespace std;

const int Max = 1e7;

const int Maxn = 100000;

typedef struct Tree
{
int num;//记录所在区间的素数的个数 int lazy;//标记所在的区间有没有被整体更新
} Tree; int vis[Max+1000]; Tree Tr[Maxn*5]; int a[Maxn+100]; void init()//素数表
{
memset(vis,0,sizeof(vis)); int m= (int)sqrt(Max); vis[0]=1; vis[1]=1; for(LL i=2; i<=m; i++)
{
if(!vis[i])
{
for(LL j=i*i; j<=Max; j+=i)
{
vis[j]=1;
}
}
}
} void Pushup(int st,int L,int R)//线段树区间向上更新
{
if(Tr[st<<1|1].lazy&&Tr[st<<1].lazy&&Tr[st<<1|1].lazy==Tr[st<<1].lazy)//
{
Tr[st].lazy=Tr[st<<1].lazy;
}
else
{
Tr[st].lazy=0;
} Tr[st].num=Tr[st<<1].num+Tr[st<<1|1].num;
} void Pushdown(int st,int L,int R)//线段树区间向下更新
{
if(Tr[st].lazy&&L!=R)
{
int mid =(L+R)>>1; Tr[st<<1].lazy=Tr[st<<1|1].lazy=Tr[st].lazy; if(Tr[st].num)
{
Tr[st<<1|1].num=R-mid; Tr[st<<1].num=mid+1-L;
}
else
{
Tr[st<<1|1].num=Tr[st<<1].num=0;
} Tr[st].lazy=0;
}
} void Build(int L,int R,int st)//初始化线段树
{
Tr[st].lazy=0; Tr[st].num=0; if(L==R)
{
Tr[st].lazy=a[L]; Tr[st].num=(!vis[a[L]]); return ;
}
int mid=(L+R)>>1; Build(L,mid,st<<1); Build(mid+1,R,st<<1|1); Pushup(st,L,R);
} void Add(int L,int R,int st,int s,int d)//单点更新
{
Pushdown(st,L,R); if(L==s&&R==s)
{
Tr[st].lazy+=d; Tr[st].num=(!vis[Tr[st].lazy]); return ;
}
int mid =(L+R)>>1; if(s<=mid)
{
Add(L,mid,st<<1,s,d);
}
else
{
Add(mid+1,R,st<<1|1,s,d);
} Pushup(st,L,R);
} void Update(int L,int R,int st,int l,int r,int d)//区间更新
{
if(L>r||R<l)
{
return ;
} if(L>=l&&R<=r)
{
Tr[st].lazy=d; Tr[st].num=(!vis[d])*(R-L+1); return ;
} Pushdown(st,L,R); int mid = (L+R)>>1; if(l<=mid)
{
Update(L,mid,st<<1,l,r,d);
} if(r>mid)
{
Update(mid+1,R,st<<1|1,l,r,d);
} Pushup(st,L,R);
} int Query(int L,int R,int st,int l,int r)//区间查询
{
if(L>r||R<l)
{
return 0;
} Pushdown(st,L,R); if(L>=l&&R<=r)
{
return Tr[st].num; }
int mid=(L+R)>>1; int sum=0; if(l<=mid)
{
sum+=Query(L,mid,st<<1,l,r);
} if(r>mid)
{
sum+=Query(mid+1,R,st<<1|1,l,r);
} Pushup(st,L,R); return sum;
}
int main()
{
int T,n,q; int l,r,s,d; char op[3]; init(); scanf("%d",&T); while(T--)
{
scanf("%d %d",&n,&q); for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
} Build(1,n,1); while(q--)
{
scanf("%s",op); if(op[0]=='A')
{
scanf("%d %d",&d,&s); Add(1,n,1,s,d);
}
else if(op[0]=='Q')
{
scanf("%d %d",&l,&r); printf("%d\n",Query(1,n,1,l,r));
}
else if(op[0]=='R')
{
scanf("%d %d %d",&d,&l,&r); Update(1,n,1,l,r,d);
}
}
}
return 0;
}

Prime Query (ZOJ 3911 线段树)的更多相关文章

  1. ZOJ 3911 线段树

    题意:有N个数字,M个操作,然后回答每个Q开头的询问 操作形式: A val pos:在pos位置上+val Q l r:询问l~r之间有多少个质数 R val l r:把l~r之间的数字替换成val ...

  2. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  3. ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  4. ZOJ 3279-Ants(线段树)

    传送门:zoj 3279 Ants Ants Time Limit: 2 Seconds      Memory Limit: 32768 KB echo is a curious and cleve ...

  5. zoj 3888 线段树 ***

    卡n^2,用线段树降到nlogn 记录每个点上所覆盖线段的次小值,保证能有两条路径能走 #include<cstdio> #include<iostream> #include ...

  6. F - Count the Colors ZOJ - 1610 线段树染色(染区间映射)

    题意:给一段0-8000的线段染色 问最后 颜色x 有几段 题解:标准线段树  但是没有push_up  最后查询是单点按顺序查询每一个点 考虑过使用区间来维护不同的线段有多少种各色的线段  思路是 ...

  7. HDU 3911 线段树区间合并

    北京赛区快了,准备袭击数据结构和图论.倒计时 18天,线段树区间合并.维护一个最长连续.. 题意:给一个01串,以下有一些操作,问区间最长的连续的1的个数 思路:非常裸的线段树区间合并 #includ ...

  8. 【HDU5869】 Different GCD Subarray Query 题解 (线段树维护区间GCD)

    题目大意:求区间$[L,R]$中所有子区间产生的最大公因数的个数. ------------------------- 对于$gcd$,我们知道$gcd(a,b,c)=gcd(gcd(a,b),c)$ ...

  9. ZOJ 3911Prime Query [素数处理 + 线段树]

    Time Limit: 5 Seconds Memory Limit: 196608 KBYou are given a simple task. Given a sequence A[i] with ...

随机推荐

  1. Java配置----JDK开发环境搭建及环境变量配置

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  2. BizTalk开发系列(二十六) 使用Web Service

    Web Service是在构建SOA平台中广泛使用的技术.在BizTalk开发过程中使用SOAP适配器接收和发送 Web Services 请求.业务流程可以发布为 Web Services 并使用外 ...

  3. poj3292-Semi-prime H-numbers(筛法打表)

    一,题意:  一个H-number是所有的模四余一的数.(x=4*k+1)  如果一个H-number是H-primes 当且仅当它的因数只有1和它本身(除1外). 一个H-number是H-semi ...

  4. NPOI简单操作excel

    本文仅当是个记录文件,仅供初学者参考. 首先得using几个npoi的空间名如下: using NPOI.HSSF.UserModel;using NPOI.HSSF.Util;using NPOI. ...

  5. oracle课堂笔记

    1.DOS登录1.1.sqlplus 输入用户名.密码1.2.sqlplus /nolog conn 用户名/密码@ip地址/数据库名称 [如果是sys登录则必须加上as sysdba ,as sys ...

  6. android发送/接收json数据

    客户端向服务器端发送数据,这里用到了两种,一种是在url中带参数,一种是json数据发送方式: url带参数的写法: url+/?r=m/calendar/contact_list&uid=3 ...

  7. Mongo使用脚本更新数据

    SQL Server中我们经常要使用脚本来刷一些数据,在mongo中我们也可以使用mongo的脚本来刷mongo的数据 首先在命令窗口中链接到本地的mongo库 load("[脚本的地址]& ...

  8. Nginx配置(日志服务器中关于日志的产生)

    一:概括 1.需要配置的概括 定义日志格式 日志的分割字段:^A 日志格式:IP地址^A服务器时间^A请求参数 配置location,记录请求日志到本地磁盘 将数据按照给定的日志格式存储到本地磁盘 二 ...

  9. Wordpress基础:文章和页面的区别

    页面: 页面是你可以单独建立一个固定页面,可以作为留言板,或者通知的单页面,发布之后是固定的网址. 页面并不能被分类.亦不能拥有标签,但是它们可以有层级关系.您可将页面附属在另一个页面之下. 对应模板 ...

  10. Python基础二. 数据结构、控制流、运算符、真值测试

    一.概述 数据结构上广义上有两种,单一类型和集合类型 单一类型,表示一种对象 集合类型,表示包含多种对象 Python 中的内建的数据类型有str.list.tuple.dict.set.number ...