题目连接: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 (水题,区间更新,求区间的素数个数)的更多相关文章

  1. 蓝桥杯 算法训练 Torry的困惑(基本型)(水题,筛法求素数)

    算法训练 Torry的困惑(基本型) 时间限制:1.0s   内存限制:512.0MB      问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7……这样的数叫做质数.Torry突 ...

  2. poj3468树状数组的区间更新,区间求和

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 47174   ...

  3. SPOJ 7259 Light Switching (水题,区间01取反)

    #include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...

  4. POJ3468 线段树(区间更新,区间求和,延迟标记)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97196   ...

  5. 牛客小白月赛6 F 发电 树状数组单点更新 求区间乘积 模板

    链接:https://www.nowcoder.com/acm/contest/136/F来源:牛客网  HA实验是一个生产.提炼“神力水晶”的秘密军事基地,神力水晶可以让机器的工作效率成倍提升.   ...

  6. poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 43507   Accepted: 12693 ...

  7. Codeforces Gym 100733J Summer Wars 线段树,区间更新,区间求最大值,离散化,区间求并

    Summer WarsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...

  8. hdu 1556 涂气球 线段树(区间更新~对区间[x,y]更新,求任意节点被更新的次数)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. 线段树区间更新,区间统计+离散化 POJ 2528 Mayor&#39;s posters

    题意:有一个非常长的板子(10000000长),在上面贴n(n<=10000)张海报.问最后从外面能看到几张不同的海报. 由于板子有10000000长,直接建树肯定会爆,所以须要离散化处理,对于 ...

随机推荐

  1. flume+kafka (分区实现 默认单分区)

    这篇文章主要是log4j+flume+kafka的内容 首先从从下面的地址下载flume+kafka的插件包 https://github.com/beyondj2ee/flumeng-kafka-p ...

  2. PHP性能优化-编译级别的缓存

    最近安装了 php5.6,发现有了 opcache.so扩展文件,于是,搜索了一下,发现 zend opcache已经融入到 ph5.5以上的版本了,即兴奋,不用再去找xcache,apc,eAcce ...

  3. C# 获取汉字的拼音首字母

    /// <summary> /// 在指定的字符串列表CnStr中检索符合拼音索引字符串 /// </summary> /// <param name="CnS ...

  4. JAVA多线程学习3--线程一些方法

    一.通过sleep方法睡眠 在指定的毫秒数内让当前正在执行的线程休眠(暂停执行).该线程不丢失任何监视器的所属权. 二.线程优先级 线程具有优先级,范围为1-10. MAX_PRIORITY线程可以具 ...

  5. vim配置python开发环境

    vim配置python开发环境 一.安装vim sudo apt-get install vim 二.vim基础配置 #Centos6.5 /usr/share/vim/vim72 vi /etc/v ...

  6. Cookie 的运行机制以及常用规则

    一   setCookie        bool setcookie ( string name [, string value [, int expire [, string path [, st ...

  7. WCF + EF 遇到的问题

    此文承接上一文章 基于WCF的API实现 http://www.cnblogs.com/heyixiaoran/p/4000695.html 由于上一次Entity部分没怎么写,到Services里识 ...

  8. Java从入门到精通——基础篇之Servlet与JSP的区别

    一.基本概念 1.1 Servlet Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面.它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器 ...

  9. MySQL 多实例启动和关闭脚本

    DB: 5.5.14 OS:CentOS 6.3 在MySQL多实例中,一个一个启动灰常麻烦,因此写个简单的启动和关闭脚本 启动脚本:start.sh #!/bin/bash for port in ...

  10. ubuntu 下dbus的环境搭建和使用

    从https://launchpad.net/ubuntu/+source/dbus/1.10.6-1ubuntu2下载需要的dbus包,然后解压,./configure make && ...