当前位置: 首页 > news >正文

P10045 [CCPC 2023 北京市赛] 线段树

Description

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

  • add ⁡ ( l , r , x ) \operatorname{add}(l,r,x) add(l,r,x):对每个 i ∈ [ l , r ] i\in[l,r] i[l,r] 执行 a i ← a i + x a_i\gets a_i+x aiai+x.
  • query ⁡ ( l , r ) \operatorname{query}(l,r) query(l,r):求 ( ∏ i = l r a i )   m o d   2 20 (\prod\limits_{i=l}^r a_i) \bmod 2^{20} (i=lrai)mod220.

Limitations

1 ≤ n , m ≤ 2 × 1 0 5 1 \le n,m \le 2\times 10^5 1n,m2×105
1 ≤ l ≤ r ≤ n 1 \le l \le r \le n 1lrn
1 ≤ a < 2 20 1 \le a < 2^{20} 1a<220,且为奇数.
0 ≤ x < 2 20 0 \le x < 2^{20} 0x<220,且为偶数.
4 s , 1 GB 4\text{s},1\text{GB} 4s,1GB

Solution

使用线段树,每个点维护多项式 ∏ i = l r ( a i + x ) \prod\limits_{i=l}^r(a_i+x) i=lr(ai+x),不难发现只用维护 x 0 ∼ x 19 x_0 \sim x_{19} x0x19 项.
不妨设展开后 x i x^i xi 项系数为 f i f_i fi.
考虑区间加 k k k,由于 k k k 为偶数,可以枚举贡献,得到:
f i ′ = ∑ i = 0 19 ( k j × f i + j × ( i + j j ) ) f^{\prime}_i=\sum\limits_{i=0}^{19}(k^j\times f_{i+j}\times \binom{i+j}{j}) fi=i=019(kj×fi+j×(ji+j)).
时间复杂度 O ( c 2 n log ⁡ n ) O(c^2n\log n) O(c2nlogn),其中 c = 20 c=20 c=20 为项数.
& 代替 % 可显著加快速度.

Code

3.47 KB , 3.71 s , 46.03 MB    (in   total,   C++20   with   O2) 3.47\text{KB},3.71\text{s},46.03\text{MB}\;\texttt{(in total, C++20 with O2)} 3.47KB,3.71s,46.03MB(in total, C++20 with O2)

#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 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 << 2);
		    build(0, 0, n - 1, a);
		}
		
		inline void pushup(int u) {
			tr[u].poly = tr[ls(u)].poly * tr[rs(u)].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) {
			if (!tr[u].tag) return;
			apply(ls(u), tr[u].tag), apply(rs(u), 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(u), l, mid, a), build(rs(u), mid + 1, r, a);
			pushup(u);
		}
		
		inline void add(int u, int l, int r, int k) {
			if (l <= tr[u].l && tr[u].r <= r) return apply(u, k);
			pushdown(u);
			const int mid = (tr[u].l + tr[u].r) >> 1;
			if (l <= mid) add(ls(u), l, r, k);
			if (r > mid) add(rs(u), l, r, k);
			pushup(u);
		}
		
		inline int query(int u, int l, int r) {
			if (l <= tr[u].l && tr[u].r <= r) return tr[u].poly[0];
			pushdown(u);
			const int mid = (tr[u].l + tr[u].r) >> 1;
			int res = 1;
			if (l <= mid) res = (res * query(ls(u), l, r)) & mask;
			if (r > mid) res = (res * query(rs(u), l, r)) & mask;
			return res;
		}
	};
}

using poly::init;
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<int> a(n);
	for (int i = 0; i < n; i++) scanf("%d", &a[i]);
	
	init();
	SegTree sgt(a);
	for (int i = 0, op, l, r, x; i < m; i++) {
		scanf("%d %d %d", &op, &l, &r), l--, r--;
		if (op == 1) {
			scanf("%d", &x);
			sgt.add(0, l, r, x);
		}
		else printf("%d\n", sgt.query(0, l, r));
	}
	return 0;
}

相关文章:

  • docker部署dify
  • VNA操作使用学习-14 再测晶振特性
  • 发布第四代液晶电视,TCL引领全新美学境界
  • 理解 RAG 第四部分:RAGA 和其他评估框架
  • Android BLE 权限管理
  • 城市街拍人像自拍电影风格Lr调色教程,手机滤镜PS+Lightroom预设下载!
  • 内网渗透(CSMSF) 构建内网代理的全面指南:Cobalt Strike 与 Metasploit Framework 深度解析
  • latex-二项式括号怎么敲?
  • Linux --centos安装显卡驱动
  • 【AI】AI编程助手:Cursor、Codeium、GitHub Copilot、Roo Cline、Tabnine
  • 零拷贝技术深度解析:原理、实现与性能革命
  • PWM控制电机转速的原理及相关寄存器值计算
  • 防窜货实时监控系统大屏:用python和Streamlit实现的防窜货大屏
  • 【算法学习】最小公倍数问题
  • HTML5前端第七章节
  • 【Go】结构体的基本使用
  • 2025年优化算法:龙卷风优化算法(Tornado optimizer with Coriolis force)
  • Java反序列化CommonsBeanutils无依赖打Shiro
  • 阿里的MNN源码如何编译成so文件,供Android调用
  • 为什么在外置容器时要保证打包方式是war包?
  • 泽连斯基启程前往土耳其
  • 小耳朵等来了春天:公益义诊筛查专家走进安徽安庆
  • 上海虹桥国际咖啡文化节周五开幕,来看Coffeewalk通关攻略
  • 国务院办公厅印发《国务院2025年度立法工作计划》
  • 江西省市场监管局原局长谢来发被双开:违规接受旅游活动安排
  • 长三角议事厅·周报|从模速空间看上海街区化AI孵化模式