传送门

线段树入门题。

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


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

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. Testng 常用总结

    自动化测试过程中使用testng管理测试用例是常规的做法,我们需要对它的特性有一个深入的了解和认识: testng的原理: 参考:https://blog.csdn.net/dm_vincent/ar ...

  2. oracle数据库查询出多条数据,合并,之后列转行

    select B.enterprise_code, B.enterprise_name, sum(B.h0_overnum) AS over00, sum(B.h1_overnum) AS over0 ...

  3. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  4. Householder矩阵,Givens矩阵

    householder 矩阵相当于对某一空间中的元素(向量.矩阵)进行镜像变换,但是模值并不发生变化. H=I-2uuT householder矩阵有几个重要的性质: 1 : H-1 = H 2:  ...

  5. TZOJ 3030 Courses(二分图匹配)

    描述 Consider a group of N students and P courses. Each student visits zero, one or more than one cour ...

  6. freetype教程网址

    http://freetype.sourceforge.net/freetype2/docs/reference/ft2-system_interface.html#FT_Stream      ht ...

  7. Android Studio 使用入门

    Android Studio 快捷键 Action Mac OSX Win/Linux 注释代码(//) Cmd + / Ctrl + / 注释代码(/**/) Cmd + Option + / Ct ...

  8. CSS day49

    前端基础之CSS CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CS ...

  9. 深入理解HTTP协议及原理分析

    1. 基础概念篇 1.1 介绍 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(World Wide Web Consortium)和 ...

  10. iOS.-.cxx_destruct

    -.cxx_destruct 方法: 该方法是由编译器生成的方法. 1. “ARC actually creates a -.cxx_destruct method to handle freeing ...