您的位置:首页 > 运维架构 > Shell

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

2018-02-09 15:50 351 查看
D. Bash and a Tough Math Puzzletime limit per test2.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBash 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.InputThe 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.OutputFor 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>
#inc
bd63
lude <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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐