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 lrx — Bash guesses that the gcd of the range [l, r] is x. Report if this guess is almost correct.
  • 2 iy — 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 lrx (1 ≤ l ≤ r ≤ n, 1 ≤ x ≤ 109).
  • 2 iy (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
Note

In the first sample, the array initially is {2, 6, 3}.

For query 1, the first two numbers already have their gcd as 2.

For query 2, we can achieve a gcd of 3 by changing the first element of the array to 3. Note that the changes made during queries of type 1are temporary and do not get reflected in the array.

After query 3, the array is now {9, 6, 3}.

For query 4, no matter which element you change, you cannot get the gcd of the range to be 2.

题意:给出一段序列,两个操作

操作1 给出l,r,x

求区间l-r的gcd,如果至多能改掉区间内的一个数,使gcd是x的倍数,那么输出YES,否则输出NO

操作2 给出pos,x

将序列中pos位置上的数字改为x

题解:我一开始看到这道题,没看到可以修改一个数,然后觉得是一道智障线段树,十分钟不到就写完了,结果非常GG地发现被题意杀了,完了,不会做

后面想了一下,对于返回的一个块,如果他的gcd不是x的倍数,那么这个区间肯定是要改数的,如果这样的区间有两个及以上,那么肯定输出NO

但是我们没法保证如果只有一个块的gcd非x的倍数时,这个块一定只用改一个值

再来考虑,每个块肯定会被分成左块和右块,如果左右两块都不是x的倍数就又GG了

如果只有一个块不是x的倍数,那么就按照上面的思路继续二分这个块

直到二分到只剩下一个数字,此时肯定输出yes

然后就搞定啦~其实还是不难的~

代码如下

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson root<<1
#define rson root<<1|1
using namespace std; int gcd(int a,int b)
{
if(b>a)
{
swap(a,b);
}
if(b)
{
return gcd(b,a%b);
}
else
{
return a;
}
} int nowson,x,cnt,n,m; struct node
{
int l,r,g;
}tr[]; void push(int root)
{
tr[root].g=gcd(tr[rson].g,tr[lson].g);
} void build(int root,int l,int r)
{
if(l==r)
{
tr[root].l=l;
tr[root].r=r;
scanf("%d",&tr[root].g);
return ;
}
int mid=(l+r)>>;
tr[root].l=l;
tr[root].r=r;
build(lson,l,mid);
build(rson,mid+,r);
push(root);
} void update(int root,int pos,int v)
{
if(tr[root].l==pos&&tr[root].r==pos)
{
tr[root].g=v;
return ;
}
int mid=(tr[root].l+tr[root].r)>>;
if(mid>=pos)
{
update(lson,pos,v);
}
else
{
update(rson,pos,v);
}
push(root);
} int query(int root,int l,int r)
{
if(tr[root].l==l&&tr[root].r==r)
{
if(tr[root].g%x!=)
{
cnt--;
nowson=root;
}
return tr[root].g;
}
int mid=(tr[root].l+tr[root].r)>>;
if(l>mid)
{
return query(rson,l,r);
}
else
{
if(r<=mid)
{
return query(lson,l,r);
}
else
{
return gcd(query(lson,l,mid),query(rson,mid+,r));
}
}
} int check(int root)
{
if(tr[root].l==tr[root].r)
{
return ;
}
if(tr[lson].g%x!=&&tr[rson].g%x!=)
{
return ;
}
if(tr[lson].g%x==)
{
return check(rson);
}
else
{
return check(lson);
}
} int main()
{
int n,m;
scanf("%d",&n);
build(,,n);
scanf("%d",&m);
for(int i=;i<=m;i++)
{
int kd,l,r;
scanf("%d",&kd);
if(kd==)
{
cnt=;
scanf("%d%d%d",&l,&r,&x);
int tmp=query(,l,r);
if(x==tmp)
{
puts("YES");
}
else
{
if((!cnt)&&check(nowson))
{
puts("YES");
}
else
{
puts("NO");
}
}
}
else
{
scanf("%d%d",&l,&r);
update(,l,r);
}
}
}

CodeForces 914DBash and a Tough Math Puzzle(线段树的骚操作)的更多相关文章

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

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

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

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

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

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

  4. CF914D Bash and a Tough Math Puzzle 线段树+gcd??奇怪而精妙

    嗯~~,好题... 用线段树维护区间gcd,按如下法则递归:(记题目中猜测的那个数为x,改动次数为tot) 1.若子区间的gcd是x的倍数,不递归: 2.若子区间的gcd是x的倍数,且没有递归到叶子结 ...

  5. CodeForces 620E New Year Tree(线段树的骚操作第二弹)

    The New Year holidays are over, but Resha doesn't want to throw away the New Year tree. He invited h ...

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

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

  7. 2018.12.08 codeforces 914D. Bash and a Tough Math Puzzle(线段树)

    传送门 线段树辣鸡题. 题意简述:给出一个序列,支持修改其中一个数,以及在允许自行修改某个数的情况下询问区间[l,r][l,r][l,r]的gcdgcdgcd是否可能等于一个给定的数. 看完题就感觉是 ...

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

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

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

随机推荐

  1. Django+xadmin打造在线教育平台(八)

    十一.用户信息 11.1.个人信息展示 (1)新建‘usercenter-bae.html’当母板 {% load staticfiles %} <!DOCTYPE html> <h ...

  2. java之内存分布图

    前言 不关我们是创建基本数据类型的变量还是引用数据类型的变量,jvm都会通过内存分布去编译和运行程序. 内存一般分为栈区.堆区.方法区(方法区里面包含常量池) 栈区一般存放变量(局部变量).方法的参数 ...

  3. [日常] Codeforces Round #440 Div.2 大力翻车实况

    上次打了一发ABC然后大力翻车...上午考试又停电+Unrated令人非常滑稽...下午终于到了CF比赛... 赛前大力安利了一发然后拉了老白/ $ljm$ / $wcx$ 一起打, 然后搞了个 TI ...

  4. JavaScript(第三十三天)【总结:封装基础前端框架】

    源码地址:https://github.com/whisper540/Base

  5. 第二次作业之微信小程序

    2.1 介绍产品相关信息 你选择的产品是? 微信小程序 为什么选择该产品作为分析? 在等待了1年多以后,小程序终于在今年初上线,即速应用在H5领域的累计,便承接在小程序上.8月7日,即速应用的用户微信 ...

  6. alpha-咸鱼冲刺day2-紫仪

    总汇链接 一,合照 emmmmm.自然是没有的. 二,项目燃尽图 三,项目进展 今天并没有什么进展,弄了好久好像也只研究出怎么把JS的功能块插入进去.html的信息提交这些还不知道要怎么弄. 四,问题 ...

  7. Linux下ftp和ssh详解

    学习了几天Linux下ftp和ssh的搭建和使用,故记录一下.学习ftp和ssh的主要目的是为了连接远程主机,并且进行文件传输.废话不多说,直接开讲! ftp服务器 1. 环境搭建 本人的系统是Arc ...

  8. HTTP协议形象展现

    关于http协议:我们分成几个模块说: http协议: HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统. HTTP协议的主要特点可概括如下: 1.支持客户 ...

  9. Twisted 延迟调用

    延迟(defer)是twisted框架中实现异步的编程体系,使程序设计可以采用事件驱动的机制 1.基本使用 defer可以看作一个管理回调函数的对象,可以向该对象添加需要的回调函数同时也可以指定该组函 ...

  10. 大神都在看的RxSwift 的完全入坑手册

    大神都在看的RxSwift 的完全入坑手册 2015-09-24 18:25 CallMeWhy callmewhy 字号:T | T 我主要是通过项目里的 Rx.playground 进行学习和了解 ...