题意

给定序列$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 线段树+线性筛的更多相关文章

  1. 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 ...

  2. 【Educational Codeforces Round 37 F】SUM and REPLACE

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 那个D函数它的下降速度是很快的. 也就是说到最后他会很快的变成2或者1 而D(2)==2,D(1)=1 也就是说,几次操作过后很多数 ...

  3. 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, ...

  4. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

  5. 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 ...

  6. Educational Codeforces Round 40 F. Runner's Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

  7. 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 ...

  8. codeforces 920 EFG 题解合集 ( Educational Codeforces Round 37 )

    E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. [Codeforces]Educational Codeforces Round 37 (Rated for Div. 2)

    Water The Garden #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h ...

随机推荐

  1. python 求下个月的最后一天

    [1]根据当前月求上个月.下个月的最后一天 (1)求当前月最后一天 (2)求前一个月的最后一天 (3)求下一个月的最后一天 学习示例与应用实例,代码如下: #!/usr/bin/python3 #-* ...

  2. 【BZOJ4205】卡牌配对 最大流

    [BZOJ4205]卡牌配对 Description 现在有一种卡牌游戏,每张卡牌上有三个属性值:A,B,C.把卡牌分为X,Y两类,分别有n1,n2张. 两张卡牌能够配对,当且仅当,存在至多一项属性值 ...

  3. wcf利用IDispatchMessageInspector实现接口监控日志记录和并发限流

    一般对于提供出来的接口,虽然知道在哪些业务场景下才会被调用,但是不知道什么时候被调用.调用的频率.接口性能,当出现问题的时候也不容易重现请求:为了追踪这些内容就需要把每次接口的调用信息给完整的记录下来 ...

  4. 怎样过滤跨站恶意脚本攻击(XSS)

    什么是XSS? XSS(Cross Site Scripting),即跨站脚本攻击,是一种常见于web application中的计算机安全漏洞.XSS通过在用户端注入恶意的可运行脚本,若服务器端对用 ...

  5. How to avoid Over-fitting using Regularization?

    http://www.mit.edu/~9.520/scribe-notes/cl7.pdf https://en.wikipedia.org/wiki/Bayesian_interpretation ...

  6. Delphi 64与32位的差异

    Delphi 64与32位的差异   最近,Delphi推出了64位预览版本, 我做为一个忠实的Delphier, 看到这消息后,第一时间学习,并写下这个做为以后的参考资料. 相同点: 在Delphi ...

  7. python+NLTK 自然语言学习处理六:分类和标注词汇一

    在一段句子中是由各种词汇组成的.有名词,动词,形容词和副词.要理解这些句子,首先就需要将这些词类识别出来.将词汇按它们的词性(parts-of-speech,POS)分类并相应地对它们进行标注.这个过 ...

  8. jauery table

    $("#tableData tr:gt(0)").each(function() { }//橘色部分是查找id为tableData的DataTable里面除第一行以外的行

  9. JVM性能优化, Part 3 垃圾回收

    ImportNew注:本文是JVM性能优化 系列-第3篇-<JVM性能优化, Part 3 —— 垃圾回收> 第一篇 <JVM性能优化, Part 1 ―― JVM简介 > 第 ...

  10. LeetCode:算法特辑——二分搜索

    LeetCode:算法特辑——二分搜索 算法模板——基础 int L =0; int R =arr.length; while(L<R) { int M = (R-L)/2+L; if(arr[ ...