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 ...
随机推荐
- python判断key是否存在
d = {: , : , : , : , : , : } def is_key_present(x): if x in d: print('Key is present in the dictiona ...
- Tp5,Service使用
C层,操控数据库,并处理页面数据展示. M层,纯粹的操作自己所对应的数据库. Service层,可以通用的处理一些逻辑计算,也可以将复杂的数据表处理整合到一起,也可以将复杂的业务逻辑整合到一起. 创建 ...
- Java语言的垃圾回收机制
java语言从诞生开始,一个吸引人眼球的功能就是垃圾回收,想一想C++中时不时的内存泄漏,当时感觉写java代码直是一种享受呀. 和.NET的引用计数不同,java的垃圾回收机制采取的是有向图 ...
- 2016 CCPC Hangzhou Onsite
A:题意:n个格子排成一排,每个a[i],要求重排成k个,每个人数相同,合并两个和划分成两个(可以不等)都是花费为1,问最小花费 题解:从前往后贪心即可,由于哪个地方忘开ll,wa了,全改成ll就过了 ...
- [转载]java正则表达式
转载自:http://butter.iteye.com/blog/1189600 1.正则表达式的知识要点1.正则表达式是什么?正则表达式是一种可以用于模式匹配和替换的强有力的工具.2.正则表达式的优 ...
- VS中的生成事件
1:为什么需要使用生成事件? 在实际开发过程中,一个公共使用的类库,在项目生成DLL后需要被复制到不同的目录下被引用,是不是觉得每次生成之后都需要人工复制是很麻烦的一件事情 我们可以利用VS中的项目生 ...
- 记录下返回list给前端 遇到 $ref":"$.data.*** 问题
1.通过对象返回给前端,对象里面有三个list 2.一个父list 2个子list 子list中的对象 是通过for循环父list按照某个条件放进去的 3.直接放进去会出现 $ref":& ...
- Nginx笔记02-nginx常用参数配置说明
nginx的主配置文件是nginx.conf,这里主要针对这个文件进行说明 1.主配置文件nginx.conf 2.nginx配置文件的结构 从上面的配置文件中我们可以总结出nginx配置文件的基 ...
- hibernate的一些缺陷(转)
例如用户在系统中,保存的信息包括简要信息(用户名.联系电话.Email.性别)和一些图像信息(照片). 但是在系统设计时,我的设计方式都是遵循业务的需要,设计一个“用户”类,包含用户名. ...
- python的单元测试代码编写流程
单元测试: 单元测试是对单独的代码块分别进行测试, 以确保它们的正确性, 单元测试主要还是由开发人员来做, 其余的集成测试和系统测试由专业的测试人员来做. python的单元测试代码编写主要记住以下几 ...