SUM and REPLACE

题意:给你n个数,进行m次操作,分别是将区间[l,r]内的所有数替换成自己的因子数 和 对区间[l,r]进行求和。

题解:可以发现2的因子个数还是2,1的因子个数还是1,所以如果某个数被更新成1或者2之后就不需要再进行更新了。

 #include<bits/stdc++.h>
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int N = 3e5+;
const int M = 1e6+;
ll tree[N<<];
bool ok[N<<];
int num[M], a[N];
int n, m;
void PushUp(int rt)
{
tree[rt] = tree[rt<<] + tree[rt<<|];
ok[rt] = ok[rt<<] && ok[rt<<|];
}
void Build(int l, int r, int rt)
{
if(l == r)
{
tree[rt] = a[l];
if(tree[rt] == || tree[rt] == ) ok[rt] = ;
else ok[rt] = ;
return ;
}
int m = l+r >> ;
Build(lson);
Build(rson);
PushUp(rt);
}
ll Query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R)
return tree[rt];
int m = l+r >> ;
ll ans = ;
if(L <= m) ans += Query(L,R,lson);
if(m < R) ans += Query(L,R,rson);
return ans;
} void Revise(int L, int R, int l, int r, int rt)
{
if(ok[rt]) return ;
if(l == r)
{
tree[rt] = num[tree[rt]];
if(tree[rt] == || tree[rt] == ) ok[rt] = ;
return ;
}
int m = l+r >> ;
if(L <= m) Revise(L,R,lson);
if(m < R) Revise(L,R,rson);
PushUp(rt);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
memset(num, , sizeof(num));
for(int i = ; i < M; i++)
for(int j = i; j < M; j+=i)
num[j]++;
cin >> n >> m;
for(int i = ; i <= n; i++)
cin >> a[i];
Build(,n,);
int q, l, r;
while(m--)
{
cin >> q >> l >> r;
if(q == )
cout << Query(l,r,,n,) << endl;
else
Revise(l,r,,n,);
}
return ;
}

Codefroces 920F SUM and REPLACE(线段树)的更多相关文章

  1. 【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) ...

  2. CF920F SUM and REPLACE 线段树

    给你一个数组a_i​,D(x)为x的约数个数 两种操作: 1.将[l,r]的a_i​替换为D(a_i) 2.输出∑​a_i ( l <= i <= r ) 当区间最大值<=2时,就不 ...

  3. Codeforces 920F - SUM and REPLACE

    920F - SUM and REPLACE 思路1: 线段树(982 ms) 每个点最多更新6次 代码: #include<bits/stdc++.h> using namespace ...

  4. codeforces 1217E E. Sum Queries? (线段树

    codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...

  5. 2018.12.15 codeforces 920F. SUM and REPLACE(线段树)

    传送门 线段树入门题. 给你一个序列:支持区间修改成自己的约数个数,区间求和. 实际上跟区间开方一个道理. 2的约数个数为2,1的约数个数为1,因此只要区间的最大值小于3就不用修改否则就暴力修改. 因 ...

  6. CodeForces - 920F SUM and REPLACE (线段树)

    题意:给N个数M次操作,(1<=N,M<=3e5, 1<=ai<=1e6),1是使[L,R]中的每个元素变成其因子的个数之和:2是求[L,R]区间之和 分析:看上去就很线段树的 ...

  7. Yandex.Algorithm 2011 Round 1 D. Sum of Medians 线段树

    题目链接: Sum of Medians Time Limit:3000MSMemory Limit:262144KB 问题描述 In one well-known algorithm of find ...

  8. Codeforces 85D Sum of Medians(线段树)

    题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...

  9. 【BZOJ4262】Sum 单调栈+线段树

    [BZOJ4262]Sum Description Input 第一行一个数 t,表示询问组数. 第一行一个数 t,表示询问组数. 接下来 t 行,每行四个数 l_1, r_1, l_2, r_2. ...

随机推荐

  1. gulp压缩js文件报错日志

    输出 gulp-uglify 压缩js文件时报错信息 gulp.task('es6', function () { return gulp.src('src/main/webapp/bower_com ...

  2. 百度网盘 人工智能书籍【Tensorflow和深度学习】

    链接:https://pan.baidu.com/s/1ejCvwn08ILI2fMhBEdXR8w 提取码:6pk9

  3. HttpsUtils

    package io.renren.modules.jqr.util; import java.io.BufferedReader; import java.io.InputStream; impor ...

  4. 理解分布式一致性与Raft算法

    理解分布式一致性与Raft算法 永远绕不开的CAP定理 出于可用性及负载方面考虑,一个分布式系统中数据必然不会只存在于一台机器,一致性简单地说就是分布式系统中的各个部分保持数据一致 但让数据保持一致往 ...

  5. Unity经典游戏教程之:冒险岛

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  6. kube-scheduler源码分析

    kubernetes集群三步安装 kube-scheduler源码分析 关于源码编译 我嫌弃官方提供的编译脚本太麻烦,所以用了更简单粗暴的方式编译k8s代码,当然官方脚本在编译所有项目或者夸平台编译以 ...

  7. JavaScript的event对象

    JavaScript的event对象中 event.target指代的是:触发事件的元素 event.currentTarget指代的是:事件绑定的元素 <!DOCTYPE html> & ...

  8. No!No!No! It's not fashion!

    还记得搞怪的hold住姐Miss Lin么,对于人们常规的行为,Miss Lin会挑起夸张的眉毛说:"Oh my God, it's not fashion!".如果程序员圈子里有 ...

  9. MVC+EF Core 完整教程20--tag helper详解

    之前我们有一篇:“动态生成多级菜单”,对使用Html Helper做了详细讲述,并且自定义了一个菜单的 Html Helper: https://www.cnblogs.com/miro/p/5541 ...

  10. 观书有感(摘自12期CSDN)

    CSDN要闻 Visual Studio 将登陆Mac平台 在11月的Connect()上,微软正式发布了Visual Studio For Max预览版,这是微软这一编程工具首次进入苹果平台.Vis ...