【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 ...
随机推荐
- java多线程实现复制大文件
有些开发的时候我们经常遇到这样一个问题,对大文件的处理.比如:日志文件.那么十几G的大文件.我们应该如何复制呢? 还有就是希望从本地和远程复制文件,文件都很大,10G级的如何办呢? 在这里我告诉你们, ...
- JSP 开发环境搭建
JSP 开发环境搭建 JSP开发环境是您用来开发.测试和运行JSP程序的地方. 本节将会带您搭建JSP开发环境,具体包括以下几个步骤. 配置Java开发工具(JDK) 这一步涉及Java SDK的下载 ...
- #define的使用方法体会
#define 创建一个宏,该宏是标识符或參数化标识符与标记字符串的关联. 在定义宏之后.编译器可用标记字符串替换源文件里标识符的每一个匹配项. 双击以所有折叠.">语法 #defin ...
- IP地址、子网掩码、网关的关系
网络管理中的IP地址.子网掩码和网关是每个网管必须要掌握的基础知识,只有掌握它,才能够真正理解TCP/IP协议的设置.以下我们就来深入浅出地讲解什么是子网掩码. IP地址的结构 要想理解什么是子网掩码 ...
- easyui首页模板
Easyui首页html代码 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "htt ...
- Manager模块 队列 管道 进程池
Manager模块 作用: 多进程共享变量. Manager的字典类型: 如果value是简单类型,比如int,可以直接赋值给共享变量,并可以后续直接修改 如果value是复杂类型 ,比如list, ...
- Sitemesh3的使用心得
项目中用到了sitemesh3,就把使用心得记下来,至于配置之类的,官方网站都有,这里只是写下自己对它的理解,方便再次理解, sitemesh是基于过滤器的原理,拦截到符合配置文件中配置的路径,然后会 ...
- 【BZOJ4205】卡牌配对 最大流
[BZOJ4205]卡牌配对 Description 现在有一种卡牌游戏,每张卡牌上有三个属性值:A,B,C.把卡牌分为X,Y两类,分别有n1,n2张. 两张卡牌能够配对,当且仅当,存在至多一项属性值 ...
- ACboy needs your help(简单DP)
HDU 1712 Problem Description ACboy has N courses this term, and he plans to spend at most M days on ...
- 大华NVR设备接分别入宇视摄像机Onvif和RTSP主子码流的方案说明
需求提要 1.各个内网现场有多种网络摄像机IPC和网络硬盘录像机NVR设备: 2.需要将这些设备统一接入到云端中心平台,进行统一的视频直播和录像回放管理: 3.由于目前IPC设备都属于高清设备,主码流 ...