P9989 [Ynoi Easy Round 2023] TEST_69 Solution

Description

给定序列 a=(a1,a2,⋯ ,an)a=(a_1,a_2,\cdots,a_n)a=(a1,a2,,an),有 mmm 个操作分两种:

  • modify⁡(l,r,x)\operatorname{modify}(l,r,x)modify(l,r,x):对每个 i∈[l,r]i \in [l,r]i[l,r] 执行 ai←gcd⁡(ai,x)a_i \gets \gcd(a_i,x)aigcd(ai,x).
  • query⁡(l,r)\operatorname{query}(l,r)query(l,r):求 (∑i=lrai)mod  232(\sum\limits_{i=l}^r a_i) \mod 2^{32}(i=lrai)mod232.

Limitations

1≤n≤2×1051 \le n \le 2\times 10^51n2×105
1≤m≤5×1051 \le m \le 5\times 10^51m5×105
1≤l≤r≤n1 \le l \le r \le n1lrn
1≤ai,x≤10181 \le a_i,x \le 10^{18}1ai,x1018

Solution

modify⁡\operatorname{modify}modify 操作不好打标记,但一个数 gcd⁡\gcdgcd 至多 log⁡V\log VlogV 次就会变为 111,考虑暴力修改.
维护区间 lcm⁡\operatorname{lcm}lcm,显然当 lcm⁡\operatorname{lcm}lcmxxx 的因数时,对这个区间的修改没有效果.
需要注意 ai,xa_i,xai,x 的范围很大,需要开 int128,要特判(事实上可以直接忽略掉) lcm⁡>1018\operatorname{lcm} > 10^{18}lcm>1018 的情况.
使用线段树即可,算上 gcd⁡\gcdgcd 后时间复杂度 O(nlog⁡nlog⁡2V)O(n \log n \log^2 V)O(nlognlog2V)VVV 为值域.

Code

2.9KB,6.77s,11.15MB  (in total, C++ 20 with O2)2.9\text{KB},6.77\text{s},11.15\text{MB}\;\texttt{(in total, C++ 20 with O2)}2.9KB,6.77s,11.15MB(in total, C++ 20 with O2)
重构了.

// Problem: P9989 [Ynoi Easy Round 2023] TEST_69
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P9989
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
using ui128 = unsigned __int128;
using f4 = float;
using f8 = double;
using f16 = long double;

template<class T>
bool chmax(T &a, const T &b){
	if(a < b){ a = b; return true; }
	return false;
}

template<class T>
bool chmin(T &a, const T &b){
	if(a > b){ a = b; return true; }
	return false;
}

constexpr i64 mod = 1LL << 32, inf = 1e18 + 15;

namespace seg_tree {
	struct Node {
	    int l, r;
	    i64 sum, lcm;
	};
	
	inline int ls(int u) { return 2 * u + 1; }
	inline int rs(int u) { return 2 * u + 2; }
	
	inline i64 calc(i64 a, i64 b) {
	    i64 c = gcd(a, b);
	    i128 ret = (i128)a / c * b;
	    if (ret > 1e18) {
	        return inf;
	    }
	    return (i64)ret;
	}
	
	struct SegTree {
		vector<Node> tr;
		inline SegTree() {}
		inline SegTree(const vector<i64>& a) {
			const int n = a.size();
			tr.resize(n << 1);
			build(0, 0, n - 1, a);
		}
		
		void pushup(int u, int mid) {
		    tr[u].sum = (tr[ls(mid)].sum + tr[rs(mid)].sum) % mod;
		    tr[u].lcm = calc(tr[ls(mid)].lcm, tr[rs(mid)].lcm);
		}
		
		void build(int u, int l, int r, const vector<i64>& a) {
		    tr[u].l = l;
		    tr[u].r = r;
		    if (l == r) {
		        tr[u].sum = tr[u].lcm = a[l];
		        return;
		    }
		    const int mid = (l + r) >> 1;
		    build(ls(mid), l, mid, a);
		    build(rs(mid), mid + 1, r, a);
		    pushup(u, mid);
		}
		
		void update(int u, int l, int r, i64 v) {
		    if (v % tr[u].lcm == 0) return;
		    if (tr[u].l == tr[u].r) {
		        tr[u].lcm = gcd(tr[u].lcm, v);
		        tr[u].sum = tr[u].lcm % mod;
		        return;
		    }
		    const int mid = (tr[u].l + tr[u].r) >> 1;
		    if (l <= mid) update(ls(mid), l, r, v);
		    if (r > mid) update(rs(mid), l, r, v);
		    pushup(u, mid);
		}
		
		i64 query(int u, int l, int r) {
		    if (l <= tr[u].l && tr[u].r <= r) return tr[u].sum;
		    const int mid = (tr[u].l + tr[u].r) >> 1;
		    i64 res = 0;
		    if (l <= mid) res += query(ls(mid), l, r);
		    if (r > mid) res += query(rs(mid), l, r);
		    return res % mod;
		}
		
		inline void range_gcd(int l, int r, i64 x) { update(0, l, r, x); }
		inline i64 range_sum(int l, int r) { return query(0, l, r); }
	};
}
using seg_tree::SegTree;

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int n, m;
	scanf("%d %d", &n, &m);
	
	vector<i64> a(n);
	for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
	
	SegTree sgt(a);
	for (int i = 0, op, l, r; i < m; i++) {
	    scanf("%d %d %d", &op, &l, &r), l--, r--;
	    
	    if (op == 1) {
	        i64 v; scanf("%lld", &v);
	        sgt.range_gcd(l, r, v);
	    }
	    else printf("%lld\n", sgt.range_sum(l, r));
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值