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 ...
随机推荐
- NSArray排序方法讲解
NSArray排序方法讲解 给数组排序有着多种方式 最麻烦的是sortedArrayUsingSelector:,其次是sortedArrayUsingDescriptors:,最容易使用的就是sor ...
- 重置 Winsock:初始化计算机网络环境
初始化网络环境,以解决由于软件冲突.病毒原因造成的参数错误问题(复杂网络环境下慎用).批处理代码: netsh winhttp reset proxy netsh winhttp reset trac ...
- shell命令工作总结
shell命令工作总结: 1.sed命令:1.1.将文本input.txt中含有”姓名”字符串的行中的谢朝辉替换成扎巴依:sed -e '/姓名/s/谢朝辉/扎巴依/g' input.txt1.2.将 ...
- oracle监听,数据库,oracle服务器的启动和停用
一.oracle监听,数据库,oracle服务器的启动和停用 使用root用户登录:切换用户su – root/root操作系统,一般只有root用户才有权限去操作系统中安装的oracle数据库服务器 ...
- 根据需求设计类并且画UML类图练习
题目如下: // 打车时,可以打专车或者快车.任何车都有车牌号和名称// 不同车价格不同,快车每公里1元,专车每公里2元// 行程开始时,显示车辆信息// 行程结束时,显示打车金额(假定行程就5公里) ...
- 使用js插件进行设备检测
一.分析新浪网是怎么做的 如新浪网有两种版本,一种是pc版,存放在www.sina.com.cn这个服务器上:另外一种是手机版,存放在www.sina.cn这个服务器上 原理是当用户输入网址www ...
- JavaScript组合继承的一点思考
今天看<JavaScript高级程序设计>一书中关于组合继承模式时.书上有这么一个Demo程序: <html> <head> </head> <b ...
- Geeks : Kruskal’s Minimum Spanning Tree Algorithm 最小生成树
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- HDU 5550 - Game Rooms(DP + 前缀和预处理)
链接: http://acm.hdu.edu.cn/showproblem.php?pid=5550 题意: 一个大楼有n(2≤n≤4000)层,每层可以建一个乒乓球房或者一个游泳房,且每种房间在大楼 ...
- Win32 HTTP Download
头文件HTTPClient.h: #pragma once #ifndef HTTPClient_H_ #define HTTPClient_H_ #include <string> us ...