CF920F SUM and REPLACE

题目链接

CF920F SUM and REPLACE

SUM and REPLACE

给定 n 个数的数组 am 次操作。操作有两种:
1. 将 i[l,r] 中的所有 ai 替换为 d(ai)d(x) 表示 x 的正约数的个数。
2. 求 i=lrai

1n,m3×1051ai106

样例 #1

样例输入 #1

7 6 6 4 1 10 3 2 4 2 1 7 2 4 5 1 3 5 2 4 4 1 5 7 2 1 7

样例输出 #1

30 13 4 22

解题思路

势能线段树

先用线性筛预处理出 1106 中每个数的约数个数 D[i],约数个数为 (α1+1)×(α2+1)××(αk+1),线性筛时设置一个变量 f[i] 表示 i 的最小素因子出现的次数,处理 i×prime[j] 时,如果 imodprime[j]=0,则说明 i 中已经含有 prime[j] 这个最小质因子,则 f[i×prime[j]]=f[i]+1D[i] 先除掉其原来最小质因子的贡献再乘上 f[i×prime[j]]+1 即为 D[i×prime[j],否则如果 imodprime[j]0,说明 primes[j]i×primes[j] 的唯一一个最小质因子
然后由于 D[1]=1,D[2]=2 这两个数修改时不影响答案,所以只需要修改那些 mx>2 的区间,每个数修改次数不会很多,线段树暴力修改即可

  • 时间复杂度:O(1000000+m×log2n)

代码

// Problem: F. SUM and REPLACE // Contest: Codeforces - Educational Codeforces Round 37 (Rated for Div. 2) // URL: https://codeforces.com/problemset/problem/920/F // Memory Limit: 256 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> // #define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=3e5+5,M=1e6+6; int n,m,a[N],D[M]; int cnt,prime[N],f[M]; bool v[M]; void primes(int n) { D[1]=f[1]=1; for(int i=2;i<=n;i++) { if(!v[i]) { cnt++; prime[cnt]=i; D[i]=2,f[i]=1; } for(int j=1;j<=cnt&&i*prime[j]<=n;j++) { v[i*prime[j]]=true; if(i%prime[j]==0) { f[i*prime[j]]=f[i]+1; D[i*prime[j]]=D[i]/(f[i]+1)*(f[i*prime[j]]+1); break; } f[i*prime[j]]=1; D[i*prime[j]]=D[i]*D[prime[j]]; } } } struct Tr { int l,r,mx; LL sum; }tr[N<<2]; void pushup(int p) { tr[p].sum=tr[p<<1].sum+tr[p<<1|1].sum; tr[p].mx=max(tr[p<<1].mx,tr[p<<1|1].mx); } void build(int p,int l,int r) { tr[p]={l,r}; if(l==r) { tr[p].sum=tr[p].mx=a[l]; return ; } int mid=l+r>>1; build(p<<1,l,mid),build(p<<1|1,mid+1,r); pushup(p); } void change(int p,int l,int r) { if(tr[p].l==tr[p].r) { tr[p].sum=tr[p].mx=D[tr[p].sum]; return ; } int mid=tr[p].l+tr[p].r>>1; if(l<=mid&&tr[p<<1].mx>2)change(p<<1,l,r); if(r>mid&&tr[p<<1|1].mx>2)change(p<<1|1,l,r); pushup(p); } LL ask(int p,int l,int r) { if(l<=tr[p].l&&tr[p].r<=r)return tr[p].sum; LL res=0; int mid=tr[p].l+tr[p].r>>1; if(l<=mid)res+=ask(p<<1,l,r); if(r>mid)res+=ask(p<<1|1,l,r); return res; } int main() { primes(M-1); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%d",&a[i]); build(1,1,n); while(m--) { int op,l,r; scanf("%d%d%d",&op,&l,&r); if(op==1)change(1,l,r); else printf("%lld\n",ask(1,l,r)); } return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16809022.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2021-10-20 阶乘分解
点击右上角即可分享
微信分享提示