[Codeforces 914D] Bash and a Tough Math Puzzle
[题目链接]
https://codeforces.com/contest/914/problem/D
[算法]
显然 , 当一个区间[l , r]中为d倍数的数的个数 <= 1 , 答案为Yes , 否则为No
线段树简单维护即可 , 详见代码 , 时间复杂度 : O(NlogN ^ 2)
[代码]
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + ; int n , m;
int val[MAXN];
int cnt; struct Segment_Tree
{
struct Node
{
int l , r;
int value;
} a[MAXN << ];
inline int gcd(int x , int y)
{
if (y == ) return x;
else return gcd(y , x % y);
}
inline void update(int x)
{
a[x].value = gcd(a[x << ].value , a[x << | ].value);
}
inline void build(int index , int l , int r)
{
a[index].l = l , a[index].r = r;
if (l == r)
{
a[index].value = val[l];
return;
}
int mid = (l + r) >> ;
build(index << , l , mid);
build(index << | , mid + , r);
update(index);
}
inline void modify(int index , int x , int y)
{
if (a[index].l == a[index].r)
{
a[index].value = y;
return;
} else
{
int mid = (a[index].l + a[index].r) >> ;
if (mid >= x) modify(index << , x , y);
else modify(index << | , x , y);
update(index);
}
}
inline void getans(int index , int l , int r , int d)
{
if (cnt > ) return;
int mid = (a[index].l + a[index].r) >> ;
if (a[index].l == l && a[index].r == r)
{
if (l == r)
{
if (a[index].value % d)
++cnt;
return;
}
if ((a[index << ].value % d) && (a[index << | ].value % d))
{
cnt += ;
return;
} else if (a[index << ].value % d) getans(index << , l , mid , d);
else if (a[index << | ].value % d) getans(index << | , mid + , r , d);
} else
{
if (mid >= r) getans(index << , l , r , d);
else if (mid + <= l) getans(index << | , l , r , d);
else
{
getans(index << , l , mid , d);
getans(index << | , mid + , r , d);
}
}
}
} SGT; template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
} int main()
{ read(n);
for (int i = ; i <= n; i++) read(val[i]);
SGT.build( , , n);
read(m);
while (m--)
{
int type;
read(type);
if (type == )
{
int l , r , x;
read(l); read(r); read(x);
cnt = ;
SGT.getans( , l , r , x);
if (cnt > ) printf("NO\n");
else printf("YES\n");
} else
{
int x , y;
read(x); read(y);
SGT.modify( , x , y);
}
} return ;
}
[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\) 想到能 ...
- 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 题意 给定一个序列,两种询问:单点修改,询问某个区间能否通过改变最多一个数使得该区间的 ...
- 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,这里不一定是准确值,如果在这个区间内改变 ...
- 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\), ...
- Bash and a Tough Math Puzzle CodeForces - 914D (线段树二分)
大意:给定序列, 单点修改, 区间询问$[l,r]$内修改至多一个数后$gcd$能否为$x$ 这题比较有意思了, 要注意到询问等价于$[l,r]$内最多有1个数不为$x$的倍数 可以用线段树维护gcd ...
随机推荐
- (2)git本地生成SSH关联github
1.安装git 2.打开 Git Bash 输入ssh ,查看是否安装了ssh 这个界面是安装了的意思 3.生成ssh 输入ssh-keygen -t rsa 指令, 再连续按三次回车 会生成两个文件 ...
- Sql server 各版本下载链接
Sql Server 2008 R2 链接: https://pan.baidu.com/s/11WPcrUL64QT0zT0_9VTb8Q 提取码: 665l 密钥:JD8Y6-MQG69-P9M8 ...
- spring解决乱码
spring提供的工具类解决乱码问题 在web.xml配置中添加如下代码: <!--乱码处理--> <filter> <filter-name>encodingFi ...
- Jetty插件实现热部署(开发时修改文件自动重启Jetty)
在pom.xml文件中配置Jetty插件的参数:scanIntervalSeconds <plugin> <groupId>org.mortbay.jetty</grou ...
- 改动Xmodem/Zmodem上传下载路径
SecureCRT能够使用Xmodem/Zmodem方便的上传和下载文件. 在Session ptions =>Xmodem/Zmodem => Directories中设置 选项=& ...
- Solidworks如何绘制标准螺纹线
1 绘制螺旋线,螺距为0.5mm,圈数为15,起始角度为0°. 2
- 互斥锁和条件变量(pthread)相关函数
互斥锁 #include <pthread.h> // 若成功返回0,出错返回正的Exxx值 // mptr通常被初始化为PTHREAD_MUTEX_INITIALIZER int pth ...
- dsp端编译异常之max和min未定义
(1)在函数之前 声明__stdcall 时 在linux 端或dsp端 linux 之前的加上宏定义 __stdcall是MS的编译器使用的只需要#define __stdcall定义一个宏就可以 ...
- android控件之间事件传递
public boolean dispatchTouchEvent(MotionEvent ev){} 用于事件的分发.Android中全部的事件都必须经过这种方法的分发.然后决定是自身消费当前事件还 ...
- Java中的文件上传和下载
文件上传原理: 早期的文件上传机制: 在TCP/IP中.最早出现的文件上传机制是FTP.他是将文件由客户端发送到服务器的标准机制. jsp中的文件上传机制: 在jsp编程中不能使用FTP的方法来上传文 ...