博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)
阅读量:4331 次
发布时间:2019-06-06

本文共 1756 字,大约阅读时间需要 5 分钟。

 题目连接:

#include 
#include
#include
#include
#define lson rt<<1,L,mid#define rson rt<<1|1,mid+1,R/*水题题意:给出n个初始值,给出两种操作 0 x y v:将[x,y]的数改成v 1 x y :查询[x,y]区间有多少个素数(相同的数,有几个算几个,即有两个3,那么个数为2)*/using namespace std;const int maxn=1000005;int t,n,q;bool isprime[maxn];struct Node{ int sum; //统计该区间的素数个数 int lazy; int len;}tree[maxn<<2];//素数筛选法void init(){ memset(isprime,true,sizeof(isprime)); for(int i=2;i
>1; build(lson); build(rson); pushUp(rt);}void update(int rt,int L,int R,int l,int r,int v){ if(l<=L&&R<=r){ if(isprime[v]){ tree[rt].sum=R-L+1; tree[rt].lazy=1; } else{ tree[rt].sum=0; tree[rt].lazy=0; } return; } pushDown(rt); int mid=(L+R)>>1; if(l<=mid) update(lson,l,r,v); if(r>mid) update(rson,l,r,v); pushUp(rt);}int query(int rt,int L,int R,int l,int r){ if(l<=L&&R<=r){ return tree[rt].sum; } int mid=(R+L)>>1; int ret=0; pushDown(rt); if(l<=mid) ret+=query(lson,l,r); if(r>mid) ret+=query(rson,l,r); return ret;}int main(){ init(); int op,x,y,v; scanf("%d",&t); for(int i=1;i<=t;i++){ printf("Case %d:\n",i); scanf("%d%d",&n,&q); build(1,1,n); for(int j=1;j<=q;j++){ scanf("%d",&op); if(op==0){ scanf("%d%d%d",&x,&y,&v); update(1,1,n,x,y,v); } else{ scanf("%d%d",&x,&y); printf("%d\n",query(1,1,n,x,y)); } } } return 0;}

 

转载于:https://www.cnblogs.com/chenxiwenruo/p/3420701.html

你可能感兴趣的文章
MSsql2005如何启用xp_cmdshell
查看>>
Forbidden(403)的3种处理方式
查看>>
[转]Vim 复制粘帖格式错乱问题的解决办法
查看>>
Hexo 博客搭建指南
查看>>
C#生成静态文件
查看>>
【并查集入门专题1】A+B+D 三道模板题 hdu1232 hdu1233 poj2524【并查集模板】
查看>>
[Django 2]第一个django应用
查看>>
Dockerfile 构建前端node应用并用shell脚本实现jenkins自动构建
查看>>
netfiler/iptables
查看>>
网络相关命令-netstat
查看>>
小佩上班日记
查看>>
设计模式之构建者(Builder)模式
查看>>
Python 入门笔记
查看>>
解决在eclipse中导入项目名称已存在的有关问题
查看>>
启发式与元启发式算法
查看>>
[转]C# CancellationTokenSource 终止线程
查看>>
校赛热身 Problem B. Matrix Fast Power
查看>>
Android动画解析--XML
查看>>
table布局, td内部元素溢出边界问题。 (已解决)
查看>>
一道递归题
查看>>