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区间能否去掉小于等于1个数,使gcd为x
分析:
线段树。
线段树二分。如果一边的gcd不是x,那么递归这一边,找到这个位置为止,计算这样的位置的个数。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
#include<set>
#include<vector>
#include<queue>
#include<map>
#define Root 1, n, 1
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define fi(s) freopen(s,"r",stdin);
#define fo(s) freopen(s,"w",stdout);
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; int gcd(int a,int b) {
return b == ? a : gcd(b, a % b);
} int g[N << ], G, cnt; void pushup(int rt) {
g[rt] = gcd(g[rt << ], g[rt << | ]);
}
void build(int l,int r,int rt) {
if (l == r) {
g[rt] = read(); return ;
}
int mid = (l + r) >> ;
build(lson), build(rson);
pushup(rt);
}
void update(int l,int r,int rt,int p,int v) {
if (l == r) {
g[rt] = v; return ;
}
int mid = (l + r) >> ;
if (p <= mid) update(lson, p, v);
else update(rson, p, v);
pushup(rt);
}
bool query(int l,int r,int rt,int L,int R) {
if (g[rt] % G == ) return true; // 如果区间gcd为G的倍数,那么这个区间合法
if (l == r) return (++cnt) <= ; // 否则递归下去,找到不合法的位置,计算有几个,大于1个不合法。
int mid = (l + r) >> ;
if (L > mid) query(rson, L, R);
else if (R <= mid) query(lson, L, R);
else return query(lson, L, R) && query(rson, L, R);
}
int main() {
int n = read();
build(Root);
int Q = read();
while (Q--) {
int opt = read();
if (opt == ) {
int l = read(), r = read(); G = read(); cnt = ;
puts(query(Root, l, r) ? "YES" : "NO");
}
else {
int p = read(), v = read();
update(Root, p, v);
}
}
return ;
}
CF 914 D. Bash and a Tough Math Puzzle的更多相关文章
- codeforces 914 D Bash and a Tough Math Puzzle
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #i ...
- 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 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- Codecraft-18 and Codeforces Round #458:D,Bash and a Tough Math Puzzle
题目传送门 题目大意:Bash喜欢对数列进行操作.第一种操作是询问l~r区间内的gcd值是否几乎为x,几乎为表示能否至多修改一个数达到.第二种操作是将ai修改为x.总共Q个询问,N个数. Soluti ...
- [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 线段树,区间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是否可能等于一个给定的数. 看完题就感觉是 ...
- Bash and a Tough Math Puzzle CodeForces - 914D (线段树二分)
大意:给定序列, 单点修改, 区间询问$[l,r]$内修改至多一个数后$gcd$能否为$x$ 这题比较有意思了, 要注意到询问等价于$[l,r]$内最多有1个数不为$x$的倍数 可以用线段树维护gcd ...
随机推荐
- PHP-------ajax返回值 返回JSON 数据
ajax返回值 返回JSON 数据 ajax返回值 有text JSON ajax返回值 返回JSON 数据 <title>无标题文档</title> <sc ...
- bagging 和boosting的概念和区别
1.先弄清楚模型融合中的投票的概念 分为软投票和硬投票,硬投票就是几个模型预测的哪一类最多,最终模型就预测那一类,在投票相同的情况下,投票结果会按照分类器的排序选择排在第一个的分类器结果.但硬投票有个 ...
- 【转】Java中关于WeakReference和WeakHashMap的理解
新美大的10月11日的笔试中有一道选择题,让选择函数返回结果,代码如下: private static String test(){ String a = new String("a&quo ...
- VC++和C语言中常见数据类型转换为字符串的方法
1.短整型(int) itoa(i,temp,10);///将i转换为字符串放入temp中,最后一个数字表示十进制 itoa(i,temp,2); ///按二进制方式转换 2.长整型(long) lt ...
- 【题解】洛谷P1896 [SCOI2005] 互不侵犯(状压DP)
洛谷P1896:https://www.luogu.org/problemnew/show/P1896 前言 这是一道状压DP的经典题 原来已经做过了 但是快要NOIP 复习一波 关于一些位运算的知识 ...
- hdu 1520 Anniversary party(第一道树形dp)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...
- 时钟晶振32.768KHz为什么是15分频?
实时时钟晶振为什么选择是32768Hz的晶振,在百度上搜索的话大部分的答案都是说2的15次方是32768,使用这个频率的晶振,人们可以很容易的通过分频电路得到1Hz的计时脉冲.但是话有说回来了,2的整 ...
- jquery实现页面图片轮播
1.创建一个html页面 <!DOCTYPE html><html lang="en"><head> <meta charset=&quo ...
- DML-修改
一, 修改单表的记录 语法: update 表名 set 字段=值[where 筛选条件] 二,修改多表 update 表名 别名 inner/left/rigth join 表二 on 连接条件 s ...
- sql中table用法
for c in (select column_value from table(f_split(V_FileID, ','))) loop --若没有填写资格开始结束时间,则填入 select co ...