B - Bash and a Tough Math Puzzle CodeForces - 914D (线段树的巧妙应用)
题目大意:当输入2时,将p处的点的值修改为x,
当输入1时,判断区间[L,R]的gcd是否几乎正确,几乎正确的定义是最多修改一个数,使得区间[L,R]的gcd为x。
题解:用线段树维护一个gcd数组,在查询的时候,线段树的查询本质就是不停的分块,这时我们可以添加一些剪纸,比如说,对一个根节点root,如果说他的左儿子的值为tree[root*2],如果他他是x的倍数,那就没必要往下分了。如果不是的话,就往下分,直到找到了某一个点,我们可以记录一下,如果说点的个数大于等于2直接可以退出了。(太秒了,我刚开始也是这样想的,当时觉得如果查每个值的话,复杂度不就是o(n)了?,如果不加剪枝的话,确实是o(n),如果加了剪枝,还是log,秒~)
code:
#include<bits/stdc++.h>
using namespace std;
const int N=5E5+;
int arr[N];
int tree[N+N+N];
int n;
int cnt;
int gcd(int a,int b){
return b? gcd(b,a%b):a;
}
void push(int root){
tree[root]=gcd(tree[root*],tree[root*+]);
}
void build(int root,int l,int r){
if(l>r) return ;
if(l==r){
scanf("%d",&tree[root]);
return ;
}
int mid=(l+r)>>;
build(*root,l,mid);
build(*root+,mid+,r);
push(root);
}
void update(int root,int l,int r,int pos,int x){
if(l>pos||r<pos||l>r) return ;
if(l==r){
if(l==pos) tree[root]=x;
return ;
}
int mid=(l+r)>>;
update(root*,l,mid,pos,x);
update(root*+,mid+,r,pos,x);
push(root);
}
void query(int root,int l,int r,int xl,int xr,int x){
if(cnt==) return ;
if(l>r||xr<l||r<xl) return ;
if(l==r){
if(tree[root]%x) cnt++;
return ;
}
int mid=(l+r)>>;
if(tree[root*]%x) query(root*,l,mid,xl,xr,x);
if(tree[root*+]%x) query(root*+,mid+,r,xl,xr,x);
}
int main(){
cin>>n;
build(,,n);
int m;cin>>m;
while(m--){
int a;cin>>a;
if(a==){
int l,r,x;
scanf("%d%d%d",&l,&r,&x);
cnt=;query(,,n,l,r,x);
if(cnt<=) puts("YES");
else puts("NO");
}
else {
int pos,x;
scanf("%d%d",&pos,&x);
update(,,n,pos,x);
}
}
return ;
}
B - Bash and a Tough Math Puzzle CodeForces - 914D (线段树的巧妙应用)的更多相关文章
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- Bash and a Tough Math Puzzle CodeForces - 914D (线段树二分)
大意:给定序列, 单点修改, 区间询问$[l,r]$内修改至多一个数后$gcd$能否为$x$ 这题比较有意思了, 要注意到询问等价于$[l,r]$内最多有1个数不为$x$的倍数 可以用线段树维护gcd ...
- Codeforces 914D Bash and a Tough Math Puzzle (ZKW线段树)
题目链接 Round #458 (Div. 1 + Div. 2, combined) Problem D 题意 给定一个序列,两种询问:单点修改,询问某个区间能否通过改变最多一个数使得该区间的 ...
- CF 914 D. Bash and a Tough Math Puzzle
D. Bash and a Tough Math Puzzle http://codeforces.com/contest/914/problem/D 题意: 单点修改,每次询问一段l~r区间能否去掉 ...
- D. Bash and a Tough Math Puzzle 解析(線段樹、數論)
Codeforce 914 D. Bash and a Tough Math Puzzle 解析(線段樹.數論) 今天我們來看看CF914D 題目連結 題目 給你一個長度為\(n\)的數列\(a\), ...
- Codecraft-18 and Codeforces Round #458:D,Bash and a Tough Math Puzzle
题目传送门 题目大意:Bash喜欢对数列进行操作.第一种操作是询问l~r区间内的gcd值是否几乎为x,几乎为表示能否至多修改一个数达到.第二种操作是将ai修改为x.总共Q个询问,N个数. Soluti ...
- [Codeforces 914D] Bash and a Tough Math Puzzle
[题目链接] https://codeforces.com/contest/914/problem/D [算法] 显然 , 当一个区间[l , r]中为d倍数的数的个数 <= 1 , 答案为Ye ...
- Codeforces 914D - Bash and a Tough Math Puzzle 线段树,区间GCD
题意: 两个操作, 单点修改 询问一段区间是否能在至多一次修改后,使得区间$GCD$等于$X$ 题解: 正确思路; 线段树维护区间$GCD$,查询$GCD$的时候记录一共访问了多少个$GCD$不被X整 ...
- Codeforces.914D.Bash and a Tough Math Puzzle(线段树)
题目链接 \(Description\) 给定一个序列,两种操作:一是修改一个点的值:二是给一个区间\([l,r]\),问能否只修改一个数使得区间gcd为\(x\). \(Solution\) 想到能 ...
随机推荐
- hdu1686kmp果题
kmp字符串匹配原理参考博客:https://blog.csdn.net/bqw18744018044/article/details/90516750 代码如下:(写一遍模板) #include&l ...
- P5663 加工零件 题解
原题链接 简要题意: 给定一个图,每次询问从 \(x\) 节点开始,\(y\) 步能不能达到 \(1\) 号节点. 算法一 这也是我本人考场算法.就是 深搜 . 因为你会发现,如果 \(x\) 用 \ ...
- CTF_WriteUp_HTTP基本认证(Basic access authentication)
HTTP基本认证 在HTTP中,基本认证(英语:Basic access authentication)是允许http用户代理(如:网页浏览器)在请求时,提供用户名和密码 的一种方式.HTTP基本认证 ...
- Mysql性能优化:什么是索引下推?
导读 索引下推(index condition pushdown )简称ICP,在Mysql5.6的版本上推出,用于优化查询. 在不使用ICP的情况下,在使用非主键索引(又叫普通索引或者二级索引)进行 ...
- python,keras,tensorflow安装问题 module 'tensorflow' has no attribute 'get_default_graph'
module ‘tensorflow’ has no attribute ‘get_default_graph’当我使用keras和tensorflow做深度学习的时候,python3.7报了这个错误 ...
- mongodb_2
一.游标 在mongodb中,底层使用js引擎进行各种操作,所以我们在命令行窗口,可直接执行js代码. #使用for循环,插入1000条数据. > for (var i=0;i<1000; ...
- SpringBoot中常见的错误
数据源配置问题 原因 spring boot默认会加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration类, ...
- Git 命令实战入门 ,奶妈级教程
我不会用*官方*的语言告诉你Git 是什么,对此我表示深深得歉意--在我看来像CSDN.博客园.掘金等博客交流平台就是小的“GitHub”,只不过在这里更多的是一些零零散散的笔记或者文章,其实Gihu ...
- java web综合案例
1.采用的技术: bootstrap+jsp+servlet+三层架构(servlet,service,dao)+mysql 注意:mysql使用的是5.5版本,使用高版本会有很多问题.可以将5.5版 ...
- Unity引擎入门——制作第一个2D游戏(1)
Unity作为当今最流行的游戏引擎之一,受到各大厂商的喜爱. 像是炉石传说,以及最近的逃离塔克夫,都是由unity引擎开发制作. 作为初学者的我们,虽然无法直接做出完成度那么高的作品,但每一个伟大的目 ...