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 ...
随机推荐
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister
http://codeforces.com/contest/812/problem/B 题意: 有n层楼,每层楼有m个房间,1表示灯开着,0表示灯关了.最两侧的是楼梯. 现在每从一个房间移动到另一个房 ...
- adb相关指令
adb rootadb connect 172.16.20.162 //通过adb连接远程设备adb pull /data/data/com.xiaomi.voicecontrol /Users/r ...
- Java语言的垃圾回收机制
java语言从诞生开始,一个吸引人眼球的功能就是垃圾回收,想一想C++中时不时的内存泄漏,当时感觉写java代码直是一种享受呀. 和.NET的引用计数不同,java的垃圾回收机制采取的是有向图 ...
- 【转】使用sklearn做特征工程
1 特征工程是什么? 有这么一句话在业界广泛流传:数据和特征决定了机器学习的上限,而模型和算法只是逼近这个上限而已.那特征工程到底是什么呢?顾名思义,其本质是一项工程活动,目的是最大限度地从原始数据中 ...
- 雷林鹏分享:Ruby CGI Cookies
Ruby CGI Cookies HTTP协议是无状态协议.但对于一个商业网站,它需要保持不同的页面间的会话信息. 如用户在网站注册过程中需要跳转页面,但又要保证之前填写的信息部丢失. 这种情况下 C ...
- VAE--就是AutoEncoder的编码输出服从正态分布
花式解释AutoEncoder与VAE 什么是自动编码器 自动编码器(AutoEncoder)最开始作为一种数据的压缩方法,其特点有: 1)跟数据相关程度很高,这意味着自动编码器只能压缩与训练数据相似 ...
- java实现的18位身份证格式验证算法
公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成.排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码.1.地址码表示编码对象常住户口所在县(市. ...
- 让FireFox支持window.event属性
场景描述: 在用户行为采集的过程中,需要侦听window下的event对象,根据事件类型做相应的过滤处理,但在firefox下window.event是未定义的: 问题分析: 要想获取event属性共 ...
- halcon之屌炸天的变形匹配(1)
在日常工程应用中,我们通常通过halcon的 shape-based matching(形状匹配)进行各种定位, 如以前文章介绍的这样,理解各个参数并灵活应用通常就能得到很好的匹配效果和匹配速度, 当 ...
- hosts 配置
hosts 配置 在windows上比较好的方法就是在本地配制hosts,在windows/system32/drivers/etc/hosts 下,增加 127.0.0.1 m.t.XXXX.com ...