CodeForces - 920F SUM and REPLACE (线段树)
题意:给N个数M次操作,(1<=N,M<=3e5, 1<=ai<=1e6),1是使[L,R]中的每个元素变成其因子的个数之和;2是求[L,R]区间之和
分析:看上去就很线段树的一题,但是却思考了很久。发现1和2即使对其,也不会改变二者的值。而且一个大于2的数进行多次1操作,也最终会退化到2。
先预处理筛出1e6以内各数的质因子个数和。在线段树的节点中维护两个值:区间和以及区间最大值。在update函数中,如果该区间的最大值不超过2,那么该区间没有更新的必要;若超过2,则递归向下找到那个位置,并更新它。
听起来像是退化成了单点更新线段树,其实打个表能发现每个数的因子个数和不会很大,退化几次就成为2了,所以在更新次数很多的情况下,复杂度并不会很高。
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+;
const int INF =0x3f3f3f3f;
const int maxv = 1e6+;
typedef long long LL;
struct SGndoe{
LL sum;
LL mx;
}tree[maxn<<];
LL a[maxn];
LL ans[maxv]; void pre()
{
for(int i=;i<=1e6;++i){
for(int j=;j<=1e6;j+=i){
ans[j]++;
}
}
} void pushup(int root)
{
tree[root].sum =tree[root<<].sum+tree[root<<|].sum;
tree[root].mx = max(tree[root<<].mx,tree[root<<|].mx);
} void build(int root,int L,int R){
if(L==R){
tree[root].sum = tree[root].mx=a[L];
return;
}
int mid =(L+R)>>;
build(root<<,L,mid);
build(root<<|,mid+,R);
pushup(root);
} void update(int root,int l,int r,int L,int R)
{
if(L<=l && R>=r && tree[root].mx<=) return;
else if(l==r){
tree[root].sum = ans[tree[root].sum];
tree[root].mx = tree[root].sum;
return;
}
int mid=(l+r)>>;
if(L<=mid)update(root<<,l,mid,L,R);
if(R>mid) update(root<<|,mid+,r,L,R);
pushup(root);
} LL query(int root,int l,int r,int L,int R)
{
if(L<=l&&r<=R) return tree[root].sum;
int mid=(l+r)>>;
LL res=;
if(L<=mid) res+=query(root<<,l,mid,L,R);
if(mid<R) res+=query(root<<|,mid+,r,L,R);
return res;
} int main(){
int T,N,M,num,t,x;
int L,R;
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
pre();
int cas=;
while(scanf("%d%d",&N,&M)==){
for(int i=;i<=N;++i) scanf("%lld",&a[i]);
build(,,N);
int op;
for(int i=;i<=M;++i){
scanf("%d%d%d",&op,&L,&R);
if(op==) update(,,N,L,R);
else printf("%lld\n",query(,,N,L,R));
}
}
return ;
}
CodeForces - 920F SUM and REPLACE (线段树)的更多相关文章
- Codeforces 920F - SUM and REPLACE
920F - SUM and REPLACE 思路1: 线段树(982 ms) 每个点最多更新6次 代码: #include<bits/stdc++.h> using namespace ...
- 2018.12.15 codeforces 920F. SUM and REPLACE(线段树)
传送门 线段树入门题. 给你一个序列:支持区间修改成自己的约数个数,区间求和. 实际上跟区间开方一个道理. 2的约数个数为2,1的约数个数为1,因此只要区间的最大值小于3就不用修改否则就暴力修改. 因 ...
- 【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) ...
- Codeforces 85D Sum of Medians(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...
- Codeforces 920F. SUM and REPLACE / bzoj 3211 花神游历各国
题目大意: 一个数列 支持两种操作 1 把区间内的数变成他们自己的约数个数 2 求区间和 思路: 可以想到每个数最终都会变成2或1 然后我们可以线段树 修改的时候记录一下每段有没有全被修改成1或2 是 ...
- Codeforces 920F - SUM and REPLACE 【线段树】
<题目链接> 题目大意: 给你一个序列,有两个操作,一个是求区间 l - r 的和,另一个是对区间l-r的元素修改值,x=d(x),d(x)为x的因子个数. 解题分析: 因为可能有多次修改 ...
- CF920F SUM and REPLACE 线段树
给你一个数组a_i,D(x)为x的约数个数 两种操作: 1.将[l,r]的a_i替换为D(a_i) 2.输出∑a_i ( l <= i <= r ) 当区间最大值<=2时,就不 ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
随机推荐
- 【BZOJ】3403: [Usaco2009 Open]Cow Line 直线上的牛(模拟)
http://www.lydsy.com/JudgeOnline/problem.php?id=3404 裸的双端队列.. #include <cstdio> #include <c ...
- VC++Debug查看堆对象内容,即使符号已经超出作用范围
Sometimes you'd like to watch the value of an object (on the heap) even after the symbol goes of sco ...
- Cannot call sendRedirect() after the response has been committed错误;
Cannot call sendRedirect() after the response has been committed提示信息其实很清楚,如果response已经提交过了,就无法再发送sen ...
- 3969 [Mz]平方和【斐波那契平方和】
3969 [Mz]平方和 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 斐波那契数列:f[0 ...
- iOS企业开发In House ipa发布流程
这两天需要发布一个ipa放到网上供其他人安装,需要用到企业级开发者账号.在网上查了一下资料,感觉没有一个比较完善的流程,于是决定把整个流程写下来,供大家参考. 首先详细说明一下我们的目标,我们需要发布 ...
- VMware虚拟机Host-Only(仅主机模式)
转载于:https://www.linuxidc.com/Linux/2016-09/135521p3.htm 三.Host-Only(仅主机模式) Host-Only模式其实就是NAT模式去除了虚拟 ...
- Spring Security OAuth2 授权失败(401) 问题整理
Spring Cloud架构中采用Spring Security OAuth2作为权限控制,关于OAuth2详细介绍可以参考 http://www.ruanyifeng.com/blog/2014/0 ...
- 本地代码推送到github仓库
git 初始化 cd 到需要提交的项目目录下,执行git init 配置用户名和邮箱 git config --global user.name "codingID" git co ...
- 全局安装了express框架,但是无法使用express指令的问题
错误截图: 产生这个错误的原因是:我安装的是express4版本,需要安装express-generator才能使用express命令 将express-generator安装后就都解决了:
- mysql insert中用case
insert into urls(company,counterType,mdUrl,tradeUrl) values('test', CASE 'test'WHEN 'CTP' THEN 1WHEN ...