题解 CF920F 【SUM and REPLACE】
可以事先打表观察每个数的约数个数,观察到如果进行替换,若干次后这个数便会被替换成1。
所以我们可以直接暴力的进行区间修改,若这个数已经到达1或2,则以后就不再修改,用并查集和树状数组进行维护。
这个方法用了P2391 白雪皑皑的思想处理,用并查集标记该点已经不再用替换。
code:
#include<bits/stdc++.h>
#include<cctype>
#define maxn 300010
#define lowbit(x) (x&(-x))
typedef long long ll;
using namespace std;
ll n,m;
ll fa[maxn],k,l,r;
ll a[maxn],tree[maxn*4];
ll d[1000010];
template<typename T> inline void read(T &x)
{
x=0;char c=getchar();bool flag=false;
while(!isdigit(c)){if(c=='-')flag=true;c=getchar();}
while(isdigit(c)){x=x*10+(c^48);c=getchar();}
if(flag)x=-x;
}
int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void add(int x,ll d)
{
while(x<=n)
{
tree[x]+=d;
x+=lowbit(x);
}
}
ll query(int x)
{
ll sum=0;
while(x)
{
sum+=tree[x];
x-=lowbit(x);
}
return sum;
}
int main()
{
for(register int i=1;i<=1000000;++i)
{
for(register int j=i;j<=1000000;j+=i)
d[j]++;//将每个数的约数个数预处理出来,便于后面替换
}
read(n);
read(m);
for(register int i=1;i<=n;++i)
{
read(a[i]);
add(i,a[i]);
fa[i]=i;
}
fa[n+1]=n+1;
while(m--)
{
read(k);
read(l);
read(r);
if(l>r)
swap(l,r);
if(k==1)
{
for(register int i=l;i<=r;)
{
add(i,(ll)d[a[i]]-a[i]);
a[i]=(ll)d[a[i]];
if(a[i]<=2)
fa[i]=i+1;//若这个数已经为1或2,这将其指向它下一个数
if(i==find(i))
i++;
else
i=fa[i];//进行跳转,忽略不再要替换的数
}
}
else
printf("%lld\n",query(r)-query(l-1));
}
return 0;
}
题解 CF920F 【SUM and REPLACE】的更多相关文章
- CF920F SUM and REPLACE 线段树
给你一个数组a_i,D(x)为x的约数个数 两种操作: 1.将[l,r]的a_i替换为D(a_i) 2.输出∑a_i ( l <= i <= r ) 当区间最大值<=2时,就不 ...
- Codefroces 920F SUM and REPLACE(线段树)
SUM and REPLACE 题意:给你n个数,进行m次操作,分别是将区间[l,r]内的所有数替换成自己的因子数 和 对区间[l,r]进行求和. 题解:可以发现2的因子个数还是2,1的因子个数还是1 ...
- Codeforces 920F - SUM and REPLACE
920F - SUM and REPLACE 思路1: 线段树(982 ms) 每个点最多更新6次 代码: #include<bits/stdc++.h> using namespace ...
- Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)
F. SUM and REPLACE time limit per test2 seconds memory limit per test256 megabytes inputstandard inp ...
- Educational Codeforces Round 37-F.SUM and REPLACE题解
一.题目 二.题目链接 http://codeforces.com/contest/920/problem/F 三.题意 给定$N$个范围在$[1, 1e6)$的数字和$M$个操作.操作有两种类型: ...
- Codeforces 920 F SUM and REPLACE
Dicription Let D(x) be the number of positive divisors of a positive integer x. For example, D(2) = ...
- 【Educational Codeforces Round 37 F】SUM and REPLACE
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 那个D函数它的下降速度是很快的. 也就是说到最后他会很快的变成2或者1 而D(2)==2,D(1)=1 也就是说,几次操作过后很多数 ...
- LeetCode题解——Two Sum
题目地址:https://oj.leetcode.com/problems/two-sum/ Two Sum Given an array of integers, find two numbers ...
- LeetCode题解之Sum Root to Leaf Numbers
1.题目描述 2.问题分析 记录所有路径上的值,然后转换为int求和. 3.代码 vector<string> s; int sumNumbers(TreeNode* root) { tr ...
随机推荐
- RabbitMQ:四、跨越集群
跨越集群主要两种插件:Federation和Shovel. 原来的rabbitmq集群将多个broker将多个节点连接起来组成逻辑上独立的单个broker,但是集群也有其局限性:集群内部借助 Erla ...
- django drf 10大请求序列化方法
## 整体单改 路由层.模型层.序列化层不需要做修改,只需要处理视图层:views.py ```python"""1) 单整体改,说明前台要提供修改的数据,那么数据就需要 ...
- .NET高级调试系列-Windbg调试入门篇
Windbg是.NET高级调试领域中不可或缺的一个工具和利器,也是日常我们分析解决问题的必备.准备近期写2篇精华文章,集中给大家分享一下如果通过Windbg进行.NET高级调试. 今天我们来一篇入门的 ...
- jquery入门(3)
4.jQuery中的事件绑定 4.1.事件绑定 on方法绑定 $('#box').on('click',function(){ alert(1); }) 直接绑定 $("#box" ...
- 用阿里的 sketch 插件 FusionDesign 来快速设计中后台
Fusion Design 是阿里推出的新的基于sketch的快速设计方案,很适合快速设计中后台. (1) 到Fusion官方站点 https://fusion.design 注册并创建一个项目. ( ...
- C++ 半同步半异步的任务队列
代码已发布至 HAsyncTaskQueue
- 如何在Mac中配置Python虚拟环境,踩了好多坑
如何在Mac中配置Python虚拟环境 1.安装virtualenv pip3 install virtualenv 2.安装virtualenvwrapper pip3 install virtua ...
- Spring Aop基于注解的实现
一.AspectOriented Programing,面向切面编程. AOP主要用于日志记录,性能统计,安全控制(权限控制),事务处理,异常处理等.将日志记录,性能统计,安全控制,事务处理,异常 ...
- 微信小程序获取index索引值的方法
功能:点击某一项,底部出现粉色边框 首先需要通过 bindtap 为每一个item项绑定一个点击事件,其次需要添加自定义属性 data-* = {{index}} ,以便在函数中获取到被点击item项 ...
- 一起来看 HTML 5.2 中新的原生元素 <dialog>
不到一个月前,HTML 5.2 正式成为 W3C 的推荐标准(REC),其中,推出了一个新的原生模态对话框元素 <dialog>,乍一看,可能感觉它就是一个新增的元素,然而,作者最近在玩的 ...