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\) 想到能 ...
随机推荐
- dom&JavaScript&Jquery
目录 dom&JavaScript&Jquery 建节点 添加节点 删除节点: 替换节点: 属性节点 获取值操作 class的操作 指定CSS操作 操作节点 获取input用户输入 操 ...
- editplus软件使用技巧
1.文本文件的特点 注:不针对editplus这个软件,对于其他的文本文件处理软件也同样适用. 文本文件就是不包含其它文字格式(比如字体,字号,对齐,行间距等)以及富文本(比如图片,表格等)的一种纯文 ...
- python 错误记录及处理
1.pandas解决“pandas.parser.CParserError: Error tokenizing data. C error: Expected 2 fields in line 3, ...
- python分布式接口,参数化实战二
1,先看一下接口测试用例 2,文件1:写get和post模板 import requestsclass PostGetModels: def isMethod(self,url,data,method ...
- 【Pytest02】全网最全最新的Pytest框架快速进阶篇(pytest前置和后置以及忽略测试用例)
一.Pytest的前置和后置方法 1.Pytest可以集成unittest实现前置和后置 import unittest import pytest class TestCase(unittest.T ...
- Python python 函数参数:必选参数,默认参数
import math # 函数的必选参数 '''函数的必选参数,指的是函数调用的时候必须传入的参数 ''' def cal (n): return n * n var = cal(2) '''上面的 ...
- 《Mathematical Analysis of Algorithms》中有关“就地排列”(In Situ Permutation)的算法分析
问题描述 把数列\((x_1,x_2,\cdots,x_n)\)变换顺序为\((x_{p(1)},x_{p(2)},\cdots,x_{p(n)})\),其中\(p\)是\(A=\{1,2,3,\cd ...
- iOS UmbrellaHeader
Lexical or Preprocessor Issue - Umbrella header for module 'xxx' does not include header 'xxx.h' fra ...
- flask中的分页器
paginate(): 分页查询,返回一个分页对象 paginate(参数1, 参数2, 参数3) : 参数1:当前是第几页(page) 参数2:每页显示几条信息(per_page) 参数3:err ...
- 面试中常问的五种IO模型和BIO,NIO,AIO
一,五种IO模型: 一个IO操作可以分为两个步骤:发起IO请求和实际的IO操作例如:1.操作系统的一次写操作分为两步:第一步,将数据从用户空间拷贝到系统空间:第二步,从系统空间往网卡写.2.一次读操作 ...