Codecraft-18 and Codeforces Round #458 D. Bash and a Tough Math Puzzle(線段樹)

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.

Examples
input
3
2 6 3
4
1 1 2 2
1 1 3 3
2 1 9
1 1 3 2
output
YES
YES
NO
input
5
1 2 3 4 5
6
1 1 4 2
2 3 6
1 1 4 2
1 1 5 2
2 5 10
1 1 5 2
output
NO
YES
NO
YES
Note

In the first sample, the array initially is {2, 6, 3}.

For query 1, the first two numbers already have their gcd as 2.

For query 2, we can achieve a gcd of 3 by changing the first element of the array to 3. Note that the changes made during queries of type 1are temporary and do not get reflected in the array.

After query 3, the array is now {9, 6, 3}.

For query 4, no matter which element you change, you cannot get the gcd of the range to be 2.


#include <bits/stdc++.h>
using namespace std;
int a[500005];
struct tree{
	int l, r, v;
}c[500005 * 3];
vector<int>g;
int gcd(int a, int b){
	return b == 0 ? a : gcd(b, a % b);
}
void build(int o, int l, int r){
	c[o].l = l; c[o].r = r;
	if(l == r){
		c[o].v = a[l]; return;
	}
	int mid = l + r >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
	c[o].v = gcd(c[o << 1].v, c[o << 1 | 1].v);
}
void update(int o, int pos, int v){
	if(c[o].l == c[o].r){
		c[o].v = v; return;
	}
	int mid = c[o].l + c[o].r >> 1;
	if(pos <= mid) update(o << 1, pos, v);
	else update(o << 1 | 1, pos, v);
	c[o].v = gcd(c[o << 1].v, c[o << 1 | 1].v);
}
void query(int o, int L, int R){
	if(c[o].l >= L && c[o].r <= R){
		g.push_back(o); return;
	}
	int mid = c[o].l + c[o].r >> 1;
	if(mid >= L) query(o << 1, L, R);
	if(mid < R) query(o << 1 | 1, L, R);
}
bool check(int o, int v){
	if(c[o].l == c[o].r){
		return 1;
	}
	int mid = c[o].l + c[o].r >> 1;
	if(c[o << 1].v % v != 0 && c[o << 1 | 1].v % v != 0){
		return false;
	}
	if(c[o << 1].l == c[o << 1].r){
		if(c[o << 1].v % v == 0 || c[o << 1 | 1].v % v == 0){
			return true;
		}
		else{
			return false;
		}
	}
	if(c[o << 1].v % v != 0){
		return check(o << 1, v);
	}
	else{
		return check(o << 1 | 1, v);
	}
}
int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i){
		scanf("%d", &a[i]);
	}
	build(1, 1, n);
	int q, op, x, y, v, l, r, mid;;
	scanf("%d", &q);
	while(q--){
		scanf("%d", &op);
		if(op == 2){
			scanf("%d %d", &x, &y);
			update(1, x, y);
		}
		if(op == 1){
			scanf("%d %d %d", &x, &y, &v);
			g.clear();
			query(1, x, y);
			int cnt = 0, pos;
			for(int i = 0; i < g.size(); ++i){
				if(c[g[i]].v % v != 0){
					cnt++;
					pos = g[i];
				}
			}
			if(cnt == 0) printf("YES\n");
			if(cnt > 1) printf("NO\n");
			if(cnt == 1){
				if(check(pos, v)) printf("YES\n");
				else printf("NO\n");
			}
		}
	}
}

/*
題意:
5e5個數,4e5次操作,每次要麼詢問一個區間[l,r]內所有數的GCD是否爲v(可以改變區間內的一個數,不影響後面操作),
要麼修改一個數。

思路:
線段樹維護區間gcd,然後分類討論一下。對於允許一次的修改操作,如果需要修改,我們一定把這個數改爲v,對吧。
那麼只要看分治整個區間,如果左右子區間gcd都不能整出v,那麼肯定NO,否則就遞歸下去。
*/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章