【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)$以内肯定可以最终化为$1$或者$2$,所以线段树记录区间最大值和区间和,$Max\le2$就返回,单点暴力更新,最后线性筛预处理出$d$
时间复杂度$O(m\log n)$
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 300005;
const int M = 1000005;
int a[N];
int lch[N << 2], rch[N << 2];
LL sum[N << 2], Max[N << 2];
LL prime[M], e[M], d[M];
int check[M], cnt;
void seive() {
    memset(check, 0, sizeof(check)); cnt = 0; d[1] = 1; e[1] = 1;
    for(int i = 2; i < M; ++i) {
        if(!check[i]) {prime[cnt++] = i; e[i] = 1; d[i] = 2;}
        for(int j = 0; j < cnt; ++j) {
            if(1LL * i * prime[j] >= M) break;
            check[i * prime[j]] = 1;
            if(i % prime[j] == 0) {
                e[i * prime[j]] = e[i] + 1;
                d[i * prime[j]] = d[i] / (e[i] + 1) * (e[i] + 2);
                break;
            }else {
                d[i * prime[j]] = d[i] * 2; e[i * prime[j]] = 1;
            }
        }
    }
}
inline void pushup(int x) {
    sum[x] = sum[x << 1] + sum[x << 1 | 1];
    Max[x] = max(Max[x << 1], Max[x << 1 | 1]);
}
void build(int x, int l, int r) {
    lch[x] = l; rch[x] = r; sum[x] = Max[x] = 0;
    if(l == r) {
        sum[x] = Max[x] = a[l]; return;
    }
    int mid = (l + r) / 2;
    build(x << 1, l, mid); build(x << 1 | 1, mid + 1, r);
    pushup(x);
}
void update(int x, int l, int r) {
    if(Max[x] <= 2) return;
    if(lch[x] == rch[x]) {
        sum[x] = Max[x] = d[sum[x]]; return;
    }
    int mid = (lch[x] + rch[x]) / 2;
    if(r <= mid) update(x << 1, l, r);
    else if(l > mid) update(x << 1 | 1, l, r);
    else update(x << 1, l, mid), update(x << 1 | 1, mid + 1, r);
    pushup(x);
}
LL query(int x, int l, int r) {
    if(l == lch[x] && rch[x] == r) return sum[x];
    int mid = (lch[x] + rch[x]) / 2;
    if(r <= mid) return query(x << 1, l, r);
    else if(l > mid) return query(x << 1 | 1, l, r);
    else return query(x << 1, l, mid) + query(x << 1 | 1, mid + 1, r);
}
int n, m, t, l, r;
int main() {
    seive();
    // for (int i = 1; i < M; i++)
    //     for (int j = i; j < M; j += i) d[j]++;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    build(1, 1, n);
    for(int i = 1; i <= m; ++i) {
        scanf("%d%d%d", &t, &l, &r);
        if(t == 1) update(1, l, r);
        else printf("%I64d\n", query(1, l, r));
    }
    return 0;
}
												
											【Educational Codeforces Round 37】F. SUM and REPLACE 线段树+线性筛的更多相关文章
- Educational Codeforces Round 23 F. MEX Queries 离散化+线段树
		
F. MEX Queries time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
 - 【Educational Codeforces Round 37 F】SUM and REPLACE
		
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 那个D函数它的下降速度是很快的. 也就是说到最后他会很快的变成2或者1 而D(2)==2,D(1)=1 也就是说,几次操作过后很多数 ...
 - Educational Codeforces Round 64 (Rated for Div. 2)    (线段树二分)
		
题目:http://codeforces.com/contest/1156/problem/E 题意:给你1-n n个数,然后求有多少个区间[l,r] 满足 a[l]+a[r]=max([l, ...
 - Educational Codeforces Round 37
		
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
 - Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements (思维,前缀和)
		
Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements time limit per test 1 se ...
 - Educational Codeforces Round 40  F. Runner's Problem
		
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
 - Educational Codeforces Round 37 A B C D E F
		
A. water the garden Code #include <bits/stdc++.h> #define maxn 210 using namespace std; typede ...
 - codeforces 920  EFG 题解合集 ( Educational Codeforces Round 37 )
		
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
 - [Codeforces]Educational Codeforces Round 37 (Rated for Div. 2)
		
Water The Garden #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h ...
 
随机推荐
- JQ多种刷新方式
			
下面介绍全页面刷新方法:有时候可能会用到 window.location.reload()刷新当前页面. parent.location.reload()刷新父亲对象(用于框架) opener.loc ...
 - strpos   判断字符串是否存在
			
strpos 中为什么要用逗号隔开的原因是因为 防止找出相匹配的中 , 如 查找1 而数组中 存在 12 那么这个结果也是可以找出来的 ,分别在1 前后加个, 就是为了区 ...
 - 使用Office 365前,企业必须要知道的10件事
			
目前的市场上充斥着很多关于微软Office 365的炒作,相信厂商.客户或者企业的都有自己不同的考虑.Office 365是微软云版本的Office,用户可以通过互联网创建一个帐户,付款.下载应用安装 ...
 - JSP的优势
			
以下列出了使用JSP带来的其他好处: 与ASP相比:JSP有两大优势.首先,动态部分用Java编写,而不是VB或其他MS专用语言,所以更加强大与易用.第二点就是JSP易于移植到非MS平台上. 与纯 S ...
 - 【BZOJ3744】Gty的妹子序列 分块+树状数组
			
[BZOJ3744]Gty的妹子序列 Description 我早已习惯你不在身边, 人间四月天 寂寞断了弦. 回望身后蓝天, 跟再见说再见…… 某天,蒟蒻Autumn发现了从 Gty的妹子树(bzo ...
 - Android系统移植与调试之------->MTK 标准编译命令
			
命令格式:./maketek [option] [project] [action] [modules]Option: -t ,-tee :输出log信息到当前终端 -o , -opt=-- ...
 - 17.Django表单验证
			
Django提供了3中方式来验证表单 官网文档:https://docs.djangoproject.com/en/1.9/ref/validators 1.表单字段验证器 a.引入:from dja ...
 - ABAP 发邮件(三)
			
[转自http://blog.sina.com.cn/s/blog_7c7b16000101bnxk.html]SAP ABAP 发邮件方法三(OO) *&------------------ ...
 - Bootstrap学习1--响应式导航栏
			
备注:最新Bootstrap手册:http://www.jqhtml.com/bootstraps-syntaxhigh/index.html <nav class="navbar n ...
 - python cookbook 数据结构
			
保留最后n个元素: from collections import deque def search (lines, pattern, history=): previous_lines = dequ ...