题目链接

CF920F SUM and REPLACE

SUM and REPLACE

给定 \(n\) 个数的数组 \(a\)\(m\) 次操作。操作有两种:
1. 将 \(i\in[l,r]\) 中的所有 \(a_i\) 替换为 \(d(a_i)\)\(d(x)\) 表示 \(x\) 的正约数的个数。
2. 求 \(\displaystyle\sum_{i=l}^r a_i\)

\(1\le n,m\le 3\times 10^5\)\(1\le a_i\le 10^6\)

样例 #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

解题思路

势能线段树

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

  • 时间复杂度:\(O(1000000+m\times log^2n)\)

代码

// 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;
}

原文地址:http://www.cnblogs.com/zyyun/p/16809022.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性