Description
给定序列 a=(a1,a2,⋯ ,an)a=(a_1,a_2,\cdots,a_n)a=(a1,a2,⋯,an),有 mmm 个操作分两种:
- add(l,r,x)\operatorname{add}(l,r,x)add(l,r,x):对每个 i∈[l,r]i\in[l,r]i∈[l,r] 执行 ai←ai+xa_i\gets a_i+xai←ai+x.
- query(l,r)\operatorname{query}(l,r)query(l,r):求 (∏i=lrai) mod 220(\prod\limits_{i=l}^r a_i) \bmod 2^{20}(i=l∏rai)mod220.
Limitations
1≤n,m≤2×1051 \le n,m \le 2\times 10^51≤n,m≤2×105
1≤l≤r≤n1 \le l \le r \le n1≤l≤r≤n
1≤a<2201 \le a < 2^{20}1≤a<220,且为奇数.
0≤x<2200 \le x < 2^{20}0≤x<220,且为偶数.
4s,1GB4\text{s},1\text{GB}4s,1GB
Solution
使用线段树,每个点维护多项式 ∏i=lr(ai+x)\prod\limits_{i=l}^r(a_i+x)i=l∏r(ai+x),不妨设 fi=[xi]∏i=lr(ai+x)f_i=[x^i]\prod\limits_{i=l}^r(a_i+x)fi=[xi]i=l∏r(ai+x).
考虑区间加 kkk,由于 kkk 为偶数,可以用二项式定理,得到:
fi′=∑i=0∑j=0(kj×fi+j×(i+jj))f^{\prime}_i=\sum\limits_{i=0}\sum\limits_{j=0}(k^j\times f_{i+j}\times \binom{i+j}{j})fi′=i=0∑j=0∑(kj×fi+j×(ji+j)).
不难发现只用维护 f0∼f19f_0 \sim f_{19}f0∼f19.
因此时间复杂度 O(c2nlogn)O(c^2n\log n)O(c2nlogn),空间复杂度 O(cn)O(cn)O(cn),其中 c=20c=20c=20 为项数.
要卡常,用 & 1048575 代替 % 1048576 可显著加快速度.
Code
4.39KB,3.62s,30.73MB (in total, C++20 with O2)4.39\text{KB},3.62\text{s},30.73\text{MB}\;\texttt{(in total, C++20 with O2)}4.39KB,3.62s,30.73MB(in total, C++20 with O2)
fastio 删了.
#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 int mask = 1048575, L = 20;
namespace fastio {}
using fastio::read;
using fastio::write;
namespace poly {
array<array<int, L + 1>, L + 1> C;
struct Poly {
vector<int> p;
inline Poly() {}
inline Poly(int val) : p(2) { p[0] = val; p[1] = 1; }
inline void resize(int n) { p.resize(n); }
inline int size() const { return p.size(); }
inline int& operator[](int i) { return p[i]; }
inline int operator[](int i) const { return p[i]; }
};
Poly operator*(const Poly& a, const Poly& b) {
const int n = a.size(), m = b.size();
Poly c; c.resize(min(n + m - 1, L));
for (int i = 0; i < n; i++)
for (int j = 0; j < m && i + j < L; j++)
c[i + j] = (c[i + j] + 1LL * a[i] * b[j]) & mask;
return c;
}
Poly operator+(const Poly& a, int k) {
const int l = a.size();
Poly r; r.resize(l);
for (int i = 0; i < l; i++) {
for (int j = 0, pw = 1; i + j < l; j++) {
r[i] = (((1LL * pw * a[i + j]) & mask) * C[i + j][j] + r[i]) & mask;
pw = (1LL * pw * k) & mask;
}
}
return r;
}
inline void init() {
for (int i = 0; i <= L; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) & mask;
}
}
}
namespace seg_tree {
using poly::Poly;
struct Node {
int l, r, tag;
Poly poly;
};
inline int ls(int u) { return 2 * u + 1; }
inline int rs(int u) { return 2 * u + 2; }
struct SegTree {
vector<Node> tr;
SegTree() {}
SegTree(const vector<int>& a) {
const int n = a.size();
tr.resize(n << 1);
build(0, 0, n - 1, a);
}
inline void pushup(int u, int mid) {
tr[u].poly = tr[ls(mid)].poly * tr[rs(mid)].poly;
}
inline void apply(int u, int k) {
tr[u].tag = (tr[u].tag + k) & mask;
tr[u].poly = tr[u].poly + k;
}
inline void pushdown(int u, int mid) {
if (!tr[u].tag) return;
apply(ls(mid), tr[u].tag), apply(rs(mid), tr[u].tag);
tr[u].tag = 0;
}
inline void build(int u, int l, int r, const vector<int>& a) {
tr[u].l = l, tr[u].r = r;
if (l == r) {
tr[u].poly = 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);
}
inline void add(int u, int l, int r, int k) {
if (l <= tr[u].l && tr[u].r <= r) return apply(u, k);
const int mid = (tr[u].l + tr[u].r) >> 1;
pushdown(u, mid);
if (l <= mid) add(ls(mid), l, r, k);
if (r > mid) add(rs(mid), l, r, k);
pushup(u, mid);
}
inline int query(int u, int l, int r) {
if (l <= tr[u].l && tr[u].r <= r) return tr[u].poly[0];
const int mid = (tr[u].l + tr[u].r) >> 1;
int res = 1;
pushdown(u, mid);
if (l <= mid) res = (res * query(ls(mid), l, r)) & mask;
if (r > mid) res = (res * query(rs(mid), l, r)) & mask;
return res;
}
inline void range_add(int l, int r, int x) { add(0, l, r, x); }
inline int range_prod(int l, int r) { return query(0, l, r); }
};
}
using poly::init;
using seg_tree::SegTree;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
const int n = read<int>(), m = read<int>();
vector<int> a(n);
for (int i = 0; i < n; i++) a[i] = read<int>();
init();
SegTree sgt(a);
for (int i = 0; i < m; i++) {
int op = read<int>(), l = read<int>(), r = read<int>();
l--, r--;
if (op == 1) sgt.range_add(l, r, read<int>());
else write(sgt.range_prod(l, r)), putchar_unlocked('\n');
}
return 0;
}
555

被折叠的 条评论
为什么被折叠?



