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 ...
随机推荐
- Windows+Git+TortoiseGit+COPSSH 安装教程及问题收集
准备工作: 1. git-1.8.1.2-preview20130201.exe 下载地址: https://code.google.com/p/msysgit/downloads/list 2. C ...
- [翻译] TCBlobDownload
TCBlobDownload TCBlobDownload uses NSOperations to download large files (typically videos, music... ...
- [翻译] USING GIT IN XCODE [2] 在XCODE中使用GIT[2]
USING GIT IN XCODE http://www.cimgf.com/2013/12/10/using-git-in-xcode/ USING AN EXISTING REMOTE PROJ ...
- 获取系统屏幕尺寸参数的类WxHxD
获取系统屏幕尺寸参数的类WxHxD 源码: // // WxHxD.h // PM2.5 // // Created by YouXianMing on 14/10/29. // Copyright ...
- python操作Exchange邮箱实例(-)
需求很简单,就是实现按公司域名及服务器模拟exchange发送邮件,主要是协助自动化测试.主要功能:收件人/抄送/正文html/附件 本实例基于:python2.7.11 exchangelib1.1 ...
- fiddler post 请求 webapi
今天小伙伴遇到一个问题,大概就是说用fiddler post 一个参数,但是后台一直无法获取,如下 后来发现请求的条件有问题,而且也很容易忽略,正确如下 content-type的设定为默认值,pos ...
- 资料整理,SQL Server ,面试前复习笔记
T-SQL 要掌握的知识点分类 SQL 面向数据库执行查询 SQL 从数据库取回数据 SQL 在数据库中插入新的记录 SQL 更新数据库中的数据 SQL 从数据库删除记录 SQL 创建新数据库 SQL ...
- 【4】python函数基础
---恢复内容开始--- 案例1:时间下一秒程序 #__author:"吉勇佳" #date: 2018/10/14 0014 #function: timestr=input(& ...
- jdk1.5-jdk1.9的主要区别
jdk1.5相对以前jdk版本主要新增功能 1.自动拆箱和装箱 其中基本数据类型的包装类有:Double,Float,Long,Integer,Short,Character和Boolean 2.提供 ...
- 切换composer国内镜像
composer config -g repo.packagist composer https://packagist.phpcomposer.com