题意:给N个数M次操作,(1<=N,M<=3e5, 1<=ai<=1e6),1是使[L,R]中的每个元素变成其因子的个数之和;2是求[L,R]区间之和

分析:看上去就很线段树的一题,但是却思考了很久。发现1和2即使对其,也不会改变二者的值。而且一个大于2的数进行多次1操作,也最终会退化到2。

先预处理筛出1e6以内各数的质因子个数和。在线段树的节点中维护两个值:区间和以及区间最大值。在update函数中,如果该区间的最大值不超过2,那么该区间没有更新的必要;若超过2,则递归向下找到那个位置,并更新它。

听起来像是退化成了单点更新线段树,其实打个表能发现每个数的因子个数和不会很大,退化几次就成为2了,所以在更新次数很多的情况下,复杂度并不会很高。

#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+;
const int INF =0x3f3f3f3f;
const int maxv = 1e6+;
typedef long long LL;
struct SGndoe{
LL sum;
LL mx;
}tree[maxn<<];
LL a[maxn];
LL ans[maxv]; void pre()
{
for(int i=;i<=1e6;++i){
for(int j=;j<=1e6;j+=i){
ans[j]++;
}
}
} void pushup(int root)
{
tree[root].sum =tree[root<<].sum+tree[root<<|].sum;
tree[root].mx = max(tree[root<<].mx,tree[root<<|].mx);
} void build(int root,int L,int R){
if(L==R){
tree[root].sum = tree[root].mx=a[L];
return;
}
int mid =(L+R)>>;
build(root<<,L,mid);
build(root<<|,mid+,R);
pushup(root);
} void update(int root,int l,int r,int L,int R)
{
if(L<=l && R>=r && tree[root].mx<=) return;
else if(l==r){
tree[root].sum = ans[tree[root].sum];
tree[root].mx = tree[root].sum;
return;
}
int mid=(l+r)>>;
if(L<=mid)update(root<<,l,mid,L,R);
if(R>mid) update(root<<|,mid+,r,L,R);
pushup(root);
} LL query(int root,int l,int r,int L,int R)
{
if(L<=l&&r<=R) return tree[root].sum;
int mid=(l+r)>>;
LL res=;
if(L<=mid) res+=query(root<<,l,mid,L,R);
if(mid<R) res+=query(root<<|,mid+,r,L,R);
return res;
} int main(){
int T,N,M,num,t,x;
int L,R;
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
pre();
int cas=;
while(scanf("%d%d",&N,&M)==){
for(int i=;i<=N;++i) scanf("%lld",&a[i]);
build(,,N);
int op;
for(int i=;i<=M;++i){
scanf("%d%d%d",&op,&L,&R);
if(op==) update(,,N,L,R);
else printf("%lld\n",query(,,N,L,R));
}
}
return ;
}

CodeForces - 920F SUM and REPLACE (线段树)的更多相关文章

  1. Codeforces 920F - SUM and REPLACE

    920F - SUM and REPLACE 思路1: 线段树(982 ms) 每个点最多更新6次 代码: #include<bits/stdc++.h> using namespace ...

  2. 2018.12.15 codeforces 920F. SUM and REPLACE(线段树)

    传送门 线段树入门题. 给你一个序列:支持区间修改成自己的约数个数,区间求和. 实际上跟区间开方一个道理. 2的约数个数为2,1的约数个数为1,因此只要区间的最大值小于3就不用修改否则就暴力修改. 因 ...

  3. 【Educational Codeforces Round 37】F. SUM and REPLACE 线段树+线性筛

    题意 给定序列$a_n$,每次将$[L,R]$区间内的数$a_i$替换为$d(a_i)$,或者询问区间和 这题和区间开方有相同的操作 对于$a_i \in (1,10^6)$,$10$次$d(a_i) ...

  4. Codeforces 85D Sum of Medians(线段树)

    题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...

  5. Codeforces 920F. SUM and REPLACE / bzoj 3211 花神游历各国

    题目大意: 一个数列 支持两种操作 1 把区间内的数变成他们自己的约数个数 2 求区间和 思路: 可以想到每个数最终都会变成2或1 然后我们可以线段树 修改的时候记录一下每段有没有全被修改成1或2 是 ...

  6. Codeforces 920F - SUM and REPLACE 【线段树】

    <题目链接> 题目大意: 给你一个序列,有两个操作,一个是求区间 l - r 的和,另一个是对区间l-r的元素修改值,x=d(x),d(x)为x的因子个数. 解题分析: 因为可能有多次修改 ...

  7. CF920F SUM and REPLACE 线段树

    给你一个数组a_i​,D(x)为x的约数个数 两种操作: 1.将[l,r]的a_i​替换为D(a_i) 2.输出∑​a_i ( l <= i <= r ) 当区间最大值<=2时,就不 ...

  8. codeforces 1217E E. Sum Queries? (线段树

    codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...

  9. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

随机推荐

  1. [Tips] Resolve error: server certificate verification failed.

    # sympton: piaoger@piaoger-ubuntu:~/w/temp$ git clone https://mygit/solidmcp/solidmcp.gitCloning int ...

  2. C#中oracle数据库的连接方法

    C#中oracle数据库的连接方法 一.关于数据库的操作 1.数据库连接      有2种:      第一种:古老的方法(较为死板,不利于灵活操作),即用OracleConnection的类来连接 ...

  3. css3 一个简单的静态立方体

    <meta charset="utf8"> <style> .cube { position: absolute; left: 50%; top: 250p ...

  4. QT creator 编辑多个UI 文件 出现 无法解析的外部符号的错误

    创建一般的Qt Gui 程序一般会默认一个UI 文件 ,但是随着应用程序窗口的增多,同时编辑多个UI 界面是必须的. 假设我们已经创建好了一个QTUI的工程,里面已经默认了一个UI文件,但是想在添几个 ...

  5. 【BZOJ】1596: [Usaco2008 Jan]电话网络(树形dp+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1596 一开始交了个貌似正确的dp,wa了. 我只考虑了儿子覆盖的情况,没有考虑父亲QAQ 那么我们要 ...

  6. uGUI动态加载控件位置错误

    最近在使用uGUI时遇到了一个问题,在此记录一下.在Canvas的Render Mode设置为Screen Space-Overlay模式时,动态加载控件是不会发生问题的.但是在Screen Spac ...

  7. point-position目标定位

    双站探测同一目标会构成两条直线:(飞行目标定位2 - ostartech - 博客园 https://www.cnblogs.com/wxl845235800/p/8858116.html) 测角偏差 ...

  8. Laravel5.1 搭建博客 --展示简单的首页

    今天起开始搭建博客,把之前学的东西运用下. 1 创建 配置项目 1.1 创建项目 composer create-project laravel/laravel blog 5.1.1 1.2 配置数据 ...

  9. Charlse 抓包

    下载Charlse:https://www.charlesproxy.com/ 安装使用: https://www.cnblogs.com/ceshijiagoushi/p/6812493.html ...

  10. springboot2.1.1 中集成websocket 单元测试异常

    单元测试在没有集成websocket之前是好好的,当集成websocket之后就出现了下面的异常(只贴出来关键信息): 2019-01-11 10:05:42 [ERROR] [org.springf ...