[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 ...
随机推荐
- express---express-session axios
express---express-session axios 使用axios访问后台获取session中的属性值为undefined 在main.js中导入axios import axios fr ...
- linux系统(ubuntu14.04)安装mentohust完毕校园网锐捷认证
近来升级电脑又一次做了系统.再次面临这linux系统下的各种校园网上网限制. 我在这里採用了mentohust来完毕锐捷认证. 这里我们选择Mentohust取代锐捷. Mentohust 是由华中科 ...
- bsp开发之驱动开发
驱动程序是可以管理虚拟设备或者物理设备,协议,服务等得软件模块,操作系统仅仅有通过驱动程序才干訪问硬件.针对windows ce开发设备驱动.就是通过platform builder创建一个新的平台, ...
- Oracle db中禁止使用sqlplus的方法
先记录下来: How to Disable a SQL*Plus Connection for a User (文档 ID 124121.1)
- YII RBAC基于角色的访问控制
基于角色的访问控制( Role-Based Access Control ),是一种简单的而又强大的集中访问控制.基于Yii Framework 的 authManager 组件实现了分等级的 RBA ...
- 高速查询hive数据仓库表中的总条数
Author: kwu 高速查询hive数据仓库中的条数.在查询hive表的条数,通常使用count(*).可是数据量大的时候,mr跑count(*)往往须要几分钟的时间. 1.传统方式获得总条数例如 ...
- ios自动生成对象类,提高开发速率
#import "autoGenerationFileUtility.h" @implementation autoGenerationFileUtility - (void)cr ...
- 凝视转换(c转换为c++)
C语言凝视->C++凝视即/*xxxxx*/->//xxxxx 在转换凝视前我们先了解一个概念:什么是有限状态机? 有限状态机FSM是软件上经常使用的一种处理方法,它把复杂的控制逻辑分解成 ...
- Codeforces 558C Amr and Chemistry
题意: n个数.每次能够选一个数 让其 *=2 或者 /=2 问至少操作多少次使得全部数相等. 思路: 对于每一个数,计算出这个数能够变成哪些数,以及变成那个数的最小步数,用两个数组保存 cnt[i] ...
- 对交换机VLAN及各种端口类型的理解
每学习一种技术时,我们往往需要去了解why,即这个技术是为解决什么问题而出现的. VLAN全称为Virtual Local Area Network,即虚拟局域网,是逻辑上的一种划分.一般来说,如果交 ...