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. 分金币 [CQOI 2011] [BZOJ 3293]

    Description 圆桌上坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值. Inpu ...

  2. 唐平中讲座笔记 Reinforcement mechanism design 20171107

    渣排版预警,纯草稿... 唐平中.研究方向是经济学和ai方向,机制设计和拍卖设计. 内容:广告优化的方法论,自动优化. [内容] Basics on mechanism design and resr ...

  3. xcode9上传app时报错iTunes Store operation failed 解决方案

    问题 上传至itunes Connect时报了两个错: iTunes Store Operation Failed ERROR ITMS-xxxxx:"description length: ...

  4. sench touch 自定义小图标(转)

    自定义图标的方法 Sencha touch自带图标有限,有时需要自己添加图标.下面介绍自定义图标的方法: 首先需要生成图标字体.有许多网站提供在线生成图标字体的功能,比如IcoMoon,通过这个网站, ...

  5. 页面嵌套iframe后,点击里面的链接,然后父窗口跳转(子窗口控制父窗口的链接跳转)

    做app的时候遇到一个问题,一个页面,然后里面嵌套了一个另一个页面,想实现点击里面的链接,然后外面进行跳转,不然的话,里面的页面永远出不来, 后面想了个办法,app的页面都是打开打开,不关闭的,然后由 ...

  6. 如何修改IE浏览器的User-Agent用户代理字符串信息

    每款浏览器都有一个专属的 User-Agent 字符串信息, 通过 User-Agent 网站可以检测用户所使用的浏览器版本.某些网站为了让用户获得更好的浏览体验,通过检测用户的浏览器版本,以确认用户 ...

  7. 修复恢复"可疑"的SQLServer数据库

    今天机房突然断电,DB连不上了,提示 无法打开数据库'MyDB'.恢复操作已将该数据库标记为 SUSPECT. 原因是断电导致DB文件损坏 通过SQL Server Management Studio ...

  8. Jetpack 架构组件 LiveData ViewModel MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  9. [Python设计模式] 第19章 分公司=部门?——组合模式

    github地址:https://github.com/cheesezh/python_design_patterns 组合模式 组合模式,将对象组合成树形结构以表示"部分-整体" ...

  10. If 条件左边写常量?

      if判断时,常量最好写左边 例如: 编程规范反复强调变量放在双等号的右边,常量放在左边,就是为了规避出现 If (ulCnt = 0)这种语法正确,但是极有可能是笔误的情况.为了杜绝这种不必要的逻 ...