2018.12.15 codeforces 920F. SUM and REPLACE(线段树)
传送门
线段树入门题。
给你一个序列:支持区间修改成自己的约数个数,区间求和。
实际上跟区间开方一个道理。
2的约数个数为2,1的约数个数为1,因此只要区间的最大值小于3就不用修改否则就暴力修改。
因此要做的就是预处理一个数的约数个数,这个可以nlnnnln_nnlnn预处理。
代码:
#include<bits/stdc++.h>
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (T[p].l+T[p].r>>1)
#define ri register int
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(+ch^48),ch=getchar();
return ans;
}
typedef long long ll;
const int N=3e5+5,M=1e6+6;
int n,m,a[N],num[M],tot=0;
struct Node{ll sum;int mx,l,r;}T[N<<2];
inline void init(){for(ri i=1;i<=1000000;++i)for(ri j=i;j<=1000000;j+=i)++num[j];}
inline void pushup(int p){T[p].sum=T[lc].sum+T[rc].sum,T[p].mx=max(T[lc].mx,T[rc].mx);}
inline void build(int p,int l,int r){
T[p].l=l,T[p].r=r;
if(l==r){T[p].sum=T[p].mx=a[l];return;}
build(lc,l,mid),build(rc,mid+1,r),pushup(p);
}
inline void update(int p,int ql,int qr){
if(T[p].mx<3)return;
if(ql<=T[p].l&&T[p].r<=qr){
if(T[p].l==T[p].r){T[p].sum=T[p].mx=num[T[p].mx];return;}
update(lc,ql,qr),update(rc,ql,qr),pushup(p);
return;
}
if(qr<=mid)update(lc,ql,qr);
else if(ql>mid)update(rc,ql,qr);
else update(lc,ql,mid),update(rc,mid+1,qr);
pushup(p);
}
inline ll query(int p,int ql,int qr){
if(ql<=T[p].l&&T[p].r<=qr)return T[p].sum;
if(qr<=mid)return query(lc,ql,qr);
if(ql>mid)return query(rc,ql,qr);
return query(lc,ql,mid)+query(rc,mid+1,qr);
}
int main(){
n=read(),m=read(),init();
for(ri i=1;i<=n;++i)a[i]=read();
build(1,1,n);
while(m--){
int op=read(),l=read(),r=read();
if(op==1)update(1,l,r);
else cout<<query(1,l,r)<<'\n';
}
return 0;
}
2018.12.15 codeforces 920F. SUM and REPLACE(线段树)的更多相关文章
- Codeforces 920F - SUM and REPLACE
920F - SUM and REPLACE 思路1: 线段树(982 ms) 每个点最多更新6次 代码: #include<bits/stdc++.h> using namespace ...
- CodeForces - 920F SUM and REPLACE (线段树)
题意:给N个数M次操作,(1<=N,M<=3e5, 1<=ai<=1e6),1是使[L,R]中的每个元素变成其因子的个数之和:2是求[L,R]区间之和 分析:看上去就很线段树的 ...
- Codeforces 920F. SUM and REPLACE / bzoj 3211 花神游历各国
题目大意: 一个数列 支持两种操作 1 把区间内的数变成他们自己的约数个数 2 求区间和 思路: 可以想到每个数最终都会变成2或1 然后我们可以线段树 修改的时候记录一下每段有没有全被修改成1或2 是 ...
- 【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) ...
- Codeforces 85D Sum of Medians(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...
- Codeforces 920F - SUM and REPLACE 【线段树】
<题目链接> 题目大意: 给你一个序列,有两个操作,一个是求区间 l - r 的和,另一个是对区间l-r的元素修改值,x=d(x),d(x)为x的因子个数. 解题分析: 因为可能有多次修改 ...
- CF920F SUM and REPLACE 线段树
给你一个数组a_i,D(x)为x的约数个数 两种操作: 1.将[l,r]的a_i替换为D(a_i) 2.输出∑a_i ( l <= i <= r ) 当区间最大值<=2时,就不 ...
- Tencent Cloud Developers Conference(2018.12.15)
时间:2018.12.15地点:北京朝阳悠唐皇冠假日酒店
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
随机推荐
- 打印低头思故乡 java
public static void main(String args[][){ char poet[] = str.tocharArray(); int pos = 18; while(true){ ...
- 200. Number of Islands (Graph)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 【Android端 adb相关】adb相关总结
一.什么是adb? adb的全称是:Android Debug Bridge,adb命令的构成是三部分,分别是:服务器.客户端.后台程序: (1)客户端:一个在PC上运行的客户端.可以通过shell端 ...
- 洛谷 P1342 请柬(SPFA)
题目描述 在电视时代,没有多少人观看戏剧表演.Malidinesia古董喜剧演员意识到这一事实,他们想宣传剧院,尤其是古色古香的喜剧片.他们已经打印请帖和所有必要的信息和计划.许多学生被雇来分发这些请 ...
- PTA 7-33 地下迷宫探索(深搜输出路径)
地道战是在抗日战争时期,在华北平原上抗日军民利用地道打击日本侵略者的作战方式.地道网是房连房.街连街.村连村的地下工事,如下图所示. 我们在回顾前辈们艰苦卓绝的战争生活的同时,真心钦佩他们的聪明才智. ...
- java命令启动jacocoagent及生成报告
启动jacocoagent: java -Xms1024m -Xmx2048m -XX:-UseGCOverheadLimit -Ddruid.keepAlive=true -javaagent:/h ...
- Xshell无法使用root远程登录Ubuntu16服务器
修改/etc/ssh/sshd_config文件,把PermitRootLogin Prohibit-password 添加#注释掉 新添加:PermitRootLogin yes 2. 重启ssh服 ...
- Exploring the world of Android :: Part 2
September 17th, 2009 by Tom van Zummeren | And I’m back! Reporting live on the glorious adventures i ...
- vue 自定义组件directives
自定义指令:以v开头,如:v-mybind. 代码示例: <input v-mybind /> directives:{ mybind:{ bind:function (el) { el. ...
- vue2.0 动画
//先来一个简单的入场 <template> <div id="box"> <input type="button" value= ...