题目连接: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. SpringMvc入门五----文件上传

      知识点: SpringMvc单文件上传 SpringMvc多文件上传   这里我直接演示多文件上传,单文件的上传就不说了,不过代码都是现成的. 效果预览:   DEMO图:     添加文件上传j ...

  2. 在hibernate中使用c3p0数据源

    1.  jar包  hibernate-release-4.1.8.Final\lib\optional\c3p0 2.  加入配置:   <!-- 数据库连接池最大连接数 -->  &l ...

  3. 杭电ACM2084--数塔

    http://acm.hdu.edu.cn/showproblem.php?pid=2084 这种DP是相对容易的,一个二维数组,遍历一次,计算结果,存在指定位置. 本题关键代码是: a[i-1][j ...

  4. POJ 1384

    求猜存钱罐中至少有多少钱.容易知道金币总的重量,接着背包. #include<cstdio> #include<iostream> using namespace std; # ...

  5. UITableView学习笔记

    //非原创 看TableView的资料其实已经蛮久了,一直想写点儿东西,却总是因为各种原因拖延,今天晚上有时间静下心来记录一些最近学习的TableView的知识.下面进入正题,UITableView堪 ...

  6. multiple backgrounds 多重背景

    语法缩写如下: background : [background-color] | [background-image] | [background-position][/background-siz ...

  7. 计算 sql查询语句所花时间

    --1:下面这种是SQL Server中比较简单的查询SQL语句执行时间方法,通过查询前的时间和查询后的时间差来计算的: declare @begin_date datetimedeclare @en ...

  8. 使用tortoise git管理gitolite版本库

    gitolite-admin是用于管理git版本库的版本库,将其从服务器上clone下来. 使用tortoise git clone的时候需要指定私钥,私钥的格式是ppk的,需要使用putty的PUT ...

  9. 在HTML中通过jQuery设置列表项符号

    在创建列表的时候,可以通过指定type来设置列表项的符号,如下所示: <body> <form id="form1" runat="server&quo ...

  10. Flex xxx-app.xml配置

    <?xml version="1.0" encoding="UTF-8"?>  <application xmlns="http:/ ...