ZOJ 3886 Nico Number(筛素数+Love(线)Live(段)树)
problemCode=3886">ZOJ 3886
题意:
定义一种NicoNico数x,x有下面特征:
全部不大于x且与x互质的数成等差数列,如x = 5 ,与5互素且不大于5的数1,2,3,4成等差数列。则5是一个NicoNico数。再定义三种操作:
1.南小鸟询问[L, R]内有多少个NicoNico数;
2.果皇把[L, R]内的数全部对v取余;
3.果皇将第K个数换成X。然后给你一个数列,并对这个数列运行若干操作
思路:
这样的问题果断要用LoveLive树线段树来做!
首先我们通过打表发现NicoNico数仅仅可能是素数。2的n次幂。6,所以能够先预处理,对范围内的全部NicoNico数进行标记。
建树过程:假设是NicoNico数则节点值为1,不是则为0。
更新操作1:对区间内的数进行取模即是区间改动。这里能够优化一下,假设区间最大值小于v,则不须要改动。
更新操作2:即单点改动。
查询操作:输出询问区间和就可以。
时间复杂度:
预处理:O(x)
建树:O(n)
查询与更新:操作次数O(m),每次操作O(logn)加起来是O(mlogn)
羞耻的代码君:
这次代码风格。
。
重在理解重在理解。
/*
* @author FreeWifi_novicer
* language : C++/C
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
using namespace std;
#define clr( x , y ) memset(x,y,sizeof(x))
#define cls( x ) memset(x,0,sizeof(x))
#define mp make_pair
#define pb push_back
#define lson l , mid , root << 1
#define rson mid + 1 , r , root << 1 | 1
typedef long long lint;
typedef long long ll;
typedef long long LL;
const int maxNico = 1e7 + 500 ;
const int maxHonoka = 1e5 + 7 ;
int Honoka_max[10 * maxHonoka] ;
int Honoka_sum[10 * maxHonoka] ;
bool Nico_prime[maxNico];
map<int , int> NicoNicoNi;
/* にっこにっこにー☆あなたのハートににこにこにー 笑颜届ける矢澤にこにこー にこにーって覚えてラブにこー */
void init(){
NicoNicoNi[0] = NicoNicoNi[6] = 1;
for( int i = 0 ; i <= 31 ; i++ ){
NicoNicoNi[( 1 << i )] = 1;
}
cls( Nico_prime );
for( int i = 2 ; i * i <= maxNico ; i++ ){
if( !Nico_prime[i] )
for( int j = i * i ; j <= maxNico ; j+=i )
Nico_prime[j] = 1;
}
for( int i = 2 ; i <= maxNico ; i++ )
if( !Nico_prime[i] ) NicoNicoNi[i] = 1;
}
void push_Yazawa( int root ){
Honoka_max[root] = max( Honoka_max[ root << 1 ] , Honoka_max[ root << 1 | 1 ] );
Honoka_sum[root] = Honoka_sum[ root << 1 ] + Honoka_sum[ root << 1 | 1 ] ;
}
void build_Kotori( int l , int r , int root ){
if( l == r ){
scanf( "%d" , &Honoka_max[root] );
if( NicoNicoNi[Honoka_max[root]] )
Honoka_sum[root] = 1;
else
Honoka_sum[root] = 0;
return ;
}
int mid = ( l + r ) / 2 ;
build_Kotori( lson );
build_Kotori( rson );
push_Yazawa( root );
}
void Honoka1( int ql , int qr , int x , int l , int r , int root ){
if( qr < l || ql > r )
return ;
if( Honoka_max[root] < x)
return ;
if( l == r ){
Honoka_max[root] %= x ;
if( NicoNicoNi[Honoka_max[root]] )
Honoka_sum[root] = 1;
else
Honoka_sum[root] = 0;
return ;
}
int mid = ( l + r ) / 2 ;
Honoka1( ql , qr , x , lson );
Honoka1( ql , qr , x , rson );
push_Yazawa( root );
}
void Honoka2( int pos , int x , int l , int r , int root ){
if( l == pos && r == pos ){
Honoka_max[root] = x ;
if( NicoNicoNi[x] )
Honoka_sum[root] = 1;
else
Honoka_sum[root] = 0;
return ;
}
int mid = ( l + r ) / 2 ;
if( mid >= pos )
Honoka2( pos , x , lson );
else
Honoka2( pos , x , rson );
push_Yazawa( root );
}
int Kotori( int ql , int qr , int l , int r , int root ){
if( ql > r || qr < l )
return 0 ;
if( ql <= l && qr >= r )
return Honoka_sum[root];
int res = 0 ;
int mid = ( l + r ) / 2 ;
if( ql <= mid )
res += Kotori( ql , qr , lson ) ;
if( qr > mid )
res += Kotori( ql , qr , rson ) ;
return res;
}
int main(){
//freopen("input.txt","r",stdin);
init();
int n ;
while( cin >> n ){
build_Kotori( 1 , n , 1 ) ;
int m ;
cin >> m ;
for( int i = 1 ; i <= m ; i++ ){
int num ;
scanf( "%d" , &num );
if( num == 1 ){
int left , right ;
scanf( "%d%d" , &left , &right ) ;
printf( "%d\n" , Kotori( left , right , 1 , n , 1 ));
}
else if( num == 2 ){
int left , right , mod ;
scanf( "%d%d%d" , &left , &right , &mod ) ;
Honoka1( left , right , mod , 1 , n , 1 ) ;
}
else if( num == 3 ){
int pos , x ;
scanf( "%d%d" , &pos , &x ) ;
Honoka2( pos , x , 1 , n , 1 ) ;
}
}
}
return 0;
}
ZOJ 3886 Nico Number(筛素数+Love(线)Live(段)树)的更多相关文章
- zoj 3886 Nico Number
中文题面: 问题描述] 我们定义一个非负整数是“好数”,当且仅当它符合以下条件之一: 1. 这个数是0或1 2. 所有小于这个数且与它互质的正整数可以排成一个等差数列 例如,8就是一个好数,因为1,3 ...
- zoj3886--Nico Number(素数筛+线段树)
Nico Number Time Limit: 2 Seconds Memory Limit: 262144 KB Kousaka Honoka and Minami Kotori are ...
- POJ-2689 Prime Distance (两重筛素数,区间平移)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13961 Accepted: 3725 D ...
- CF449C Jzzhu and Apples (筛素数 数论?
Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time li ...
- POJ2689-Prime Distance-区间筛素数
最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total S ...
- [Luogu]A%BProblem——线性筛素数与前缀和
题目描述 题目背景 题目名称是吸引你点进来的[你怎么知道的] 实际上该题还是很水的[有种不祥的预感..] 题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m接下来n行, ...
- POJ 2689.Prime Distance-区间筛素数
最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total S ...
- leetcode 204. Count Primes(线性筛素数)
Description: Count the number of prime numbers less than a non-negative number, n. 题解:就是线性筛素数的模板题. c ...
- Prime Path(POJ - 3126)【BFS+筛素数】
Prime Path(POJ - 3126) 题目链接 算法 BFS+筛素数打表 1.题目主要就是给定你两个四位数的质数a,b,让你计算从a变到b共最小需要多少步.要求每次只能变1位,并且变1位后仍然 ...
随机推荐
- JVM 内存分配模型概念和java中各种对象的存储
JVM 内存分配模型概念 --在工作中可能用到的机会不多,有个概念的了解 --此文是转载某位读者,应该是在阅读了<深入理解Java虚拟机JVM高级特性与最佳实践> 一书后,总结所得.写的不 ...
- modelform的操作以及验证
1,model的两个功能 1,数据库操作 2,验证只有一个clean方法作为钩子来操作,方法比较少 2,form(专门用来做验证的) 根据form里面写的类,类里面的字段,这些字段里有内置的的正则表达 ...
- Django中提供的6种缓存方式
由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用: 缓存,缓存将一个某个views的返回值保存至内存或者memcache中, ...
- python之接口与归一化设计
1接口 接口的概念: Java 语言中的接口很好的展现了接口的含义: IAnimal.java /* * Java的Interface很好的体现了我们前面分析的接口的特征: * 1)是一组功能的集合, ...
- vue路由打开新窗口
一. <router-link>标签实现新窗口打开: 官方文档中说 v-link 指令被 <router-link> 组件指令替代,且 <router-link> ...
- 附近有什么?8款可以查周边的App
如今科技发达的时代,手机的功能不仅仅只是能通讯聊天,而是逐渐的走进了人们的生活中.因为有了APP,我们的生活才更丰富,并且有很多是我们生活中不可缺少的软件,而这些软件便是根据手机中的GPS定位系统而来 ...
- Go web编程实例
1. go web编程入门 记录个web编程例子方便以后使用. 主要有: chan的使用(带缓存,不带缓存) client发起get/post请求 server解析get/post请求参数 http. ...
- mysql存储过程实例,查询多参数赋值
drop procedure if exists p_for_create_customer; create procedure p_for_create_customer()begin declar ...
- 未能加载文件或程序集“SuperMap.Data.dll”
重新配置的新的开发环境,使用的是原来的工程文件,编译通过,运行报错:"未能加载文件或程序集"SuperMap.Data.dll"或它的某一个依赖项.找不到指定的模块&qu ...
- Webdings字体和Wingdings字体对照表
一.Webdings是一个TrueType的dingbat字体,于1997年发表,搭载在其后的Microsoft Windows视窗系统内,大多的字形都没有Unicode的相对字. 使用很简单1.网页 ...