【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 ...
随机推荐
- Idea中优化Markdown Support显示效果
转自:https://www.jianshu.com/p/d093c42a8c29 因为工作中为提高工作效率,我一般习惯于直接在`idea`中使用`markdow support`插件来进行相关文档的 ...
- spring mvc数据验证
今天来说一下.前段验证,与后端数据验证.大家都知道.在我们.注册与登陆的时候,往往需要对数据进行效验.那么前段我们都知道,可以使用,js去做处理. 今天主要讲解.后端的数据效验.这里我们采用Hiber ...
- python 求下个月的最后一天
[1]根据当前月求上个月.下个月的最后一天 (1)求当前月最后一天 (2)求前一个月的最后一天 (3)求下一个月的最后一天 学习示例与应用实例,代码如下: #!/usr/bin/python3 #-* ...
- vue实践---vue不依赖外部资源实现简单多语
vue使用多语,最常见的就是 vue-i18n, 但是如果开发中的多语很少,比如就不到10个多语,这样就没必要引入vue-i18n了, 引入了反正导致代码体积大了,这时候单纯用vue实现多语就是比较好 ...
- react-native create-react-app创建项目报错SyntaxError: Unexpected end of JSON input while parsing near '...ttachment":false,"tar' npm代理
SyntaxError: Unexpected end of JSON input while parsing near '...ttachment":false,"tar' 错误 ...
- 微信小程序之如何注册微信小程序
所有文章均是CSDN博客所看,已按照作者要求,注明出处了,感谢作者的整理! 博客文章地址:http://blog.csdn.net/michael_ouyang/article/details/546 ...
- MySQL复制经常使用拓扑结构具体解释
复制的体系结构有下面一些基本原则: (1) 每一个slave仅仅能有一个master: (2) 每一个slave仅仅能有一个唯一的serverID: (3) 每一个master能够有 ...
- Brain Network (easy)(并查集水题)
G - Brain Network (easy) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- vue 计算属性和监听器
一.计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div> {{ message.split('').rev ...
- 我的Android进阶之旅------>启动Activity的标准Action和标准Category
Android内部提供了大量标准的Action和Category常量. 除了参考本文外,您还可以参考了以下链接: http://developer.android.com/reference/andr ...