2018.12.08 codeforces 914D. Bash and a Tough Math Puzzle(线段树)
传送门
线段树辣鸡题。
题意简述:给出一个序列,支持修改其中一个数,以及在允许自行修改某个数的情况下询问区间[l,r][l,r][l,r]的gcdgcdgcd是否可能等于一个给定的数。
看完题就感觉是道线段树。
修改操作不谈了。
查询给人的第一感觉有点新奇。
但仔细分析就是sbsbsb操作了。
我们维护一个全局变量cntcntcnt来记录为了达到要求已经修改了几个数,如果cnt>1cnt>1cnt>1剪枝就行了。
代码:
#include<bits/stdc++.h>
#define ri register int
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (T[p].l+T[p].r>>1)
using namespace std;
const int N=5e5+5;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
int n,a[N],q,cnt;
struct Node{int l,r,g;}T[N<<2];
inline void pushup(int p){T[p].g=__gcd(T[lc].g,T[rc].g);}
inline void build(int p,int l,int r){
T[p].l=l,T[p].r=r;
if(l==r){T[p].g=a[l];return;}
build(lc,l,mid),build(rc,mid+1,r),pushup(p);
}
inline void update(int p,int k,int v){
if(T[p].l==T[p].r){T[p].g=v;return;}
if(k<=mid)update(lc,k,v);
else update(rc,k,v);
pushup(p);
}
inline void query(int p,int ql,int qr,int v){
if(cnt>=2)return;
if(ql<=T[p].l&&T[p].r<=qr){
if(T[p].g%v==0)return;
if(T[p].l==T[p].r){++cnt;return;}
if(T[lc].g%v&&T[rc].g%v){cnt+=2;return;}
T[lc].g%v?query(lc,ql,qr,v):query(rc,ql,qr,v);
return;
}
if(qr<=mid)return query(lc,ql,qr,v);
if(ql>mid)return query(rc,ql,qr,v);
query(lc,ql,mid,v),query(rc,mid+1,qr,v);
}
int main(){
n=read();
for(ri i=1;i<=n;++i)a[i]=read();
build(1,1,n);
for(ri tt=read(),l,r,x,op;tt;--tt){
op=read();
if(op==1)l=read(),r=read(),x=read(),cnt=0,query(1,l,r,x),puts(cnt<2?"YES":"NO");
else l=read(),x=read(),update(1,l,x);
}
return 0;
}
2018.12.08 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(线段树)
题目链接 \(Description\) 给定一个序列,两种操作:一是修改一个点的值:二是给一个区间\([l,r]\),问能否只修改一个数使得区间gcd为\(x\). \(Solution\) 想到能 ...
- [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 (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,这里不一定是准确值,如果在这个区间内改变 ...
随机推荐
- HTTP 基础
HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...
- linux ubuntu 18.04 无线网卡 rtl8821ce的安装
解压rtl8821ce.zip 修改makefile,在shell中输入pwd,查看当前文件的路径,之后在Makefile中查找export Topdir ?= /home/zzm/Download ...
- html5文件读取+按钮样式重置+文件内容预览
FileReader读取文件详细介绍请访问:http://www.cnblogs.com/xyyt/p/9066882.html FileReader提供了如下方法: readAsArrayBuffe ...
- java bean validation 参数验证
一.前言 二.几种解决方案 三.使用bean validation 自带的注解验证 四.自定义bean validation 注解验证 一.前言 在后台开发过程中,对参数的校验成为开发环境不可缺少的一 ...
- privilege instruction error
检查新建的回调函数是否用了__stdcall修饰
- ubuntu 安装Pangolin 过程
Pangolin 是一款开源的OPENGL显示库,可以用来视频显示.而且开发容易. 代码我们可以从Github 进行下载:https://github.com/zzx2GH/Pangolin.git ...
- 理解Linux中的shutdown、poweroff、halt和reboot命令
原文 http://os.51cto.com/art/201706/541525.htm 在本篇中,我们会向你解释 shutdown.poweroff.halt 以及 reboot 命令.我们会 ...
- vue-awesome-swiper轮播的使用
一.安装vue-awesome-swiper npm install vue-awesome-swiper --save 二.引入插件 main.js里面分别引入(记得有些电脑要引入样式) impor ...
- MYSLQ数据库 day 1
啥是SQL? 据库的组成部分,其中数据库管理系统可以接收一些命令,对数据文件进行添加.删除.修改.查询等操作.那么这些命令就是 SQL . SQL:(Structured Query Language ...
- Liunx Mkdir
linux mkdir命令: 创建目录 介绍:该命令创建指定的目录名,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录1语法: mkdir [-m] [-p] 目录 ...