CodeForces - 920F SUM and REPLAC(线段树)

题目链接:点击这里

题目大意:
给定一个长度为 n n n 的序列,进行 m m m 个操作,操作有两种:

  1. [ l , r ] [l,r] [l,r] 的每一个元素变成其因子个数
  2. [ l , r ] [l,r] [l,r] 的元素和

题目分析:
此题难点是操作 1 1 1
我们发现 d ( x ) ≤ x d(x)\le x d(x)x ,而且很多都是远小于,所以可以大胆猜测修改到相等,即 x = 1 , 2 x=1,2 x=1,2 所用的次数不会很多
然后提前线筛求出 d ( x ) d(x) d(x) ,然后用线段树暴力修改即可,同时维护区间最大值,如果最大值小于等于 2 2 2 就说明不用修改了,否则就暴力修改
事实证明这个猜测是对的,那么为什么呢:
一个数 x x x 的因子最多有 2 x 2\sqrt x 2x 个,即 d ( x ) ≤ 2 x d(x)\le 2\sqrt x d(x)2x ,所以每次修改相当于对 x x x 开了一次根号,那么显然会在很少次(5)修改后修改到 2 2 2 ,所以均摊起来就是 O ( n log ⁡ n ) O(n\log n) O(nlogn) 的了

具体细节见代码:

//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<unordered_map>
#define ll long long
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3f
//#define int  ll
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int read()
{
	int res = 0,flag = 1;
	char ch = getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch == '-') flag = -1;
		ch = getchar();
	}
	while(ch>='0' && ch<='9')
	{
		res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
		ch = getchar();
	}
	return res*flag;
}
const int maxn = 1e6+5;
const int mod = 1e9+7;
const double pi = acos(-1);
const double eps = 1e-8;
int cnt,pri[maxn],d[maxn],num[maxn];
bool vis[maxn];
void get_d(int n)
{
	d[1] = 1;
	for(int i = 2;i <= n;i++)
	{
		if(!vis[i]) 
		{
			pri[++cnt] = i;
			d[i] = 2;num[i] = 1;
		}
		for(int j = 1;j <= cnt && i*pri[j] <= n;j++)
		{
			vis[i*pri[j]] = true;
			if(i%pri[j] == 0)
			{
				d[i*pri[j]] = d[i]/(num[i]+1)*(num[i]+2);
				num[i*pri[j]] = num[i]+1;
				break;
			}
			d[i*pri[j]] = d[i]*2;
			num[i*pri[j]] = 1;
		}
	}
}

int n,m;
struct node{
	int maxx; ll val;
}a[maxn<<2];
void pushup(int root)
{
	a[root].val = a[root<<1].val+a[root<<1|1].val;
	a[root].maxx = max(a[root<<1].maxx,a[root<<1|1].maxx);
}
void build(int root,int l,int r)
{
	if(l == r)
	{
		a[root].val = a[root].maxx = read();
		return ;
	}
	int mid = l+r>>1;
	build(root<<1,l,mid); build(root<<1|1,mid+1,r);
	pushup(root);
}
void update(int root,int l,int r,int ql,int qr)
{
	if(a[root].maxx <= 2 || l>qr || r<ql) return ;
	if(l == r)
	{
		a[root].val = a[root].maxx = d[a[root].val];
		return ;
	}
	int mid = l+r>>1;
	update(root<<1,l,mid,ql,qr);
	update(root<<1|1,mid+1,r,ql,qr);
	pushup(root);
}
ll query(int root,int l,int r,int ql,int qr)
{
	if(l>qr || r<ql) return 0;
	if(l>=ql && r<=qr) return a[root].val;
	int mid = l+r>>1;
	return query(root<<1,l,mid,ql,qr)+query(root<<1|1,mid+1,r,ql,qr);
}
int main()
{
	get_d(1e6);
	// for(int i = 1;i <= 20;i++) cout<<i<<":"<<d[i]<<endl;
	n = read(),m = read();
	build(1,1,n);
	while(m--)
	{
		int opt = read(),l = read(),r = read();
		if(opt == 1) update(1,1,n,l,r);
		else printf("%lld\n",query(1,1,n,l,r));
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值