CodeForces - 914D Bash and a Tough Math Puzzle(线段树)
题面
Bash likes playing with arrays. He has an array a1, a2, ... an of n integers. He likes to guess the greatest common divisor (gcd) of different segments of the array. Of course, sometimes the guess is not correct. However, Bash will be satisfied if his guess is almost correct.
...
题意
给定一个数组,有两种操作,第一种修改某个值,第二种查询区间内,如果任意修改某个值,区间所有值的\(gcd\)可不可以等于\(x\).
思路
所有数\(gcd\)等于\(x\),可以推出所有数能整除\(x\).
如果区间内(长度为\(len\)) 有\(len-1\)个数字都能整除\(x\),那么只需把剩下的那个数改成\(x\)就行了.
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cerr<<#x<<" = "<<x<<endl;
#define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl;
#define lson l,mid,ls
#define rson mid+1,r,rs
#define ls (rt<<1)
#define rs ((rt<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int loveisblue = 486;
const int maxn = 500086;
const int maxm = 100086;
const int inf = 0x3f3f3f3f;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
int gcd[maxn<<2];
int a[maxn];
void build(int l,int r,int rt){
if(l==r){
gcd[rt]=a[l];
return;
}
int mid = (l+r)>>1;
build(lson);
build(rson);
gcd[rt]=__gcd(gcd[ls],gcd[rs]);
}
void update(int l,int r,int rt,int pos,int val){
if(l==r){
gcd[rt]=val;
return;
}
int mid = (l+r)>>1;
if(pos<=mid)update(lson,pos,val);
else update(rson,pos,val);
gcd[rt]=__gcd(gcd[ls],gcd[rs]);
}
int query(int l,int r,int rt,int L,int R,int val){
int ans = 0;
if(L<=l&&R>=r&&gcd[rt]%val==0){
return 0;
}
if(l==r){
return 1;
}
int mid = (l+r)>>1;
if(L<=mid){
ans+=query(lson,L,R,val);
}
if(ans>=2){return ans;}
if(R>mid){
ans+= query(rson,L,R,val);
}
return ans;
}
int main() {
ios::sync_with_stdio(true);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int n;
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 type;
scanf("%d",&type);
if(type==1){
int l,r;
scanf("%d%d",&l,&r);
int p;
scanf("%d",&p);
if(query(1,n,1,l,r,p)<=1){
printf("YES\n");
}else{
printf("NO\n");
}
}else{
int pos,val;
scanf("%d",&pos);
scanf("%d",&val);
update(1,n,1,pos,val);
}
}
return 0;
}
如需转载,请注明出处
如有侵权,联系删除
2290713181@qq.com
如有侵权,联系删除
2290713181@qq.com