D. Bash and a Tough Math Puzzle
time limit per test

2.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Bash likes playing with arrays. He has an array a1, a2, ... an of n integers. He likes to guess the greatest common divisor (gcd) of different segments of the array. Of course, sometimes the guess is not correct. However, Bash will be satisfied if his guess is almost correct.

Suppose he guesses that the gcd of the elements in the range [l, r] of a is x. He considers the guess to be almost correct if he can change at most one element in the segment such that the gcd of the segment is x after making the change. Note that when he guesses, he doesn't actually change the array — he just wonders if the gcd of the segment can be made x. Apart from this, he also sometimes makes changes to the array itself.

Since he can't figure it out himself, Bash wants you to tell him which of his guesses are almost correct. Formally, you have to process qqueries of one of the following forms:

  • 1 l r x — Bash guesses that the gcd of the range [l, r] is x. Report if this guess is almost correct.
  • 2 i y — Bash sets ai to y.

Note: The array is 1-indexed.

Input

The first line contains an integer n (1 ≤ n ≤ 5·105)  — the size of the array.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109)  — the elements of the array.

The third line contains an integer q (1 ≤ q ≤ 4·105)  — the number of queries.

The next q lines describe the queries and may have one of the following forms:

  • 1 l r x (1 ≤ l ≤ r ≤ n, 1 ≤ x ≤ 109).
  • 2 i y (1 ≤ i ≤ n, 1 ≤ y ≤ 109).

Guaranteed, that there is at least one query of first type.

Output

For each query of first type, output "YES" (without quotes) if Bash's guess is almost correct and "NO" (without quotes) otherwise.

Examples
input
3
2 6 3
4
1 1 2 2
1 1 3 3
2 1 9
1 1 3 2

output
YES
YES
NO

input
5
1 2 3 4 5
6
1 1 4 2
2 3 6
1 1 4 2
1 1 5 2
2 5 10
1 1 5 2

output
NO
YES
NO
YES

题意:

对于长度为n的数列有两种操作:

①询问L, R, x:区间[L, R]是否能将最多一个数字修改成x后它们Gcd为x,YESorNO

②修改x,y:将第x个数改成y

用线段树维护一个区间的Gcd,修改直接就是单点修改

不过询问允许你将区间中一个数字改成x

也就是说区间[L, R]是否满足最多只有一个数不是x的倍数

线段树区间查询的时候直接查询Gcd不是x倍数的区间即可!如果这样的区间超过1个就一定是NO!

显然真正递归到叶子的区间只会有1个,所以复杂度O(nlogn)

#include<stdio.h>
int tre[2222222];
int Gcd(int a, int b)
{if(b==0)return a;return Gcd(b, a%b);
}
void Create(int l, int r, int x)
{int m;if(l==r){scanf("%d", &tre[x]);return;}m = (l+r)/2;Create(l, m, x*2);Create(m+1, r, x*2+1);tre[x] = Gcd(tre[x*2], tre[x*2+1]);
}
void Update(int l, int r, int x, int a, int b)
{int m;if(l==r){tre[x] = b;return;}m = (l+r)/2;if(a<=m)Update(l, m, x*2, a, b);elseUpdate(m+1, r, x*2+1, a, b);tre[x] = Gcd(tre[x*2], tre[x*2+1]);
}
int Query(int l, int r, int x, int a, int b, int mt)
{int m, ans = 0;if(l==r)return 1;m = (l+r)/2;if(a<=m && tre[x*2]%mt!=0)ans = Query(l, m, x*2, a, b, mt);if(b>=m+1 && tre[x*2+1]%mt!=0 && ans<=1)ans += Query(m+1, r, x*2+1, a, b, mt);return ans;
}
int main(void)
{int n, q, opt, L, R, x, y;scanf("%d", &n);Create(1, n, 1);scanf("%d", &q);while(q--){scanf("%d", &opt);if(opt==1){scanf("%d%d%d", &L, &R, &x);if(Query(1, n, 1, L, R, x)<=1)printf("YES\n");elseprintf("NO\n");}else{scanf("%d%d", &x, &y);Update(1, n, 1, x, y);}}return 0;
}

Codecraft-18 and Codeforces Round #458: D. Bash and a Tough Math Puzzle(线段树)相关推荐

  1. 【Codeforces Round #458 D.Bash and a Tough Math Puzzl】线段树

    链接 Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) 题意 给你一个区间,要支持两种区间操作. 第一种操作是单点更 ...

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

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

  3. cf914D. Bash and a Tough Math Puzzle(线段树)

    题意 题目链接 Sol 直接在线段树上二分 当左右儿子中的一个不是\(x\)的倍数就继续递归 由于最多递归到一个叶子节点,所以复杂度是对的 开始时在纠结如果一段区间全是\(x\)的两倍是不是需要特判, ...

  4. Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp

    D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...

  5. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸

    D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  6. Codeforces Round #686 (Div. 3) F. Array Partition(二分+线段树)

    题意:一段区间,让你分割成三段,第一段取max,第二段取min,第三段取max.问你怎么分割这个区间. 题解: 三个区间我们可以用两个点将一段区间分成三段区间. 二分:我们首先找这个题有关的单调性,我 ...

  7. Educational Codeforces Round 81 (Rated for Div. 2) E. Permutation Separation 线段树 + dp

    传送门 文章目录 题意: 思路: 题意: 给你一个打乱的排列,每个位置都各有一个价值,让你选择一个分界点,分成p1,p2,...,prp_1,p_2,...,p_rp1​,p2​,...,pr​和pr ...

  8. Codeforces Round #223 (Div. 2): E. Sereja and Brackets(线段树)

    题意: 给你一个括号序列和m次询问,每次询问区间[L, R]内匹配的括号个数 思路: 这道题线段树只用来维护区间最小值,所以理论上RMQ也可以,主要是要稍微推一下 设左括号为1,右括号为-1,s[]为 ...

  9. Codeforces Round #807 (Div. 2) E. Mark and Professor Koro(线段树二分)

    E. Mark and Professor Koro 题意 给定一个长度为n的数组,有q次更新操作,每次更新会将下标为k的元素的值更新为l,数组的更新是永久的.将数组更新后假设重复做以下操作,求可以得 ...

最新文章

  1. 25个让人惊叹的 HTML5 应用实验
  2. CSS padding margin border属性讲解
  3. 支付宝php rsa签名验签工具,alipay rsa2 签名验证
  4. Netty源码分析第3章(客户端接入流程)----第3节: NioSocketChannel的创建
  5. 2020年上海将初步建成“泛在化、融合化、智敏化”智慧城市
  6. 你想要的宏基因组-微生物组知识全在这(2021.8)
  7. Drool规则引擎介绍
  8. php初级入门教程_PHP初学者教程
  9. matlab虚数求模,matlab计算带有复数的函数,最后求复数函数的模,结果里面却有...
  10. 串口调试工具--SecureCRT的使用
  11. java word合并单元格_java使用freemarker模板导出word(带有合并单元格)文档
  12. Docker推送一个自制镜像到dockerhub
  13. FFMpeg AVPacket 之理解与掌握
  14. 数字逻辑实验|逻辑函数及其描述工具(Logisim)
  15. 基于JavaSwing和BeautyEye美化包实现的小型资源管理器
  16. Prime Ring Problem -- 第一个真正意义上的 搜索
  17. 用户注册——注册信息保存到数据库
  18. 【信息安全】-安全协议
  19. 抓包!抓包! HTTPS中间人抓包
  20. 药一点诊所管理软件_医院管理软件HIS

热门文章

  1. 学python需要什么基础-Python入门学习需要哪些条件?
  2. python编程案例教程-Python程序设计案例教程——从入门到机器学习(微课版)
  3. 会聊天到底有多重要?汽车语音识别大盘点
  4. h5 在线语音识别接口
  5. python中的classmethod_面试题:python 中 staticmethod 和 classmethod有什么区别
  6. android jar包 权限,Android系统启动执行jar程序
  7. windows openssl环境变量_run custom build command for `openssl-sys v0.9.55`
  8. 索尼高清影视技术学院参观观后感
  9. IIS 301重定向跳转
  10. 初中计算机ps教程,初中信息技术《认识Photoshop CS2的工作界面》教案