Codeforces Round#458Div.1+2 D.Bash and a Tough Math Puzzle 区间GCD

本文介绍了Codeforces的一道题目,涉及数组、区间GCD和区间查询操作。Bash需要判断他的区间GCD猜测是否可以通过修改最多一个元素变为正确。问题通过线段树来解决,维护区间GCD并处理修改操作。对于查询,如果区间GCD已经是x的倍数,或者通过修改一个元素能变为x的倍数,则答案为'YES',否则为'NO'。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Bash and a Tough Math Puzzle
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Suppose he guesses that the gcd of the elements in the range [l, r] of a is x. 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 x 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 x. 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 qqueries of one of the following forms:

  • 1 l r x — Bash guesses that the gcd of the range [l, r] is x. Report if this guess is almost correct.
  • 2 i y — Bash sets ai to y.

Note: The array is 1-indexed.

Input

The first line contains an integer n (1 ≤ n ≤ 5·105)  — the size of the array.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109)  — the elements of the array.

The third line contains an integer q (1 ≤ q ≤ 4·105)  — the number of queries.

The next q lines describe the queries and may have one of the following forms:

  • 1 l r x (1 ≤ l ≤ r ≤ n, 1 ≤ x ≤ 109).
  • 2 i y (1 ≤ i ≤ n, 1 ≤ y ≤ 109).

Guaranteed, that there is at least one query of first type.

Output

For each query of first type, output "YES" (without quotes) if Bash's guess is almost correct and "NO" (without quotes) otherwise.


链接

一、题意

        给定一个大小为n的数组,可以对数组进行两种操作,一是询问区间[l,r]是否能够修改之多1个数字,使其区间GCD为x。二是修改某个数组元素的值。

二、思路

        对于带修改的区间GCD查询可以使用线段树来维护,父区间的区间GCD等于两个子区间的区间GCD再求一次GCD,记tree[rt].val表示节点编号为rt的父区间的区间GCD,lson和rson分别表示左右子区间,则有tree[rt].val = GCD(tree[lson].val, tree[rson].val),第二个操作便很容易实现。对于第一个操作,当tree[rt].val = (x的倍数)时,只需要修改最小值为x即可满足题意。当tree[rt].val != (x的倍数)时,我们必须修改其中一个数使其为x的倍数,且其他值已经为x的倍数才有可能满足题意,所以只需要考虑这个区间中存在几个数不是x的倍数,当超过一个时便不能满足题意,等价的就是在线段树上超过两个子区间的区间GCD不等于x的倍数。

三、代码

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;
typedef long long LL;
const int MAXN = 500500;
const int MOD7 = 1e9 + 7;
const int MOD9 = 1e9 + 9;
const int INF = 2e9;
const double EPS = 1e-6;
const double PI = 3.14159265358979;
const int dir_4r[] = { -1, 1, 0, 0 };
const int dir_4c[] = { 0, 0, -1, 1 };
const int dir_8r[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
const int dir_8c[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

#define lson	(rt*2+1)
#define rson	(rt*2+2)

struct Node {
	int l, r;
	int val;
	int mid() { return (l + r) >> 1; }
};

Node tree[MAXN << 2];
int input[MAXN];

int GCD(int a, int b) {
	int r;
	while (b) {
		r = a%b;
		a = b;
		b = r;
	}
	return a;
}

void pushUp(int rt) {
}

void pushDown(int rt) {
}

void buildTree(int rt, int l, int r) {
	tree[rt].l = l;
	tree[rt].r = r;

	if (l == r)
		tree[rt].val = input[l];
	else {
		int mid = tree[rt].mid();
		buildTree(lson, l, mid);
		buildTree(rson, mid + 1, r);
		tree[rt].val = GCD(tree[lson].val, tree[rson].val);
	}
}

void update(int rt, int pos, int val) {
	if (tree[rt].l == tree[rt].r) {
		tree[rt].val = val;
		return;
	}

	int mid = tree[rt].mid();
	if (pos <= mid)
		update(lson, pos, val);
	else
		update(rson, pos, val);

	tree[rt].val = GCD(tree[lson].val, tree[rson].val);//由子区间的GCD更新父区间的GCD
}

void query(int rt, int l, int r, int x, int &cnt) {
	if (tree[rt].l == l && tree[rt].r == r) {
		if (tree[rt].val % x != 0) {						//该区间区间GCD不是x的倍数
			if (l == r)										//叶节点特判
				cnt++;
			else if (tree[lson].val % x != 0) {				//左子区间不满足
				if (tree[rson].val % x != 0) {				//右子区间同时不满足
					cnt += 2;								//则已经至少存在两个数不满足x的倍数
					return;									//可以直接返回
				}
				query(lson, l, tree[rt].mid(), x, cnt);		//左子区间不满足右子区间满足时,查询左子区间
			}
			else
				query(rson, tree[rt].mid() + 1, r, x, cnt);	//左子区间满足时,查询右子区间
		}
		return;
	}

	int mid = tree[rt].mid();
	if (mid >= r)
		query(lson, l, r, x, cnt);
	else if (mid < l)
		query(rson, l, r, x, cnt);
	else {
		query(lson, l, mid, x, cnt);
		query(rson, mid + 1, r, x, cnt);
	}
}

int main() {
	int n, q, l, r, x, p, cmd;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", input + i);
	buildTree(0, 0, n - 1);

	scanf("%d", &q);
	for (int i = 0; i < q; ++i) {
		scanf("%d", &cmd);
		if (cmd == 1) {
			scanf("%d%d%d", &l, &r, &x);
			l--;
			r--;
			int cnt = 0;//记录区间中不等于x的倍数的数有几个
			query(0, l, r, x, cnt);
			if (cnt <= 1)
				printf("YES\n");
			else
				printf("NO\n");
		}
		else {
			scanf("%d%d", &p, &x);
			p--;
			update(0, p, x);
		}
	}

	//system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值