Codecraft-18 and Codeforces Round #458:D,Bash and a Tough Math Puzzle

题目传送门

题目大意:Bash喜欢对数列进行操作。第一种操作是询问l~r区间内的gcd值是否几乎为x,几乎为表示能否至多修改一个数达到。第二种操作是将ai修改为x。总共Q个询问,N个数。

Solution:简单来说,就是对区间gcd值的维护,使用线段树实现。

code:

#include <cstdio>
using namespace std;

int read()
{
    char c;while(c=getchar(),c<‘0‘||c>‘9‘);
    int x=c-‘0‘;while(c=getchar(),c>=‘0‘&&c<=‘9‘)x=x*10+c-‘0‘;
    return x;
}

const int MAXN=5*100000+5;

int N,Q,a[MAXN],o,cnt,x,y,c;
int seg[MAXN*4];

int gcd(int x,int y){return !y?x:gcd(y,x%y);}

void build(int node,int l,int r)
{
    if(l==r)seg[node]=a[l];
    else{
        int mid=l+r>>1;
        build(node<<1,l,mid);
        build(node<<1|1,mid+1,r);
        seg[node]=gcd(seg[node<<1],seg[node<<1|1]);//更行该节点的值
    }
    return ;
}//建树

void change(int node,int l,int r,int dist,int v)
{
    if(l==dist && r==dist){
        seg[node]=v;
        return ;
    }//到达要修改的节点
    int mid=l+r>>1;
    if(mid>=dist)change(node<<1,l,mid,dist,v);
    else         change(node<<1|1,mid+1,r,dist,v);
    seg[node]=gcd(seg[node<<1],seg[node<<1|1]);//更新
    return ;
}

void get(int l,int r,int ql,int qr,int node,int v)
{
    if(cnt>1)return ;//优化
    if(l==r){//此处之所以可以这样,因为我们在搜到这个点之前保证了这个点的seg值%v不为0
        cnt++;
        return ;
    }
    int mid=l+r>>1;
    if(mid>=ql&&seg[node<<1]%v/*此处为上面做法的原因*/)  get(l,mid,ql,qr,node<<1,v);
    if(mid<qr &&seg[node<<1|1]%v)get(mid+1,r,ql,qr,node<<1|1,v);
    return ;
}

void find(int x,int y,int c)
{
    cnt=0;get(1,N,x,y,1,c);
    if(cnt<=1)puts("YES");
    else puts("NO");
    return ;
}

int main()
{
    N=read();
    register int i,j;
        for(i=1;i<=N;i++)a[i]=read();
    build(1,1,N);
    Q=read();
        while(Q--){
            o=read();
            if(o==1){x=read(),y=read(),c=read(),find(x,y,c);}
            else    {x=read(),y=read(),change(1,1,N,x,y);}
        }
    return 0;
}

原文地址:https://www.cnblogs.com/Cptraser/p/8434351.html

时间: 2024-07-31 14:03:27

Codecraft-18 and Codeforces Round #458:D,Bash and a Tough Math Puzzle的相关文章

Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变一个数的值(注意不是真的改变),使得这个区间的gcd是小明所猜的数也算小明猜对.另一种操作就是真的修改某一点的值. 解题思路 这里我们使用线段树,维护区间内的gcd,判断的时候需要判断这个区间的左右子区间的gcd是不是小明猜的数的倍数或者就是小明猜的数,如果是,那么小明猜对了.否则就需要进入这个区间

codeforces 914 D Bash and a Tough Math Puzzle

1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 #include<cstdio> 6 using namespace std; 7 const int N=500100; 8 int n,a[N],m; 9 struct tree{ 10 int l,r,gcd; 11 }tr[N*5]; 12 int gcd(int x,in

Codeforces 914D - Bash and a Tough Math Puzzle 线段树,区间GCD

题意: 两个操作, 单点修改 询问一段区间是否能在至多一次修改后,使得区间$GCD$等于$X$ 题解: 正确思路; 线段树维护区间$GCD$,查询$GCD$的时候记录一共访问了多少个$GCD$不被X整除的区间即可,大于一个就NO 要注意的是,如果真的数完一整个区间,肯定会超时,因此用一个外部变量存储数量,一旦超过一个,就停止整个查询 #include <bits/stdc++.h> #define endl '\n' #define ll long long #define IO ios::s

Codecraft-18 and Codeforces Round #458 C dp D 线段树

Codecraft-18 and Codeforces Round #458 C. Travelling Salesman and Special Numbers 题意: 一个由0.1 组成的数 n,操作:n 有 m 个 1,就把 n 变为 m. 问 <=n 的数中有多少个恰好经过 k 次操作能变为 1. tags:  dp[i][j] 表示长度为 i 且有 j 个 1  的串,比对应的 n 要小的方案数. #include<bits/stdc++.h> using namespace

Codeforces Round #300 (A,B,C,D)

题目传送:Codeforces Round #300 A. Cutting Banner 思路:一看题就会错意了,然后一顿猛敲,果不其然的被hack了,然后才发现只需要剪中间那一段就可以了,然后又傻逼得少写一个等号,还是被hack了,心累啊 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #in

Educational Codeforces Round 55:B. Vova and Trophies

B. Vova and Trophies 题目链接:https://codeforc.es/contest/1082/problem/B 题意: 给出一个"GS"串,有一次交换两个字母的机会,问最大的连续"G"串是多少. 题解: 在末尾后面放一个哨兵"S",然后扫两遍,维护S左边和右边连续的"G"分别有多少个,然后求最大就可以了. 注意并不是所有的串都可以通过交换使长度变大这种情况,比如 "SGGGGS",

Educational Codeforces Round 49(A,B,C,D)

A Palindromic Twist 字符串模拟,暴力check下. 1 #include <set> 2 #include <map> 3 #include <queue> 4 #include <deque> 5 #include <stack> 6 #include <cmath> 7 #include <cstdio> 8 #include <vector> 9 #include <string

Codeforces Round #262 (Div. 2) 总结:二分

B. Little Dima and Equation 思路:本来前两题都很快做出来的,然后觉得ranting应该可以增加了.然后觉得代码没问题了,又想到了一组cha人的数据,然后就锁了,然后刚锁就被别人cha了看了代码才发现尼玛忘了判断小于10^9了,然后C反正想了好多种方法都不会就没心情了,就这样rating又降了 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #in

Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟

E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to