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)ai←gcd(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=l∑rai)mod232.
Limitations
1≤n≤2×1051 \le n \le 2\times 10^51≤n≤2×105
1≤m≤5×1051 \le m \le 5\times 10^51≤m≤5×105
1≤l≤r≤n1 \le l \le r \le n1≤l≤r≤n
1≤ai,x≤10181 \le a_i,x \le 10^{18}1≤ai,x≤1018
Solution
modify\operatorname{modify}modify 操作不好打标记,但一个数 gcd\gcdgcd 至多 logV\log VlogV 次就会变为 111,考虑暴力修改.
维护区间 lcm\operatorname{lcm}lcm,显然当 lcm\operatorname{lcm}lcm 是 xxx 的因数时,对这个区间的修改没有效果.
需要注意 ai,xa_i,xai,x 的范围很大,需要开 int128
,要特判(事实上可以直接忽略掉) lcm>1018\operatorname{lcm} > 10^{18}lcm>1018 的情况.
使用线段树即可,算上 gcd\gcdgcd 后时间复杂度 O(nlognlog2V)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;
}