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(段)树)的更多相关文章

  1. zoj 3886 Nico Number

    中文题面: 问题描述] 我们定义一个非负整数是“好数”,当且仅当它符合以下条件之一: 1. 这个数是0或1 2. 所有小于这个数且与它互质的正整数可以排成一个等差数列 例如,8就是一个好数,因为1,3 ...

  2. zoj3886--Nico Number(素数筛+线段树)

    Nico Number Time Limit: 2 Seconds      Memory Limit: 262144 KB Kousaka Honoka and Minami Kotori are ...

  3. POJ-2689 Prime Distance (两重筛素数,区间平移)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 D ...

  4. CF449C Jzzhu and Apples (筛素数 数论?

    Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time li ...

  5. POJ2689-Prime Distance-区间筛素数

    最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  6. [Luogu]A%BProblem——线性筛素数与前缀和

    题目描述 题目背景 题目名称是吸引你点进来的[你怎么知道的] 实际上该题还是很水的[有种不祥的预感..] 题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m接下来n行, ...

  7. POJ 2689.Prime Distance-区间筛素数

    最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  8. leetcode 204. Count Primes(线性筛素数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题解:就是线性筛素数的模板题. c ...

  9. Prime Path(POJ - 3126)【BFS+筛素数】

    Prime Path(POJ - 3126) 题目链接 算法 BFS+筛素数打表 1.题目主要就是给定你两个四位数的质数a,b,让你计算从a变到b共最小需要多少步.要求每次只能变1位,并且变1位后仍然 ...

随机推荐

  1. C++程序设计方法3:default修饰符

    编译器自动生成的成员函数 如果以下成员函数用户都没有为类实现,则编译器会自动为类生成他们的缺省的实现 默认构造函数,空函数,什么也不做 析构函数,空函数,什么也不做: 拷贝构造函数-按bit位复制对象 ...

  2. mysql找安装路经,更改密码

    1:如果安装了mysql但是启动不了,可以到C:/Windows/System32的cmd.exe,用管理员身份打开,然后输入net strat mysql即可 2:忘记安装路经       sele ...

  3. js获取浏览器屏幕的尺寸

    浏览器屏幕尺寸参照表: 如何获取屏幕宽度: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: ...

  4. Spring MVC4 + Spring Security4 + Hibernate实例

    http://www.yiibai.com/spring-security/spring-mvc-4-and-spring-security-4-integration-example.html 在这 ...

  5. 问题7:JavaScript 常用正则示例

    1. trim功能(清除字符串两端空格) String.prototype.trim = function() { return this.replace(/(^\s+)|(\s+$)/g, ''); ...

  6. 咏南中间件支持JWT TOKEN

    咏南中间件支持JWT TOKEN

  7. YUV420、YUV422、RGB24转换

      //平面YUV422转平面RGB24static void YUV422p_to_RGB24(unsigned char *yuv422[3], unsigned char *rgb24, int ...

  8. Caffe-SSD相关源码说明和调试记录

    1      对Blob的理解及其操作: Blob是一个四维的数组.维度从高到低分别是: (num_,channels_,height_,width_) 对于图像数据来说就是:图片个数,彩色通道个数, ...

  9. springboot RestTemplate httpclient

    RestTemplate是spring支持的一个请求http rest服务的模板对象,性质上有点像jdbcTemplate RestTemplate底层还是使用的httpclient(org.apac ...

  10. C# IOThread

    在看微软的ASP.NET - 将 ASP.NET 用作高性能文件下载器 示例里面用到了IO 线程,以前打算自己撸的,这里贴出来 已标记一下: ///////////////////////////// ...