Educational Codeforces Round 37-F.SUM and REPLACE题解
一、题目

二、题目链接
http://codeforces.com/contest/920/problem/F
三、题意
给定$N$个范围在$[1, 1e6)$的数字和$M$个操作。操作有两种类型:
$1$ $l$ $r$:更新区间$[l$, $r]$的数字ai为d[ai]。其中d[i]表示数字i的因子的个数。如:d[1] = 1, d[2] = 2, d[3] = 2, d[4] = 3。
$2$ $l$ $r$:查询区间$[l, r]$的数字和并输出。
四、思路
典型的数据结构题。很明显这是线段树的菜。
对于线段树的每个节点,维护所“管辖”区间的和值和最大值。
更新时,如果当前区间的最大值$maxv<=2$,那么,不需要往下更新下去了。因为$d[1] = 1$, $d[2] = 2$。否则,继续更新下去。这样并不会超时,因为对于一个范围在$[1, 1e6)$的数字而言,反复做$i = d[i]$这样的操作,次数并不会太多。
另外,更新完子区间后,要做上推合并操作。这是线段树很常见的操作。
五、源代码
#pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
;
template <class T> inline void read(T &x) {
int t;
bool flag = false;
')) ;
';
+ t - ';
if(flag) x = -x;
}
typedef struct {
LL maxv, sum;
} Node;
Node data[MAXN * ];
<< ], n, q;
void init() {
;
; i <= N; ++i) {
for(int j = i; j <= N; j += i)
d[j]++;
}
memset(data, , sizeof(data));
}
void pushup(int root){
data[root].sum = data[root << ].sum + data[root << | ].sum;
data[root].maxv = max(data[root << ].maxv, data[root << | ].maxv);
}
, , int r = n) {
if(l > x || r < x)return;
if(l == r && l == x) {
data[root].sum = data[root].maxv = LL(val);
return;
}
;
, l, mid);
| , mid + , r);
pushup(root);
}
, , int r = n) {
if(l > r || l > ur || r < ul)return;
if(ul <= l && ur >= r && data[root].maxv <= 2LL)return;
if(l == r) {
data[root].sum = data[root].maxv = LL(d[data[root].sum]);
return;
}
;
, l, mid);
| , mid + , r);
pushup(root);
}
LL query(, , int r = n) {
if(l > r || l > qr || r < ql)return 0LL;
if(ql <= l && qr >= r)return data[root].sum;
;
, l, mid) + query(ql, qr, root << | , mid + , r);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("Finput.txt", "r", stdin);
#endif // ONLINE_JUDGE
init();
int tmp, type, l, r;
scanf("%d%d", &n, &q);
; i <= n; ++i){
read(tmp);
build(i, tmp);
}
while(q--){
read(type), read(l), read(r);
)update(l, r);
else printf("%lld\n", query(l, r));
}
;
}
Educational Codeforces Round 37-F.SUM and REPLACE题解的更多相关文章
- 【Educational Codeforces Round 37 F】SUM and REPLACE
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 那个D函数它的下降速度是很快的. 也就是说到最后他会很快的变成2或者1 而D(2)==2,D(1)=1 也就是说,几次操作过后很多数 ...
- 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 65 (Rated for Div. 2)题解
Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 64 (Rated for Div. 2)题解
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...
- 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 ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
随机推荐
- FW: How to use Hibernate Lazy Fetch and Eager Fetch Type – Spring Boot + MySQL
原帖 https://grokonez.com/hibernate/use-hibernate-lazy-fetch-eager-fetch-type-spring-boot-mysql In the ...
- 【JMeter】 使用Synchronizing Timer设置请求集合点,实现绝对并发
布局设置说明 参数说明: Number of Simulated Users to Group 每次释放的线程数量.如果设置为0,等同于设置为线程租中的线程数量. Timeout in millise ...
- 后端利用Redis队列及哈希实现定时推送提醒的三个思路
周煦辰 2016年8月31日 本文介绍了一下本人在开发过程中遇到"定时推送提醒"的需求的时候所思考的三种解决方案. 明确问题 首先明确一下这个需求可能包含的几个"坑&qu ...
- Vue.js系列之项目搭建(1)
项目搭建具体步骤如下: 1.安装node 到官网下载安装,我这里是win7系统. (中)https://nodejs.org/zh-cn/ (英)https://nodejs.org/en/ 2.安装 ...
- jsapi微信扫一扫
微信公众号开发--微信JS-SDK扫一扫功能 首先请阅读微信JS-SDK说明文档,了解微信JS的相关说明. 根据官方的使用步骤,关键的有以下几步 绑定域名(很关键) 引入JS文件(很简单) 通过con ...
- python中实现上下文管理器的两种方法
上下文管理器: python中实现了__enter__和__exit__方法的对象就可以称之为上下文管理器 实现方法一举例: def File(object): def __init__(self, ...
- CentOS怎样安装Python3.6
yum install -y openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel安装可能用到的依赖 ...
- JAVA四则运算算法
一.程序要求 解析一般数学算式,实现简单的带括号的加减乘除运算. 二.基本思路 前面两篇介绍了直接解析字符串和用数组容器辅助解析的两种方式,这次再介绍最常用的解析算法——解析后缀表达式(逆波兰表达式) ...
- 十四年风雨路 苹果iMac电脑进化论
1998年起,在CEO乔布斯的带领下,苹果先后创造除了“软糖”iMac G3.“台灯”iMac G4和“像框”G5.iMac凭借其漂亮的外形和强大的性能,迅速赢得了消费者们的喜爱,甚至改变了整个人类社 ...
- POJ2728 Desert King 最优比率生成树
题目 http://poj.org/problem?id=2728 关键词:0/1分数规划,参数搜索,二分法,dinkelbach 参考资料:http://hi.baidu.com/zzningxp/ ...