跳到正文

题解

【P4435 [COCI 2017/2018 #2] ​​Garaža】

Pig_Eat_Earth约 22 分钟
原文
文章信息与版本

文章信息

发布
2026年8月22日
收录
2026年8月22日
更新
2026年11月11日
篇幅
约 22 分钟
题目
P4435

版本

  1. v1当前

考场乱搞做法,没想到竟然是正解 OwO,看了题解区大佬写的要么跟我不一样要么太难懂了,所以写一篇。

约定

  • gcd⁡[l,r]=gcd⁡(al,⋯ ,ar)\gcd\left[l,r\right]=\gcd\left(a_l,\cdots,a_r\right):
    特别地,如果 l>rl>r,gcd⁡[l,r]=inf⁡\gcd\left[l,r\right]=\inf;
  • WW 表示 aia_i 的值域。

正文

考虑对于每个右端点维护它能到达最远的左端点,记为 bib_i(bb 是 bound 的意思),为了方便查询,在线段树中维护 i−bi+1i-b_i+1。

另外,根据最大公因数的结合律,另开一棵线段树维护区间 GCD。显然我们可以 O(log⁡N)\Omicron(\log N) 做到以下操作:

考虑查询。答案为 ∑i=lr(i−max⁡{l,bi}+1)\sum_{i=l}^r(i-\max\left\{l,b_i\right\}+1)。显然 pip_i 单调不降,可以二分找到最后一个 gcd⁡[l,i]>1\gcd\left[l,i\right]>1 的位置,则在其左边能达到的左端点为 ll,在其右边为 bib_i。显然如果我们二分出来了分界点,左边可以 O(1)\Omicron(1) 计算,右边可以通过一个 O(log⁡N)\Omicron(\log N) 的线段树查询计算。

因此我们只需要考虑怎么维护 bib_i 就好了。

修改时,我们首先需要消除原数的影响。即二分出 pp 能到达的最远右端点 R0R_0,将 bp+1⋯bR0b_{p+1}\cdots b_{R_0} 全置为 p+1p+1。

接着先将数组更新,然后我们可以二分找出 pp 能达到的最远右端点 RR,只有 [p,R]\left[p,R\right] 区间内的 bib_i 需要修改。而对于每个右端点 R′R',其可能取到的左端点只与 gcd⁡[p,R′]\gcd\left[p,R'\right] 有关。这时我们有一个关键观察:

神秘小结论

对于所有 R′∈[p,R]R'\in\left[p,R\right],gcd⁡[p,R′]\gcd\left[p,R'\right] 单调不降,且至多有 log⁡W\log W 种取值。

(这里参考了这篇题解,在此表达感谢)

证明

对于一个下标 i∈(p,R]i\in(p,R],令 g=gcd⁡[p,i−1]g=\gcd\left[p,i-1\right],g′=gcd⁡[p,i]g'=\gcd\left[p,i\right]。

则有两种情况:

  1. g∣aig\mid a_i,则 g′=gg'=g;
  2. g∤aig\nmid a_i,则必有 g′=gkg'=\dfrac gk,其中 kk 为 gg 的真因子,必满足 k≥2k\ge2,因此 g′≤g2g'\le \dfrac g2。

因此我们可以把 [p,R]\left[p,R\right] 按 gcd⁡[p,R′]\gcd\left[p,R'\right] 分为至多 log⁡W\log W 段,每段分别更新,段内 bib_i 相等,可以在线段树上进行区间修改操作。

现在考虑怎么快速分段。直接看伪代码。

定义lbins⁡(p,c)=二分最右边的k使gcd⁡[p,k]>crbins⁡(p,c)=二分最左边的k使gcd⁡[k,p]>cassign⁡(l,r,x)=将bl到br置为x初始化R′←lbins⁡(p,1)c←gcd⁡[p,R′]算法Forever:t←lbins⁡(p,c)k←rbins⁡(R′,1)assign⁡(t+1,R′,gcd⁡[k,R′])if t<p:breakR′←tc←gcd⁡[p,R′]\begin{align*} &\text{定义}\\ &\operatorname{lbins}(p,c)= 二分最右边的 k 使 \gcd\left[p,k\right]>c\\ &\operatorname{rbins}(p,c)= 二分最左边的 k 使 \gcd\left[k,p\right]>c\\ &\operatorname{assign}\left(l,r,x\right)= 将 b_l 到 b_r 置为 x\\ \\ &\text{初始化}\\ &R'\leftarrow \operatorname{lbins}(p,1)\\ &c\leftarrow \gcd\left[p,R'\right]\\ \\ &\text{算法}\\ &\text{Forever}:\\ &\qquad t\leftarrow \operatorname{lbins}\left(p,c\right)\\ &\qquad k\leftarrow \operatorname{rbins}\left(R',1\right)\\ &\qquad \operatorname{assign}\left(t+1,R',\gcd\left[k,R'\right]\right)\\ &\qquad \text{if}\space t<p:\\ &\qquad\qquad\text{break}\\ &\qquad R'\leftarrow t\\ &\qquad c\leftarrow\gcd\left[p,R'\right] \end{align*}

复杂度分析

空间复杂度显然为 O(N)\Omicron(N)。

(时间复杂度分析建议参考实现)

对于线段树操作:

  1. 维护 gcd⁡\gcd 的线段树:
    建树 O(N)\Omicron(N)
    单点修改、区间查询为 O(log⁡N)\Omicron(\log N);
    二分为 O(log⁡Nlog⁡W)\Omicron(\log N\log W)(计算 gcd⁡\gcd 为 O(log⁡W)\Omicron(\log W));
  2. 维护 bib_i 的线段树:
    建树 O(Nlog⁡Nlog⁡W)\Omicron(N\log N\log W)(每个元素二分一次);
    区间修改、查询均为 O(log⁡N)\Omicron(\log N)。

单次修改进行 O(log⁡W)\Omicron(\log W) 次二分,单次 O(log⁡Nlog⁡W)\Omicron(\log N\log W),共 O(log⁡Nlog⁡2W)\Omicron(\log N\log^2 W)。

单次询问进行一次二分和一次线段树上区间查询,共 O(log⁡Nlog⁡W)\Omicron(\log N\log W)。

综上,总时间复杂度 O(Nlog⁡Nlog⁡W+Qlog⁡Nlog⁡2W)\Omicron(N\log N\log W+Q\log N\log^2 W)。

代码实现

AC Code
C++
#include <bits/stdc++.h>
#define int ll
using namespace std;
using ll = long long;
const int N = 1e5, P = N << 2;
int n, q, a[N + 5];

namespace SegG{
	int d[P + 5], rd[P + 5];
	
	inline void pushup(int p){ d[p] = __gcd(d[p << 1], d[p << 1 | 1]); }
	
	void build(int s = 1, int t = n, int p = 1){
		if(s == t){
			d[p] = a[s];
			return;
		}
		const int m = s + t >> 1;
		build(s, m, p << 1);
		build(m + 1, t, p << 1 | 1);
		pushup(p);
	}
	
	void upd(int k, int c, int s = 1, int t = n, int p = 1){
		if(s == t){
			d[p] = c;
			return;
		}
		const int m = s + t >> 1;
		if(k <= m) upd(k, c, s, m, p << 1);
		else upd(k, c, m + 1, t, p << 1 | 1);
		pushup(p);
	}
	
	int get(int l, int r, int s = 1, int t = n, int p = 1){
		if(l <= s && t <= r) return d[p];
		const int m = s + t >> 1;
		if(r <= m) return rd[p] = get(l, r, s, m, p << 1);
		else if(l > m) return rd[p] = get(l, r, m + 1, t, p << 1 | 1);
		else return rd[p] = __gcd(get(l, r, s, m, p << 1), get(l, r, m + 1, t, p << 1 | 1));
	}
	
	inline int grd(int l, int r, int s, int t, int p){ return t < l || s > r ? 1 : l <= s && t <= r ? d[p] : rd[p]; }
	
	int _lbins(int k, int c, int l, int s = 1, int t = n, int p = 1){
		if(s == t) return s - (__gcd(l, a[s]) <= c); 
		const int m = s + t >> 1;
		if(k > m) return _lbins(k, c, l, m + 1, t, p << 1 | 1);
		else if(__gcd(l, grd(k, n, s, m, p << 1)) > c) return _lbins(k, c, __gcd(l, grd(k, n, s, m, p << 1)), m + 1, t, p << 1 | 1);
		else return _lbins(k, c, l, s, m, p << 1);
	}
	
	inline int lbins(int k, int c = 1){
		get(k, n);
//		cout << "lbins " << k << ' ' << c << ' ' << _lbins(k, c, a[k]) << endl;
		return _lbins(k, c, a[k]);
	}
	
	int _rbins(int k, int c, int l, int s = 1, int t = n, int p = 1){
		if(s == t) return s + (__gcd(l, a[s]) <= c);
		const int m = s + t >> 1;
		if(k <= m) return _rbins(k, c, l, s, m, p << 1);
		else if(__gcd(l, grd(1, k, m + 1, t, p << 1 | 1)) > c) return _rbins(k, c, __gcd(l, grd(1, k, m + 1, t, p << 1 | 1)), s, m, p << 1);
		else return _rbins(k, c, l, m + 1, t, p << 1 | 1);
	}
	
	inline int rbins(int k, int c = 1){
		get(1, k);
//		cout << "rbins " << k << ' ' << c << ' ' << _rbins(k, c, a[k]) << endl;
		return _rbins(k, c, a[k]);
	}
}

namespace SegS{
	ll d[P + 5];
	int b[P + 5];
	
	inline void pushup(int p){ d[p] = d[p << 1] + d[p << 1 | 1]; }
	
	inline void chg(int s, int t, int k, int p){
		d[p] = ((s + t) * (t - s + 1ll) >> 1) - (k - 1ll) * (t - s + 1ll);
		b[p] = k;
	}
	
	inline void pushdown(int s, int m, int t, int p){
		if(!b[p]) return;
		chg(s, m, b[p], p << 1);
		chg(m + 1, t, b[p], p << 1 | 1);
		b[p] = 0;
	}
	
	void upd(int l, int r, int c, int s = 1, int t = n, int p = 1){
		if(l <= s && t <= r){
			chg(s, t, c, p);
			return;
		}
		const int m = s + t >> 1;
		pushdown(s, m, t, p);
		if(l <= m) upd(l, r, c, s, m, p << 1);
		if(m < r) upd(l, r, c, m + 1, t, p << 1 | 1);
		pushup(p);
	}
	
	ll get(int l, int r, int s = 1, int t = n, int p = 1){
		if(l <= s && t <= r) return d[p];
		const int m = s + t >> 1;
		pushdown(s, m, t, p);
		ll res = 0;
		if(l <= m) res += get(l, r, s, m, p << 1);
		if(m < r) res += get(l, r, m + 1, t, p << 1 | 1);
		return res;
	}
}

signed main(){
	cin.tie(0)->sync_with_stdio(0);
	cin >> n >> q;
	for(int i = 1; i <= n; ++ i) cin >> a[i];
	SegG::build();
	for(int i = 1; i <= n; ++ i){
		if(a[i] > 1) SegS::upd(i, i, SegG::rbins(i));
//		cout << i << ' ' << a[i] << ' ' << SegG::rbins(i) << '\n';
	}
	int o, x, y, nq = 0;
	while(q --){
		cin >> o >> x >> y;
		if(o == 1){
//			cout << "CHG " << x << ' ' << a[x] << "->" << y << '\n';
			const int t = SegG::lbins(x);
			if(t > x) SegS::upd(x + 1, t, x + 1);
			if(y == 1){
				SegG::upd(x, a[x] = 1);
				SegS::upd(x, x, x + 1);
			}
			else{
				SegG::upd(x, a[x] = y);
				int p = SegG::lbins(x), c = SegG::get(x, p);
				for(int t = 114514; t > x; ){
					t = SegG::lbins(x, c);
//					cout << "xt " << p << ' ' << c << ' ' << t << endl;
					SegS::upd(t + 1, p, SegG::rbins(p));
//					cout << "rbp " << SegG::rbins(p) << endl;
					if(t < x) break;
					c = SegG::get(x, p = t);
					if(c >= y){
						SegS::upd(x, p, SegG::rbins(p));
						break;
					}
				}
			}
		}
		else{
			const int t = min(y, SegG::lbins(x));
//			cout << "t " << t << endl;
			ll ans = t >= x ? ((t + x) * (t - x + 1) >> 1) - (x - 1ll) * (t - x + 1) : 0;
//			cout << "ll " << ans << endl;
			if(t < y) ans += SegS::get(t + 1, y);
//			cout << "QUERY " << (++ nq) << ' ' << x << ' ' << y << ' ';
			cout << ans << '\n';
		}
	}
}
考场代码

唯一不同在于二分的实现,这份代码中的单次二分时间复杂度为 O(log⁡2Nlog⁡W)\Omicron(\log^2 N\log W)。

C++
#include <bits/stdc++.h>
#define int ll
using namespace std;
using ll = long long;
const int N = 1e5, P = N << 2;
int n, q, a[N + 5];

template<typename T>
inline void chmax(T& a, const T b){ a = max(a, b); }
template<typename T>
inline void chmin(T& a, const T b){ a = min(a, b); }

namespace SegG{
	int d[P + 5];
	
	inline void pushup(int p){ d[p] = __gcd(d[p << 1], d[p << 1 | 1]); }
	
	void build(int s = 1, int t = n, int p = 1){
		if(s == t){
			d[p] = a[s];
			return;
		}
		const int m = s + t >> 1;
		build(s, m, p << 1);
		build(m + 1, t, p << 1 | 1);
		pushup(p);
	}
	
	void upd(int k, int c, int s = 1, int t = n, int p = 1){
		if(s == t){
			d[p] = c;
			return;
		}
		const int m = s + t >> 1;
		if(k <= m) upd(k, c, s, m, p << 1);
		else upd(k, c, m + 1, t, p << 1 | 1);
		pushup(p);
	}
	
	int get(int l, int r, int s = 1, int t = n, int p = 1){
		if(l <= s && t <= r) return d[p];
		const int m = s + t >> 1;
		if(r <= m) return get(l, r, s, m, p << 1);
		else if(l > m) return get(l, r, m + 1, t, p << 1 | 1);
		else return __gcd(get(l, r, s, m, p << 1), get(l, r, m + 1, t, p << 1 | 1));
	}
	
	inline int lbins(int k, int c = 1){
		int l = k, r = n, m, ans = k - 1;
		while(l <= r){
			m = l + r >> 1;
			if(get(k, m) > c){
				ans = m;
				l = m + 1;
			}
			else r = m - 1;
		}
		return ans;
	}
	
	inline int rbins(int k, int c = 1){
		int l = 1, r = k, m, ans = k + 1;
		while(l <= r){
			m = l + r >> 1;
			if(get(m, k) > c){
				ans = m;
				r = m - 1;
			}
			else l = m + 1;
		}
		return ans;
	}
}

namespace SegS{
	ll d[P + 5];
	int b[P + 5];
	
	inline void pushup(int p){ d[p] = d[p << 1] + d[p << 1 | 1]; }
	
	inline void chg(int s, int t, int k, int p){
		d[p] = ((s + t) * (t - s + 1ll) >> 1) - (k - 1ll) * (t - s + 1ll);
		b[p] = k;
	}
	
	inline void pushdown(int s, int m, int t, int p){
		if(!b[p]) return;
		chg(s, m, b[p], p << 1);
		chg(m + 1, t, b[p], p << 1 | 1);
		b[p] = 0;
	}
	
	void upd(int l, int r, int c, int s = 1, int t = n, int p = 1){
		if(l <= s && t <= r){
			chg(s, t, c, p);
			return;
		}
		const int m = s + t >> 1;
		pushdown(s, m, t, p);
		if(l <= m) upd(l, r, c, s, m, p << 1);
		if(m < r) upd(l, r, c, m + 1, t, p << 1 | 1);
		pushup(p);
	}
	
	ll get(int l, int r, int s = 1, int t = n, int p = 1){
		if(l <= s && t <= r) return d[p];
		const int m = s + t >> 1;
		pushdown(s, m, t, p);
		ll res = 0;
		if(l <= m) res += get(l, r, s, m, p << 1);
		if(m < r) res += get(l, r, m + 1, t, p << 1 | 1);
		return res;
	}
}

signed main(){
	cin.tie(0)->sync_with_stdio(0);
	cin >> n >> q;
	for(int i = 1; i <= n; ++ i) cin >> a[i];
	SegG::build();
	for(int i = 1; i <= n; ++ i){
		if(a[i] > 1) SegS::upd(i, i, SegG::rbins(i));
//		cout << i << ' ' << a[i] << ' ' << SegG::rbins(i) << '\n';
	}
	int o, x, y, nq = 0;
	while(q --){
		cin >> o >> x >> y;
		if(o == 1){
//			cout << "CHG " << x << ' ' << a[x] << "->" << y << '\n';
			const int t = SegG::lbins(x);
			if(t > x) SegS::upd(x + 1, t, x + 1);
			if(y == 1){
				SegG::upd(x, a[x] = 1);
				SegS::upd(x, x, x + 1);
			}
			else{
				SegG::upd(x, a[x] = y);
				int p = SegG::lbins(x), c = SegG::get(x, p);
				for(int t = 114514; t > x; ){
					t = SegG::lbins(x, c);
//					cout << "xt " << p << ' ' << c << ' ' << t << endl;
					SegS::upd(t + 1, p, SegG::rbins(p));
//					cout << "rbp " << SegG::rbins(p) << endl;
					if(t < x) break;
					c = SegG::get(x, p = t);
					if(c >= y){
						SegS::upd(x, p, SegG::rbins(p));
						break;
					}
				}
			}
		}
		else{
			const int t = min(y, SegG::lbins(x));
//			cout << "t " << t << endl;
			ll ans = t >= x ? ((t + x) * (t - x + 1) >> 1) - (x - 1ll) * (t - x + 1) : 0;
//			cout << "ll " << ans << endl;
			if(t < y) ans += SegS::get(t + 1, y);
//			cout << "QUERY " << (++ nq) << ' ' << x << ' ' << y << ' ';
			cout << ans << '\n';
		}
	}
}
原文