题意

给定序列$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. Unity5 怎样做资源管理和增量更新

    工具 Unity 中的资源来源有三个途径:一个是Unity自己主动打包资源.一个是Resources.一个是AssetBundle. Unity自己主动打包资源是指在Unity场景中直接使用到的资源会 ...

  2. vim visual模式 复制

    按ESC再按“V”,进入visual模式 用键盘向左向右箭头选中要复制的文字,按两下"Y"键 再到要粘贴的地方,按“P”键即可. 转自: http://jingyan.baidu. ...

  3. 【python系列】python画报表(Chartkick、Flask)(附中文乱码解决方式)

    chartkick 能够画 javascript 报表, 并且比較美观.可是网上搜了下.非常难找到 python 版本号的,于是查了些资料,摸索了下. 对 Flask 也不非常熟悉,这里就仅仅抛砖引玉 ...

  4. hiho一下 第二周&第四周:从Trie树到Trie图

    hihocoder #1014 题目地址:http://hihocoder.com/problemset/problem/1014 hihocoder #1036 题目地址: http://hihoc ...

  5. 安卓编译出错: Process 'command 'C:\Java\jdk1.8.0_51\bin\java.exe'' finished with non-zero exit value 1 解决!

    安卓编译出错: Process 'command 'C:\Java\jdk1.8.0_51\bin\java.exe'' finished with non-zero exit value 1 解决! ...

  6. 【原创】Hibernate自动生成(2)

    本实战是博主初次学习Java,分析WCP源码时,学习HibernateTools部分的实战,由于初次接触,难免错误,仅供参考,希望批评指正. 开发环境: Eclipse Version: Photon ...

  7. nodejs windows下安装运行

    node 官网下载地址http://nodejs.org/  下载自己对应的版本  ,我下的是windows版本 node-v4.1.1-x64.msi 然后 下一步 下一步 就完成安装了,非常简单, ...

  8. cocos2d-x3.6 生成带类图的离线文档

    我的博客:http://blog.csdn.net/dawn_moon cocos2d-x的官网有点慢,并且最新3.6的在线API文档居然没有了类图,不知道什么原因,之前2.2.6都是有的. 只是能够 ...

  9. abap Excel 导入

    ABAP 将EXECL数据导入SAP内表的几个步骤. 本文转自:http://blog.csdn.net/szlaptop/article/details/8663451   http://www.c ...

  10. 读取ByteBuffer有效的数据

    转:https://zhidao.baidu.com/question/427134449349230532.html说道 ByteBuffer的缓冲区,就需要知道缓冲区的的三个状态1)capacit ...