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. asp.net 中 UEditor 图片和附件上传失败的处理方法

    1.0 找到 net 文件夹下面的 web.config 配置文件,注释掉如下的两句: 2.0 Uploader文件默认属性为编译,将其属性改为内容以后重新运行程序,图片上传成功. 3.0 删除 im ...

  2. Web前端代码页面布局总结

    一.  html (1)编码:所有编码均采用xhtml,标签必须闭合,属性值用双引号包括,编码统一为utf-8. (2)语义化:语义化html,正确使用标签. (3)文件命名:命名以中文命名,依实际模 ...

  3. Learn ZYNQ (3)

    移植android3.3到ZedBoard follow doc:Android移植Guide1.3.pdf follow website: http://elinux.org/Zedboard_An ...

  4. zju(11)在IAR中移植ucos到msp430

    准备材料 1.在TI官网上下载430的固件库,我用的是msp430f5528的板子,下载的是F5xx_F6xx_Core_Lib 地址http://www.ti.com/tool/msp-exp430 ...

  5. BizTalk开发系列(二十四) BizTalk项目框架建议

    Asp.NET有MVC框架,大部份的开发都是按照MVC进行的.BizTalk是面向消息的开发,不能完全采用分层的开发模式.而微软只提供了 BizTalk项目开发的基本策略,通过分析相关的Complex ...

  6. Final-阶段站立会议4

    组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...

  7. 20145224&20145238《信息安全系统设计基础》实验四

    20145224陈颢文20145238荆玉茗 <信息安全系统设计基础>第四次实验报告 课程:信息安全系统设计基础 班级: 1452 姓名:荆玉茗 陈颢文 学号:20145238 20145 ...

  8. ssm maven项目启动 报SYSTEM_PROPERTIES_MODE_ENVIRONMENT

    1.jar包冲突,查看项目中的jar和pom.xml中配置的jar包 版本,把没用的jar包删掉

  9. EntityFramework SQLite

    安装完sqlite的nuget包后,还要设置App.config文件才能正常使用 1.  在<providers>节点添加一条提供器配置 <provider invariantNam ...

  10. Nest查询示例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...