CF914D Bash and a Tough Math Puzzle

本文介绍了一个基于线段树的数据结构问题解决方案,通过维护区间最大公约数(GCD),支持单点修改和查询区间是否至多修改一个数后GCD为特定值。采用二分查找优化查询效率,实现O(nlog²n)的时间复杂度。

Bash and a Tough Math Puzzle

写发博客证明自己还活着QwQ

题目描述

Bash likes playing with arrays. He has an array a1,a2,... ana_{1},a_{2},...\ a_{n}a1,a2,... an of nnn 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.
Suppose he guesses that the gcd of the elements in the range [l,r][l,r][l,r] of aaa is xxx . He considers the guess to be almost correct if he can change at most one element in the segment such that the gcd of the segment is xxx after making the change. Note that when he guesses, he doesn’t actually change the array — he just wonders if the gcd of the segment can be made xxx . Apart from this, he also sometimes makes changes to the array itself.
Since he can’t figure it out himself, Bash wants you to tell him which of his guesses are almost correct. Formally, you have to process qqq queries of one of the following forms:

  • 1 l r x1 \ l\ r\ x1 l r x — Bash guesses that the gcd of the range [l,r][l,r][l,r] is xxx . Report if this guess is almost correct.
  • 2 i y2 \ i \ y2 i y — Bash sets aia_{i}ai to yyy .

Note: The array is 111 -indexed.

题目大意

给你一个序列,要求支持单点修改,查询一个区间是否满足至多修改一个数使得这个区间的GCDGCDGCDxxx

Solution

  • 很显然的线段树,用线段树维护区间GCDGCDGCD,单点修改的问题就解决了。
  • 至于查询的时候,因为只要有超过两个数字不满足条件,就可以结束判断了;所以我们选择在线段树上二分,去找不满足条件的数字,如果一个区间的GCDGCDGCD不等于xxx,就说明这个区间里有不满足条件的数字,到这个区间里面找就行了,单次复杂度O(logn)O(logn)O(logn)
  • 总复杂度O(nlog2n)O(nlog^2n)O(nlog2n)
#include<cstdio>

const int maxn = 5e5 + 7;

class SegmentTree{
private :
    struct Node{
        Node *child[2];
        int left, right, val;

        Node (int left = 0, int right = 0) :
            left(left),
            right(right) {
            child[0] = NULL;
            child[1] = NULL;
        }
    };
    Node *root;
    int sum;

    int Gcd(int x, int y) {
        if (!x) {
            return y;
        }
        return Gcd(y % x, x);
    }

    void BuildTree(Node *now, int *a) {
        if (now->left == now->right) {
            now->val = a[now->left];
            return;
        }
        int mid = now->left + now->right >> 1;
        now->child[0] = new Node(now->left, mid);
        now->child[1] = new Node(mid + 1, now->right);
        BuildTree(now->child[0], a), BuildTree(now->child[1], a);
        Update(now);
    }

    void Update(Node *now) {
        now->val = Gcd(now->child[0]->val, now->child[1]->val);
    }

    void Change(Node *now, int pos, int x) {
        if (now->left == now->right) {
            now->val = x;
            return;
        }
        int mid = now->left + now->right >> 1;
        if (pos <= mid) {
            Change(now->child[0], pos, x);
        } else {
            Change(now->child[1], pos, x);
        }
        Update(now);
    }

    void Query(Node *now, int x, int l, int r) {
        if (sum > 1) {
            return;
        }
        if (now->left == now->right) {
            if (now->val % x != 0) {
                sum++;
            }
            return;
        }
        if (now->left >= l && now->right <= r) {
            if (now->val % x != 0) {
                Query(now->child[0], x, l, r);
                Query(now->child[1], x, l, r);
            }
            return;
        }
        int mid = now->left + now->right >> 1;
        if (r <= mid) {
            Query(now->child[0], x, l, r);
        } else if (l > mid) {
            Query(now->child[1], x, l, r);
        } else {
            Query(now->child[0], x, l, r), Query(now->child[1], x, l, r);
        }
    }

public :
    void Init(int n, int *a) {
        root = new Node(1, n);
        BuildTree(root, a);
    }

    void Change(int pos, int x) {
        Change(root, pos, x);
    }

    bool Query(int l, int r, int x) {
        sum = 0;
        Query(root, x, l, r);
        return sum <= 1;
    }
};

class Solution{
private :
    int n, m, a[maxn];
    SegmentTree tree;
    
public :
    Solution() {
        Get();
        Solve();
    }

    void Get() {
        scanf("%d", &n);
        for (register int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
        }
        tree.Init(n, a);
    }

    void Solve() {
        scanf("%d", &m);
        for (register int i = 1; i <= m; i++) {
            int op, l, r, x, y;
            scanf("%d", &op);
            if (op == 1) {
                scanf("%d %d %d", &l, &r, &x);
                if (tree.Query(l, r, x)) {
                    printf("YES\n");
                } else {
                    printf("NO\n");
                }
            } else {
                scanf("%d %d", &x, &y);
                tree.Change(x, y);
            }
        }
    }
};
Solution sol;

int main() {}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值