传送门

线段树辣鸡题。

题意简述:给出一个序列,支持修改其中一个数,以及在允许自行修改某个数的情况下询问区间[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(线段树)的更多相关文章

  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. [Codeforces 914D] Bash and a Tough Math Puzzle

    [题目链接] https://codeforces.com/contest/914/problem/D [算法] 显然 , 当一个区间[l , r]中为d倍数的数的个数 <= 1 , 答案为Ye ...

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

    题目链接  Round #458 (Div. 1 + Div. 2, combined)  Problem D 题意  给定一个序列,两种询问:单点修改,询问某个区间能否通过改变最多一个数使得该区间的 ...

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

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

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

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

  7. 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 ...

  8. 914D Bash and a Tough Math Puzzle

    传送门 分析 用线段树维护区间gcd,每次查询找到第一个不是x倍数的点,如果这之后还有gcd不能被x整除的区间则这个区间不合法 代码 #include<iostream> #include ...

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

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

随机推荐

  1. http406错误

    The resource identified by this request is only capable of generating responses with characteristics ...

  2. OC 开发规范指南 - 个人见解写的很好

    纽约时报 移动团队 Objective-C 规范指南 这份规范指南概括了纽约时报 iOS 团队的代码约定. 介绍 关于这个编程语言的所有规范,如果这里没有写到,那就在苹果的文档里: • Objecti ...

  3. SQL Merge 语法 单表查询

    --项目中需要用到Merg语法,于是去网上查了资料,发现竟然都是多表查询,问题是我只有一张表,于是我纳闷了,后来我灵机一动,就搞定了!--表名:t_login(登录表)--字段:f_userName( ...

  4. AndroidManifest中注册application

    <application android:icon="@drawable/icon1" android:label="@string/app_name" ...

  5. ubuntu 安装Pangolin 过程

    Pangolin 是一款开源的OPENGL显示库,可以用来视频显示.而且开发容易. 代码我们可以从Github 进行下载:https://github.com/zzx2GH/Pangolin.git ...

  6. 9.13 h5日记

    9.13 面试题 为什么两个P出此案的效果不同,原因是 浏览器在解析第二个P的时候,因为字母没有空格,浏览器会认为这个单词没有打完,所以不会换行. 列表 ul ol dl li 1.无序列表 ul ( ...

  7. html标签二

    1.没有前后顺序的信息列表<ul> <li></li> <li></li></ul>2.有序列表 <ol>  < ...

  8. Architecture.SOLID-Principles

    SOLID Principles Reference 1. Single Responsibility http://en.wikipedia.org/wiki/Single_responsibili ...

  9. 洛谷3084 [USACO13OPEN]照片Photo

    原题链接 神仙\(DP\)啊... 题解请移步隔壁大佬的博客\(QAQ\) #include<cstdio> using namespace std; const int N = 2e5 ...

  10. LibreOJ 6004. 「网络流 24 题」圆桌聚餐 网络流版子题

    #6004. 「网络流 24 题」圆桌聚餐 内存限制:256 MiB时间限制:5000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数 ...