SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)
题目连接:http://www.spoj.com/problems/CNTPRIME/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#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=;
int t,n,q;
bool isprime[maxn]; struct Node{
int sum; //统计该区间的素数个数
int lazy;
int len;
}tree[maxn<<]; //素数筛选法
void init(){
memset(isprime,true,sizeof(isprime));
for(int i=;i<maxn;i++){
if(isprime[i]){
for(int j=i*;j<maxn;j+=i){
isprime[j]=false;
}
}
}
}
void pushUp(int rt){
tree[rt].sum=tree[rt<<].sum+tree[rt<<|].sum;
}
void pushDown(int rt){
if(tree[rt].lazy!=-){
tree[rt<<].lazy=tree[rt<<|].lazy=tree[rt].lazy;
tree[rt<<].sum=tree[rt].lazy?tree[rt<<].len:;
tree[rt<<|].sum=tree[rt].lazy?tree[rt<<|].len:;
tree[rt].lazy=-;
}
}
void build(int rt,int L,int R){
tree[rt].lazy=-;
tree[rt].len=R-L+;
if(L==R){
int v;
scanf("%d",&v);
if(isprime[v])
tree[rt].sum=;
else
tree[rt].sum=;
return;
}
int mid=(L+R)>>;
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+;
tree[rt].lazy=;
}
else{
tree[rt].sum=;
tree[rt].lazy=;
}
return;
}
pushDown(rt);
int mid=(L+R)>>;
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)>>;
int ret=;
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=;i<=t;i++){
printf("Case %d:\n",i);
scanf("%d%d",&n,&q);
build(,,n);
for(int j=;j<=q;j++){
scanf("%d",&op);
if(op==){
scanf("%d%d%d",&x,&y,&v);
update(,,n,x,y,v);
}
else{
scanf("%d%d",&x,&y);
printf("%d\n",query(,,n,x,y));
}
}
}
return ;
}
SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)的更多相关文章
- 蓝桥杯 算法训练 Torry的困惑(基本型)(水题,筛法求素数)
算法训练 Torry的困惑(基本型) 时间限制:1.0s 内存限制:512.0MB 问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7……这样的数叫做质数.Torry突 ...
- poj3468树状数组的区间更新,区间求和
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 47174 ...
- SPOJ 7259 Light Switching (水题,区间01取反)
#include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...
- POJ3468 线段树(区间更新,区间求和,延迟标记)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 97196 ...
- 牛客小白月赛6 F 发电 树状数组单点更新 求区间乘积 模板
链接:https://www.nowcoder.com/acm/contest/136/F来源:牛客网 HA实验是一个生产.提炼“神力水晶”的秘密军事基地,神力水晶可以让机器的工作效率成倍提升. ...
- poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 43507 Accepted: 12693 ...
- Codeforces Gym 100733J Summer Wars 线段树,区间更新,区间求最大值,离散化,区间求并
Summer WarsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...
- hdu 1556 涂气球 线段树(区间更新~对区间[x,y]更新,求任意节点被更新的次数)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 线段树区间更新,区间统计+离散化 POJ 2528 Mayor's posters
题意:有一个非常长的板子(10000000长),在上面贴n(n<=10000)张海报.问最后从外面能看到几张不同的海报. 由于板子有10000000长,直接建树肯定会爆,所以须要离散化处理,对于 ...
随机推荐
- Android实现Http协议案例
在Android开发中,使用Http协议实现网络之间的通信是随处可见的,使用http方式主要采用2中请求方式即get和post两种方式. 一.使用get方式: HttpGet httpGet = ne ...
- C#学习笔记(第1周作业)
受队友诱惑,去听了李强的C#公选课,第二天就完成作业了. 作业要求: 1. 小学生加法程序: 2. 能自由设置难度: 3. 对结果进行统计. 第一次写C#程序,遇到不少困难,和队友讨论,百度谷歌一齐上 ...
- Firefox 与 IE 对Javascript和CSS的区别
1. document.formName.item("itemName") 问题 说明:IE下,可以使用document.formName.item("itemName& ...
- C++实现设计模式之 — 简单工厂模式
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4251756.html 所谓简单工厂模式,是一种实例化对象的方式,只要输入需要实例化对象的名字 ...
- WP开发笔记——不同Item显示不同ApplicationBar:适用于Pivot与Panorama
一.在xaml页面定义两个ApplicationBar: <phone:PhoneApplicationPage.Resources> <shell:ApplicationBar I ...
- 支持IE6的树形节结构TreeTable实际应用案例
<script src="jquery.js" type="text/javascript"></script> <script ...
- 为什么要用ajax
Ajax应用程序的优势在于:1. 通过异步模式,提升了用户体验2. 优化了浏览器和服务器之间的传输,减少不必要的数据往返,减少了带宽占用3. Ajax引擎在客户端运行,承担了一部分本来由服务器承担的工 ...
- PHP 网站保存快捷方式的实现代码
介绍下使用PHP实现网站快捷方式的保存方法. PHP怎么实现网站保存快捷方式呢?下面是一段PHP代码,下面这段代码,可以PHP实现网站保存快捷方式,以便用户随时浏览. <?php /** * ...
- KBase使用教程
1. SELECT * FROM EXPERT_BASEINFO WHERE (源照片='*' not 源照片 is null) and 标准一级机构='山东大学' and 当前职称='*教授' 2. ...
- 小课堂Week10 例外处理设计的逆袭Part3
小课堂Week10 例外处理设计的逆袭Part3 今天是<例外处理设计的逆袭>这本书阅读的第三天,也是最后一天,我们会主要通过实例,对Part2中提出的例外处理等级进行解读. Level1 ...