CF914D Bash and a Tough Math Puzzle 线段树+gcd??奇怪而精妙
嗯~~,好题。。。
用线段树维护区间gcd,按如下法则递归:(记题目中猜测的那个数为x,改动次数为tot)
1.若子区间的gcd是x的倍数,不递归;
2.若子区间的gcd不是x的倍数,且没有递归到叶子结点,那么向下递归
3.若递归到叶子结点,说明这个数需要改动,++tot
4.若在任意时刻有tot>1,则直接return(不符题意)
#include<cstdio>
#include<iostream>
#define ll long long
#define R register int
#define pc(x) putchar(x)
#define ls (tr<<1)
#define rs (tr<<1|1)
const int M=;
using namespace std;
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
}
int n,m,tot;
int sum[M<<];
inline int gcd(int a,int b) {return b?gcd(b,a%b):a;}
inline void build(int tr,int l,int r) {
if(l==r) {sum[tr]=g(); return ;} R md=(l+r)>>;
build(ls,l,md),build(rs,md+,r); sum[tr]=gcd(sum[ls],sum[rs]);
}
inline void update(int tr,int l,int r,int pos,int inc) {
if(l==r) {sum[tr]=inc; return ;} R md=(l+r)>>;
if(pos<=md) update(ls,l,md,pos,inc); else update(rs,md+,r,pos,inc);
sum[tr]=gcd(sum[ls],sum[rs]);
}
inline void query(int tr,int l,int r,int LL,int RR,int mod) {
if(tot>) return ; if(l==r) { ++tot; return ;} R md=(l+r)>>;
if(LL<=md&&sum[ls]%mod) query(ls,l,md,LL,RR,mod); if(RR>md&&sum[rs]%mod) query(rs,md+,r,LL,RR,mod);
}
signed main() {
n=g(); build(,,n); m=g();
for(R i=;i<=m;++i) {
R k=g(),l=g(),r=g(),mod;
if(k&) {
mod=g(),tot=,query(,,n,l,r,mod);
tot>?(pc('N'),pc('O')):(pc('Y'),pc('E'),pc('S')); pc('\n');
} else update(,,n,l,r);
}
}
2019.04.19
CF914D Bash and a Tough Math Puzzle 线段树+gcd??奇怪而精妙的更多相关文章
- cf914D. Bash and a Tough Math Puzzle(线段树)
题意 题目链接 Sol 直接在线段树上二分 当左右儿子中的一个不是\(x\)的倍数就继续递归 由于最多递归到一个叶子节点,所以复杂度是对的 开始时在纠结如果一段区间全是\(x\)的两倍是不是需要特判, ...
- 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\) 想到能 ...
- CodeForces 914DBash and a Tough Math Puzzle(线段树的骚操作)
D. Bash and a Tough Math Puzzle time limit per test 2.5 seconds memory limit per test 256 megabytes ...
- [CF914D]Bash and a Tough Math Puzzle
给定一个数列$a_1,a_2,...,a_n$,支持两种操作 1 l r x,猜测数列中[l,r]位置上的数的最大公约数$x$,判断这个猜测是否是接近正确的.如果我们可以在数列[l,r]位置中改动至多 ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- 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\), ...
- 2018.12.08 codeforces 914D. Bash and a Tough Math Puzzle(线段树)
传送门 线段树辣鸡题. 题意简述:给出一个序列,支持修改其中一个数,以及在允许自行修改某个数的情况下询问区间[l,r][l,r][l,r]的gcdgcdgcd是否可能等于一个给定的数. 看完题就感觉是 ...
随机推荐
- Java多线程编程核心 - 对象及变量的并发访问
1.什么是“线程安全”与“非线程安全”? “非线程安全”会在多个线程对同一对象总的实例变量进行并发访问时发生,产生的后果是“脏读”,也就是取到的数据其实是被更改过的. “线程安全”是以获得的实例变量的 ...
- marlab之rangesearch()函数
rangesearch()函数:查找某个点周围指定距离内的所有邻居. rng('default') % for reproducibility X = randn(,); Y = randn(,); ...
- logback备注
<?xmlversion="1.0"encoding="UTF-8"?> <!-- <configuration>包含的属性 sc ...
- xpath normalize-sapce 函数的Java实现
normalize-space函数实现的功能是:删除字符串前后空格,中间的空格有多个只保留一个. 1. 用Java正则表达式 public static String normalizeSpace(S ...
- ISO文件怎么安装
iso是光盘映像文件,打开iso文件有几种方法:1.使用光盘映像工具软件或者虚拟光驱才能打开iso文件.像软碟通(UItraISO)就是一个很好的光盘映像工具软件,使用它就可以打开iso文件,提取里面 ...
- [acm]HDOJ 2059 龟兔赛跑
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=2059 起点和终点,共n+2个点,n+2个状态,简单DP即可. //11512698 2014-08- ...
- phpstorm 2016.3.2 的最新破解方法(截止2017-2-20)
最新更新 ,http://idea.imsxm.com 这个地址是亲测可用的,针对最新的phpstorm 2016.3.2的版本.使用方式和下面一致,选择license server.然后复制http ...
- 【LeetCode】031. Next Permutation
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- POJ2182:Lost Cows
浅谈线段树和树状数组:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:http://poj.org/problem?id=2182 线段树,倒着确 ...
- libvirtError: 无效参数:could not find capabilities for domaintype=kvm
libvirtError: 无效参数:could not find capabilities for domaintype=kvm 编辑/etc/nova/nova.conf 在[libvirt] 添 ...