首页 > 其他分享 >ZOJ 3911 Prime Query

ZOJ 3911 Prime Query

时间:2022-11-09 19:03:36浏览次数:55  
标签:Prime rr int ZOJ scanf mid void Query ll


Prime Query


Time Limit: 1 Second       Memory Limit: 196608 KB


You are given a simple task. Given a sequence A[i] with N

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]

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

Input

The first line is a signer integer T

For each test case, The first line contains two numbers N and Q (1 <= N, Q

The second line contains N

In next Q

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<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=10000005;
int T,n,m,p[maxn],u[maxn/10],tot=0;
int l,r,x;
char ch[2];

void pre()
{
p[0]=p[1]=1;
for (int i=2;i<maxn;i++)
{
if (p[i]==0) u[tot++]=i;
for (int j=0;j<tot&&i*u[j]<maxn;j++)
{
p[i*u[j]]=1;
if (i%u[j]==0) break;
}
}
}

struct ST
{
const static int maxn=500005;
int f[maxn],u[maxn];
void update(int x)
{
u[x]=u[x+x]+u[x+x+1];
f[x]=0;
}
void build(int x,int l,int r)
{
if (l==r)
{
scanf("%d",&f[x]);
u[x]=p[f[x]]^1;
}
else
{
int mid=l+r>>1;
build(x+x,l,mid);
build(x+x+1,mid+1,r);
update(x);
}
}
void down(int x,int l,int r)
{
int mid=l+r>>1;
f[x+x]=f[x+x+1]=f[x]; f[x]=0;
u[x+x]=(p[f[x+x]]^1)*(mid-l+1);
u[x+x+1]=(p[f[x+x+1]]^1)*(r-mid);
}
void add(int x,int l,int r,int a,int b)
{
if (l==r) {f[x]+=b; u[x]=p[f[x]]^1;}
else
{
if (f[x]) down(x,l,r);
int mid=l+r>>1;
if (a<=mid) add(x+x,l,mid,a,b);
else add(x+x+1,mid+1,r,a,b);
update(x);
}
}
void change(int x,int l,int r,int ll,int rr,int b)
{
if (ll<=l&&r<=rr) {f[x]=b; u[x]=(p[b]^1)*(r-l+1);}
else
{
int mid=l+r>>1;
if (f[x]) down(x,l,r);
if (ll<=mid) change(x+x,l,mid,ll,rr,b);
if (rr>mid) change(x+x+1,mid+1,r,ll,rr,b);
update(x);
}
}
int query(int x,int l,int r,int ll,int rr)
{
int ans=0;
if (ll<=l&&r<=rr) ans=u[x];
else
{
int mid=l+r>>1;
if (f[x]) down(x,l,r);
if (ll<=mid) ans+=query(x+x,l,mid,ll,rr);
if (rr>mid) ans+=query(x+x+1,mid+1,r,ll,rr);
}
return ans;
}
}st;

int main()
{
pre();
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
st.build(1,1,n);
while (m--)
{
scanf("%s",ch);
switch (ch[0])
{
case 'A':scanf("%d%d",&x,&l);
st.add(1,1,n,l,x); break;
case 'Q':scanf("%d%d",&l,&r);
printf("%d\n",st.query(1,1,n,l,r));break;
case 'R':scanf("%d%d%d",&x,&l,&r);
st.change(1,1,n,l,r,x); break;
}
}
}
return 0;
}





标签:Prime,rr,int,ZOJ,scanf,mid,void,Query,ll
From: https://blog.51cto.com/u_15870896/5838294

相关文章

  • ZOJ 3905 Cake
    CakeTimeLimit: 4Seconds     MemoryLimit: 65536KBAliceandBoblikeeatingcakeverymuch.Oneday,AliceandBobwenttoabakeryandboughtm......
  • [bzoj3033] 太鼓达人 (欧拉回路)
    学会了欧拉回路pwpwpwpwpwpDescription七夕祭上,Vani牵着cl的手,在明亮的灯光和欢乐的气氛中愉快地穿行。这时,在前面忽然出现了一台太鼓达人机台,而在机台前坐着的是......
  • QueryDet复现
     准备数据集来到官网下载页面:https://cocodataset.org/#download Images就是数据集,Annotations表示标注信息使用JSON格式存储(annotations),COCOAPI用于访问......
  • day28 jQuery
    概述:jQuery是一个轻量级的js库,它将js的功能进行了封装(所有的内容都是函数),它在封装的基础上做了进一步的兼容(兼容性好)特点:(优势)可链式调用(里......
  • 12个方便易用的jquery表单验证插件
    绝大部分网站都是开放注册的,而注册就需要使用表单验证,因为网站都需要对注册用户的信息安全性和合理性做出判断,表单的注册都应该具备完善的验证方式,比如注册使用的手机号是否......
  • jquery实现简单富文本编辑
      <script>$("#under").click(function(){varsec=getSelection()if(sec==undefined){return;......
  • jq(JQuery)操作cookie
     先引入jq封装好的方法<scriptsrc="//cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script> 设置新的cookie$.cookie('name','yvioo');//设......
  • R3下用ZwQueryObject/ZwDuplicateObject关闭互斥体和解除文件占用
      不少程序在运行时会创建/打开全局Mutex,来限制用户多开。百度上搜一圈下来,他们的实现基本是这样:intmain(intargc,char*argv[]){HANDLEhMtx=CreateMutex(NULL......
  • jQuery实现下拉选项框
    html文件:<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title><style>*{padding:0px;margin:0px;}.content{width:10......
  • jquery(js)实现上滑加载更多内容(分页查询)
    移动端上滑加载更多步骤移动端实现滚动上滑加载更多内容分为以下几步:步骤1禁用系统滚动条使用CSS样式禁用移动端的滚动条,代码如下所示:html,body{width:100%;overflow:hidde......