题意

给定序列$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. EasyDSS流媒体服务器灵活地帮助用户实现摄像机RTSP转RTMP直播功能

    简要描述 今天突然接到国内某上市公司同事打来的技术咨询电话,经过简单的沟通,大概所描述的需求是: 1.目前现场有非常多的摄像机资源需要接入: 2.需要将摄像机的RTSP流转成RTMP流接入到微信小程序 ...

  2. intellij idea 自动生成setter getter

    windows下: alt + insert,然后选择要生成的成员. mac下: command + N

  3. api 爬虫 避免相同 input 在信息未更新 情况下 重复请求重复

  4. js绑定键盘enter事件

    js写法: document.onkeydown = function(event){ var event=document.all?window.event:event; if((event.key ...

  5. 测试站如何最快获取正式站的最新数据: ln -s

    针对静态数据, 比如图片/js等文件, 测试站如何获取最新的呢? ln -s /alidata/www/mysite/uploads /alidata/www/mysite_test/uploads ...

  6. 【python】-- 进程与线程

    进程与线程 一.概念 1.简述: 计算机,所有的指令的操作都是有CPU来负责的,cpu是来负责运算的.OS(操作系统) 调度cpu的最小单位就是线程.程序启动后,从内存中分一块空间,把数据临时存在内存 ...

  7. The Princess and the Pea,摘自iOS应用Snow White and more stories

    Once upon a time there was a prince who wanted to marry a real princess.从前,有个王子想和真正的公主结婚. He looked ...

  8. 【SHARE】WEB前端学习资料

    参考资料:https://github.com/karlhorky/learn-to-program 学习网站:http://www.codecademy.com/learn https://www. ...

  9. Linux下自动清除MySQL日志文件

    MySQL运行过程中会生成大量的日志文件,占用不少空间,修改my.cnf文件配置bin-log过期时间,在Linux下自动清除MySQL日志文件 [mysqld] expire-logs-days= ...

  10. 装箱问题【STL】

    7-9 装箱问题(20 分) 假设有N项物品,大小分别为s​1​​.s​2​​.-.s​i​​.-.s​N​​,其中s​i​​为满足1≤s​i​​≤100的整数.要把这些物品装入到容量为100的一批箱 ...