传送门

线段树入门题。

给你一个序列:支持区间修改成自己的约数个数,区间求和。


实际上跟区间开方一个道理。

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(线段树)的更多相关文章

  1. Codeforces 920F - SUM and REPLACE

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

  2. CodeForces - 920F SUM and REPLACE (线段树)

    题意:给N个数M次操作,(1<=N,M<=3e5, 1<=ai<=1e6),1是使[L,R]中的每个元素变成其因子的个数之和:2是求[L,R]区间之和 分析:看上去就很线段树的 ...

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

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

  4. 【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) ...

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

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

  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. Tencent Cloud Developers Conference(2018.12.15)

    时间:2018.12.15地点:北京朝阳悠唐皇冠假日酒店

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

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

随机推荐

  1. TOJ4127: Root of String

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4127 4127: Root of ...

  2. 204. Count Primes (Integer)

    Count the number of prime numbers less than a non-negative number, n. 思路:寻找质数的方法 class Solution { pu ...

  3. windows phpstudy如何扩展MongoDB

    phpstudy如何扩展MongoDB 作者: default|标签:phpstudy MongoDB PHP|2017-9-9 10:17 phpstudy扩展MongoDB 前置工作安装PHPst ...

  4. 项目总结12:bootstrap-select下拉框模糊搜索

    bootstrap select下拉框模糊搜索 关键字 bootstrap-select 下拉框模糊搜索 正文(直接上源码) <%@ page language="java" ...

  5. 如何开发简单的javaweb项目,jsp+javabean+servlet

    一.相关的软件下载和环境配置 1.下载并配置JDK. 2.下载eclipse. 3.下载并配置apache-tomcat(服务器). 4.下载MySQL(数据库). 5.下载Navicat for M ...

  6. AttributeError: 'WebElement' object has no attribute 'send_keys'

    这个是没问题的代码:用来打开谷歌搜索cheese并退出 from selenium import webdriver from selenium.common.exceptions import Ti ...

  7. 视觉slam十四讲

    对这个的学习一直都在,感觉到了这本书很强大呀!!! ch2---安装ubuntu:安装kdevelop. ch3---安装eigen3---几何模块:安装Pangolin可视化. ch4---安装So ...

  8. css让内层div自动撑开外层div

    .clear{clear:both;height:0px;font-size: 1px;line-height: 0px;} <div class="audi_items"& ...

  9. 故障处理分析:华为5885v3 cable/ Interconnect (LEFT Panel)

    故障现象: 处理结果: 1.重新把插左前面板,重启,故障消失.

  10. 解决 win 7 64 位 vs2010 调试silverlight项目无法加载,提示更新developer ,跟新报 消息 ID: 1517 已安装了 Silverlight 的 64 位版本

    出现上面的问题是我们安装的silverlight的版本和系统给的silverlight下载的版本冲突, 解决的方法是,首先卸载Silverlight runtime(也就是默认的silverlight ...