codeforces 914 D 线段树+数学
题意
给出一个长度为\(n\)的数列\(a\),两种询问,第一种给出三个数\(l,r,x\),区间\([l,r]\)的\(gcd\)值是否和\(x\)相似,若最多改变区间\([l,r]\)中的一个数使区间\([l,r]\)的\(gcd\)值等于\(x\),则相似,第二种给出两个数\(i,y\),将\(a[i]\)变为\(y\)。
分析
建一个线段树维护区间\(gcd\),这个线段树非常好写,因为是单点修改,所以不需要tag数组和pushdown,查询的时候用一个变量\(cnt\),记录区间\([l,r]\)中有多少个数不是\(x\)的倍数,\(cnt>1\)时直接跳出。
Code
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=1e9;
const int mod=1e9+7;
const int maxn=5e5+10;
int n,a[maxn];
int ans[maxn*5];
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
void pushup(int p){
ans[p]=gcd(ans[p<<1],ans[p<<1|1]);
}
void build(int l,int r,int p){
if(l==r){
ans[p]=a[l];
return;
}
int mid=l+r>>1;
build(l,mid,p<<1);
build(mid+1,r,p<<1|1);
pushup(p);
}
void update(int dl,int dr,int l,int r,int p,int k){
if(l>=dl&&r<=dr){
ans[p]=k;
return;
}
int mid=l+r>>1;
if(dl<=mid) update(dl,dr,l,mid,p<<1,k);
if(dr>mid) update(dl,dr,mid+1,r,p<<1|1,k);
pushup(p);
}
int cnt;
bool query(int dl,int dr,int l,int r,int p,int x){
if(l>=dl&&r<=dr){
if(ans[p]%x==0) {//这个区间的gcd是x的倍数时直接返回true,否则继续向下查询
return true;
}
if(l==r){//这个数不是x的倍数,cnt++
cnt++;
if(cnt>1) return false;
return true;
}
}
int mid=l+r>>1;
if(dl<=mid) if(!query(dl,dr,l,mid,p<<1,x)) return false;
if(dr>mid) if(!query(dl,dr,mid+1,r,p<<1|1,x)) return false;
return true;
}
int main(){
//ios::sync_with_stdio(false);
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
build(1,n,1);
int q;
scanf("%d",&q);
while(q--){
int op,l,r,x,i,y;
scanf("%d",&op);
if(op==1){
scanf("%d%d%d",&l,&r,&x);
cnt=0;
if(query(l,r,1,n,1,x)){
puts("YES");
}else{
puts("NO");
}
}else{
scanf("%d%d",&i,&y);
update(i,i,1,n,1,y);
}
}
return 0;
}
codeforces 914 D 线段树+数学的更多相关文章
- Vasya and a Tree CodeForces - 1076E(线段树+dfs)
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...
- Codeforces 787D. Legacy 线段树建模+最短路
D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)
Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...
- Sereja and Brackets CodeForces - 380C (线段树+分治思路)
Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...
- CodeForces 91B Queue (线段树,区间最值)
http://codeforces.com/problemset/problem/91/B B. Queue time limit per test: 2 seconds memory limit p ...
- 【线段树/数学/扩展欧几里得】 Bzoj 3913:奇数国
Description 在一片美丽的大陆上有100000个国家,记为1到100000.这里经济发达,有数不尽的账房,并且每个国家有一个银行.某大公司的领袖在这100000个银行开户时都存了3大洋,他惜 ...
- Codeforces 343D WaterTree - 线段树, DFS序
Description Translated by @Nishikino_Maki from Luogu 行吧是我翻的 Mad scientist Mike has constructed a roo ...
- codeforces 787D - Legacy 线段树优化建图,最短路
题意: 有n个点,q个询问, 每次询问有一种操作. 操作1:u→[l,r](即u到l,l+1,l+2,...,r距离均为w)的距离为w: 操作2:[l,r]→u的距离为w 操作3:u到v的距离为w 最 ...
- 【BZOJ3813】【清华集训2014】奇数国 线段树 数学
题目描述 给你一个长度为\(n\)的数列,第\(i\)个数为\(a_i\).每个数的质因子都只有前\(60\)个质数.有\(q\)个询问,每次给你\(l,r\),求\(\varphi(\prod_{i ...
随机推荐
- Ubuntu安装 和 python开发
在ubuntu上安装pycharm 可以好几种下载办法 1.pycharm之linux版本下载地址: https://download.jetbrains.8686c.com/python/pycha ...
- 水晶报表自定义纸张大小打印 (Crystal Report Print with custom paper size)
System.Drawing.Printing.PrintDocument doc = new PrintDocument(); doc.PrinterSettings.PrinterName = & ...
- [翻译] UIGlossyButton
UIGlossyButton https://github.com/waterlou/UIGlossyButton Feature create standard iPhone buttons wit ...
- UNIX高级环境编程(2)FIle I/O - 原子操作、共享文件描述符和I/O控制函数
引言: 本篇通过对open函数的讨论,引入原子操作,多进程通信(共享文件描述符)和内核相关的数据结构. 还会讨论集中常见的文件IO控制函数,包括: dup和dup2 sync,fsync和fdatas ...
- 多台服务器共享session问题
在现在的大型网站中,如何实现多台服务器中的session数据共享呢 当使用多台服务器架设成集群之后,我们通过负载均衡的方式,同一个用户(或者ip)访问时被分配到不同的服务器上,假设在A服务器登录,如果 ...
- Atom 绝赞插件
文件图标: file-icons 根据不同文件后缀名显示不同类型图标 标签栏根据不同文件格式显示色彩: filetype-color 在标签栏不同格式文件显示不同的颜色的标题,支持二度设置. 小地图: ...
- mysql常见问题总结
061 如何删除表? 答案:运行命令 drop table table_name; 062 创建索引 对于查询占主要的应用来说,索引显得尤为重要.很多时候性能问题很简单的就是因为我们忘了添加索引而造成 ...
- 囧啊!!时间戳转化为时间出错php
最近写了一个api,测试也没发现啥问题.可是上线之后发现有时api的返回结果不正确.为什么呢? 调我接口的同学给了两个调用示例,理论上两个的结果应该一致,实际结果却不一致. api调用带了一个时间戳参 ...
- java Math数学工具及Random随机函数
Math类包含用于执行基本数学运算的方法,如绝对值.对数.平方根和三角函数.它是一个final类,其中定义的都是一些常量和静 态方法.常用方法如下:public static double sqrt( ...
- 【MySQL学习杂记】 2017年7月13日
1. 关于分组 当select使用groupby语法时,select返回字段集合里面除去 <使用了聚合函数的字段>.<不包含在 group by 子句的字段> 的其他字段,这些 ...