题目大意:当输入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 (线段树的巧妙应用)的更多相关文章

  1. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  2. Bash and a Tough Math Puzzle CodeForces - 914D (线段树二分)

    大意:给定序列, 单点修改, 区间询问$[l,r]$内修改至多一个数后$gcd$能否为$x$ 这题比较有意思了, 要注意到询问等价于$[l,r]$内最多有1个数不为$x$的倍数 可以用线段树维护gcd ...

  3. Codeforces 914D Bash and a Tough Math Puzzle (ZKW线段树)

    题目链接  Round #458 (Div. 1 + Div. 2, combined)  Problem D 题意  给定一个序列,两种询问:单点修改,询问某个区间能否通过改变最多一个数使得该区间的 ...

  4. 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区间能否去掉 ...

  5. D. Bash and a Tough Math Puzzle 解析(線段樹、數論)

    Codeforce 914 D. Bash and a Tough Math Puzzle 解析(線段樹.數論) 今天我們來看看CF914D 題目連結 題目 給你一個長度為\(n\)的數列\(a\), ...

  6. Codecraft-18 and Codeforces Round #458:D,Bash and a Tough Math Puzzle

    题目传送门 题目大意:Bash喜欢对数列进行操作.第一种操作是询问l~r区间内的gcd值是否几乎为x,几乎为表示能否至多修改一个数达到.第二种操作是将ai修改为x.总共Q个询问,N个数. Soluti ...

  7. [Codeforces 914D] Bash and a Tough Math Puzzle

    [题目链接] https://codeforces.com/contest/914/problem/D [算法] 显然 , 当一个区间[l , r]中为d倍数的数的个数 <= 1 , 答案为Ye ...

  8. Codeforces 914D - Bash and a Tough Math Puzzle 线段树,区间GCD

    题意: 两个操作, 单点修改 询问一段区间是否能在至多一次修改后,使得区间$GCD$等于$X$ 题解: 正确思路; 线段树维护区间$GCD$,查询$GCD$的时候记录一共访问了多少个$GCD$不被X整 ...

  9. Codeforces.914D.Bash and a Tough Math Puzzle(线段树)

    题目链接 \(Description\) 给定一个序列,两种操作:一是修改一个点的值:二是给一个区间\([l,r]\),问能否只修改一个数使得区间gcd为\(x\). \(Solution\) 想到能 ...

随机推荐

  1. hdu1455 拼木棍(经典dfs)

    给定木棍序列,求解能将木棍拼成相同长度的数根长木棍的情况下长木棍长度的最小值. /*hdu1455dfs */ #include<bits/stdc++.h> using namespac ...

  2. 题解 CF1304E 【1-Trees and Queries】

    前言 这场比赛,在最后 \(5\) 分钟,我想到了这道题的 \(Idea\),但是,没有打完,比赛就结束了. 正文 题目意思 这道题目的意思就是说,一棵树上每次给 \(x\) 和 \(y\) 节点连 ...

  3. jviisualvm监控远程主机java程序实战与问题排查

    1.远程主机运行jstatd 首先新建文件 jstatd.all.policy ,内容如下 grant codebase "file:${java.home}/../lib/tools.ja ...

  4. FormDataMultiPart获取表单文件的大小

    在完成springboard某个功能时遇到个问题,前端表单上传了个文件,服务端接收的是FormDataMultiPart,希望通过FormDataMultiPart拿到上传文件的size. 一开始获取 ...

  5. 在Windows Python3.4 上安装NumPy、Matplotlib、SciPy和IPython

    NumPy 下载地址: http://sourceforge.net/projects/numpy/files/NumPy/1.8.1/ SciPy 下载地址: http://sourceforge. ...

  6. 十分钟一起学会Inception网络

    作者 | 荔枝boy 编辑 | 安可 一.Inception网络简介 二.Inception网络模块 三.Inception网络降低参数计算量 四.Inception网络减缓梯度消失现象 五.Ince ...

  7. 基于vue-cli-和element-ui的开发admin(1)

    //首先以下仅是记录个人本次vue后台管理系统的登录界面部分操作的流程以及踩坑的注意点 一.首先是搭建vue-cli工作环境 这里有两种方式:1.用npm:(在安装了vue,vue-cli以及webp ...

  8. centos7中安装mysql

    centos7中安装mysql网上已经很多资源了,我就不在赘述了.我这里只是记录下我安装的时候出现的一些问题. 原文:https://www.cnblogs.com/bigbrotherer/p/72 ...

  9. [HDU1029]Ignatius and the Princess IV<桶 水题>

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029 题目大意: 多组数据,每组数据先给一个n,然后给n各数字,找出n各数字中出现了至少(n+1)/2 ...

  10. 小案例带你揭秘JS事件

    小案例带你揭秘JS事件 ### 什么是事件? 在js中一个事件的组成由那些呢? 谁触发事件:事件源 触发什么事件: 事件的类型 触发事件干什么事:事件处理函数 事件传播的过程 捕获阶段 就是从wind ...