题目大意:当输入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. div 3 frog jump

    There is a frog staying to the left of the string s=s1s2…sn consisting of n characters (to be more p ...

  2. 洛谷 P5221 Product 题解

    原题链接 庆祝!第二道数论紫题. 推式子真是太有趣了! \[\prod_{i=1}^n \prod_{j=1}^n \frac{\operatorname{lcm}(i,j)}{\gcd(i,j)} ...

  3. jquery实现元素的显示和隐藏

    这个是指定表格列的隐藏的demo,其余的都大同小异了 $("#account-table tr").find($("#tcNum")).show();//sho ...

  4. 重磅!!!一文总结Pytorch的8张思维导图!

    本文以思维导图的形式,为大家介绍了深度学习的核心内容,主要包括:深度学习与Pytorch简介.词向量.用pytorch处理常见的NLP和CV任务.图片风格迁移和GAN.Seq2Seq与Attentio ...

  5. 2.Grpc消息定义

    一个简单示例 syntax ="proto3";//设置默认版本,不写默认为proto2 //1,2,3 是字段的标记 Tag 不表示字段的值 message FirstMessa ...

  6. [POJ2533]Longest Ordered Subsequence<dp>

    题目链接:http://poj.org/problem?id=2533 描述: A numeric sequence of ai is ordered if a1 < a2 < ... & ...

  7. Html,css构建一个对话框,练习201911281028

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  8. Java递归练习201908091049

    package org.jimmy.autofactory.test; public class TestRecursive20190809 { public static void main(Str ...

  9. 十进制转化为非十进制C++代码

    还是先为大家介绍一下原理吧. 假设余数为 r ,十进制数为 n :(拆分为整数 zs ,余数 ys) 对 zs:需要将 zs 除 r 取余数,直到商为 0 停止,将余数倒序排列即可. 对 ys:需要将 ...

  10. 消息队列 NSQ 源码学习笔记 (一)

    nsqlookupd 用于Topic, Channel, Node 三类信息的一致性分发 概要 nsqlookup 知识点总结 功能定位 为node 节点和客户端节点提供一致的topic, chann ...