Codeforces.914D.Bash and a Tough Math Puzzle(线段树)
\(Description\)
给定一个序列,两种操作:一是修改一个点的值;二是给一个区间\([l,r]\),问能否只修改一个数使得区间gcd为\(x\)。
\(Solution\)
想到能维护区间gcd就很简单了。
对于区间查询,两个子区间只能有一个区间的gcd不整除\(x\),再递归这个子区间。
因为这样递归至多递归到两个叶子,所以复杂度OK。
至于线段树维护gcd...这是1个log的,大概是因为。。
你辗转相处一次
你的数字会/2
你得按顺序做gcd
全部和答案去做gcd
——by dls
似乎和BZOJ上一道题很像?
//280ms 8000KB
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
const int N=5e5+5;
int read();
struct Segment_Tree
{
#define S N<<2
#define ls rt<<1
#define rs rt<<1|1
#define lson l,m,ls
#define rson m+1,r,rs
int g[S];
#undef S
#define Update(rt) g[rt]=Gcd(g[ls],g[rs])
int Gcd(int x,int y)
{
return y?Gcd(y,x%y):x;
}
void Build(int l,int r,int rt)
{
if(l==r) {g[rt]=read(); return;}
int m=l+r>>1; Build(lson), Build(rson);
Update(rt);
}
void Modify(int l,int r,int rt,int p,int v)
{
if(l==r) {g[rt]=v; return;}
int m=l+r>>1;
if(p<=m) Modify(lson,p,v);
else Modify(rson,p,v);
Update(rt);
}
bool Query(int l,int r,int rt,int L,int R,int x,int &K)
{
if(!(g[rt]%x)) return 1;
if(l==r) return --K>0;
int m=l+r>>1;
if(L<=m)
if(m<R) return Query(lson,L,R,x,K)&&Query(rson,L,R,x,K);
else return Query(lson,L,R,x,K);
return Query(rson,L,R,x,K);
}
}T;
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
int main()
{
#define S 1,n,1
int n=read();
T.Build(S);
for(int Q=read(),l,r,tmp; Q--; )
if(read()==1) l=read(),r=read(),puts(T.Query(S,l,r,read(),tmp=2)?"YES":"NO");
else l=read(),T.Modify(S,l,read());
return 0;
}
Codeforces.914D.Bash and a Tough Math Puzzle(线段树)的更多相关文章
- Codeforces 914D - Bash and a Tough Math Puzzle 线段树,区间GCD
题意: 两个操作, 单点修改 询问一段区间是否能在至多一次修改后,使得区间$GCD$等于$X$ 题解: 正确思路; 线段树维护区间$GCD$,查询$GCD$的时候记录一共访问了多少个$GCD$不被X整 ...
- [Codeforces 914D] Bash and a Tough Math Puzzle
[题目链接] https://codeforces.com/contest/914/problem/D [算法] 显然 , 当一个区间[l , r]中为d倍数的数的个数 <= 1 , 答案为Ye ...
- 2018.12.08 codeforces 914D. Bash and a Tough Math Puzzle(线段树)
传送门 线段树辣鸡题. 题意简述:给出一个序列,支持修改其中一个数,以及在允许自行修改某个数的情况下询问区间[l,r][l,r][l,r]的gcdgcdgcd是否可能等于一个给定的数. 看完题就感觉是 ...
- Codeforces 914D Bash and a Tough Math Puzzle (ZKW线段树)
题目链接 Round #458 (Div. 1 + Div. 2, combined) Problem D 题意 给定一个序列,两种询问:单点修改,询问某个区间能否通过改变最多一个数使得该区间的 ...
- cf914D. Bash and a Tough Math Puzzle(线段树)
题意 题目链接 Sol 直接在线段树上二分 当左右儿子中的一个不是\(x\)的倍数就继续递归 由于最多递归到一个叶子节点,所以复杂度是对的 开始时在纠结如果一段区间全是\(x\)的两倍是不是需要特判, ...
- CF914D Bash and a Tough Math Puzzle 线段树+gcd??奇怪而精妙
嗯~~,好题... 用线段树维护区间gcd,按如下法则递归:(记题目中猜测的那个数为x,改动次数为tot) 1.若子区间的gcd是x的倍数,不递归: 2.若子区间的gcd是x的倍数,且没有递归到叶子结 ...
- 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 ...
- 914D Bash and a Tough Math Puzzle
传送门 分析 用线段树维护区间gcd,每次查询找到第一个不是x倍数的点,如果这之后还有gcd不能被x整除的区间则这个区间不合法 代码 #include<iostream> #include ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
随机推荐
- Linux驱动开发必看详解神秘内核(完全转载)
Linux驱动开发必看详解神秘内核 完全转载-链接:http://blog.chinaunix.net/uid-21356596-id-1827434.html IT168 技术文档]在开始步入L ...
- eclipse 反编译
Eclipse Class Decompiler安装此插件,可以编译源代码且调试
- mysql通过centos本地命令行还原数据库出现乱码问题
将sql文件上传到centos系统中,还原mysql数据库,发现是乱码 mysql -h10.11.8.62 -uroot -p dbtest </data/dbsql/dbtest.sql 数 ...
- Web 中调用FreeSWITCH的Portal GUI配置记录
具体设定步骤: ①加载 mod_xml_rpc 模块:load mod_xml_rpc 若想让该模块在FreeSWITCH启动时而自动加载,在conf/autoload_configs/modules ...
- Spark学习之第一个程序打包、提交任务到集群
1.免秘钥登录配置: ssh-keygen cd .ssh touch authorized_keys cat id_rsa.pub > authorized_keys chmod 600 au ...
- iOS 8 WKWebView 知识点
首先看看这篇文章,写得很好:http://nshipster.cn/wkwebkit/ 再推荐去看看 iOS_8_by_Tutorials 这本书里的 WKWebView相关章节! 我这里说下自己的简 ...
- CSS伪元素before、after妙用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Innodb引擎下mysql自身配置优化
1.简单介绍InnoDB给MySQL提供了具有提交,回滚和崩溃恢复能力的事务安全(ACID兼容)存储引擎.InnoDB锁定在行级并且也在SELECT语句提供一个Oracle风格一致的非锁定读.这些特色 ...
- jquery----用户密码验证
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 《转》利用cxf实现webservice
首先下载cxf包,目前最新的版本是apache-cxf-2.1.,下栽地址http://cxf.apache.org/download.html. 1. 首先新建一个web工程CxfService,倒 ...