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长,直接建树肯定会爆,所以须要离散化处理,对于 ...
随机推荐
- How to use For loop in CruiseControl.net
Looping through parameter values in Cruisecontrol.net Example >>>>>>>>>&g ...
- linux IO子系统和文件系统读写流程
本文转载自:http://blog.csdn.net/kidd_3/article/details/6909097 Technorati 标签: I/O 子系统 --------------- ...
- javaScript 对json数据按key值排序
var ajson= { "result":[ { "cid":1, "name":"aaa", "price ...
- memory model
最近看C++11 atomic发现对memory_order很是不理解,memory_order_relaxed/memory_order_consume/memory_order_acquire/m ...
- Eclispe使用Maven添加官方库的jar包
先到百度或google搜索maven仓库,在仓库中搜索需要的jar包,如poi.jar. 搜索到之后找到需要的jar包,找到这里
- 重写DataGridViewColumn
做个项目需要用到DataGridView,这个控件还是挺好用的,但是今天却发现无法实现自己想要的功能.主要是DataGridViewCheckBoxColumn这个列虽然是提供了复选框,但是却未能在复 ...
- 基于jQuery的判断iPad、iPhone、Android是横屏还是竖屏
function orient() {if (window.orientation == 90 || window.orientation == -90) {//ipad.iphone竖屏:Andri ...
- 使用 Visual Studio Team Test 进行单元测试和java中的测试
C#中test测试地 方法一. 1.从NUnit官网(http://www.nunit.org/index.php)下载最新版本NUnit,当前版本为NUnit2.5.8. 2.安装后,在VS2008 ...
- 第六周 E题 期望.....
Description Given a dice with n sides, you have to find the expected number of times you have to thr ...
- opencv学习笔记(04)——ROI
ROI的用法:1.直接相加:2.掩码法 #include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgpro ...