2018.12.15 codeforces 920F. SUM and REPLACE(线段树)
传送门
线段树入门题。
给你一个序列:支持区间修改成自己的约数个数,区间求和。
实际上跟区间开方一个道理。
2的约数个数为2,1的约数个数为1,因此只要区间的最大值小于3就不用修改否则就暴力修改。
因此要做的就是预处理一个数的约数个数,这个可以nlnnnln_nnlnn预处理。
代码:
#include<bits/stdc++.h>
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (T[p].l+T[p].r>>1)
#define ri register int
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(+ch^48),ch=getchar();
return ans;
}
typedef long long ll;
const int N=3e5+5,M=1e6+6;
int n,m,a[N],num[M],tot=0;
struct Node{ll sum;int mx,l,r;}T[N<<2];
inline void init(){for(ri i=1;i<=1000000;++i)for(ri j=i;j<=1000000;j+=i)++num[j];}
inline void pushup(int p){T[p].sum=T[lc].sum+T[rc].sum,T[p].mx=max(T[lc].mx,T[rc].mx);}
inline void build(int p,int l,int r){
T[p].l=l,T[p].r=r;
if(l==r){T[p].sum=T[p].mx=a[l];return;}
build(lc,l,mid),build(rc,mid+1,r),pushup(p);
}
inline void update(int p,int ql,int qr){
if(T[p].mx<3)return;
if(ql<=T[p].l&&T[p].r<=qr){
if(T[p].l==T[p].r){T[p].sum=T[p].mx=num[T[p].mx];return;}
update(lc,ql,qr),update(rc,ql,qr),pushup(p);
return;
}
if(qr<=mid)update(lc,ql,qr);
else if(ql>mid)update(rc,ql,qr);
else update(lc,ql,mid),update(rc,mid+1,qr);
pushup(p);
}
inline ll query(int p,int ql,int qr){
if(ql<=T[p].l&&T[p].r<=qr)return T[p].sum;
if(qr<=mid)return query(lc,ql,qr);
if(ql>mid)return query(rc,ql,qr);
return query(lc,ql,mid)+query(rc,mid+1,qr);
}
int main(){
n=read(),m=read(),init();
for(ri i=1;i<=n;++i)a[i]=read();
build(1,1,n);
while(m--){
int op=read(),l=read(),r=read();
if(op==1)update(1,l,r);
else cout<<query(1,l,r)<<'\n';
}
return 0;
}
2018.12.15 codeforces 920F. SUM and REPLACE(线段树)的更多相关文章
- Codeforces 920F - SUM and REPLACE
920F - SUM and REPLACE 思路1: 线段树(982 ms) 每个点最多更新6次 代码: #include<bits/stdc++.h> using namespace ...
- CodeForces - 920F SUM and REPLACE (线段树)
题意:给N个数M次操作,(1<=N,M<=3e5, 1<=ai<=1e6),1是使[L,R]中的每个元素变成其因子的个数之和:2是求[L,R]区间之和 分析:看上去就很线段树的 ...
- Codeforces 920F. SUM and REPLACE / bzoj 3211 花神游历各国
题目大意: 一个数列 支持两种操作 1 把区间内的数变成他们自己的约数个数 2 求区间和 思路: 可以想到每个数最终都会变成2或1 然后我们可以线段树 修改的时候记录一下每段有没有全被修改成1或2 是 ...
- 【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 【线段树】
<题目链接> 题目大意: 给你一个序列,有两个操作,一个是求区间 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时,就不 ...
- Tencent Cloud Developers Conference(2018.12.15)
时间:2018.12.15地点:北京朝阳悠唐皇冠假日酒店
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
随机推荐
- Java项目--俄罗斯方块
Java项目--俄罗斯方块 百度盘链接 链接:http://pan.baidu.com/s/1mhQ9SYc 密码:9ujo 一.心得 二.游戏实例 游戏截图 目录结构 三.代码 1.主界面 Tetr ...
- unity项目开发必备插件Asset Hunter 2(资源猎人2)
unity必备插件 Asset Hunter 2 2.4 , 工程项目过大,垃圾太多之后的清洁利器,能识别 ,移除你用不到的资源 扫码时备注或说明中留下邮箱 付款后如未回复请至https://shop ...
- h5外部浏览器直接调起app
1. 安卓端: 其中,scheme必须是小写的,同时要求H5必须是“<a href="appback://">启动应用程序</a> ” 2. h5端完整示例 ...
- http://www.rabbitmq.com/documentation.html
http://www.rabbitmq.com/documentation.html https://www.gitbook.com/book/geewu/rabbitmq-quick/details
- Gym - 101243F Vitamins(思维+并查集)
题意 有三种药丸,白色W>红色R>蓝色B,给你m个约束条件,问你n个药丸的颜色,不能确定颜色输出‘?’ 题解 如果1<2<3,只要找到2就能确定1和3的颜色 如果2=4,只要确 ...
- HDU 2680 Choose the best route(SPFA)
Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...
- Python: re.sub()第二个参数
起源: 问题源于解析kissanime.io这个网站.为反扒抑或是防止ddos攻击,此视频页面,初进去会有个5秒延迟并提交一表单验证.而其表单验证,为下面一段html代码: <form id=& ...
- Django的具体操作(二)
今日内容:用户登录以及分页的实现 views.py # 登录动作 def login_action(request): # 必须继承request if request.method == 'POST ...
- supervisor 使用教程(转)
原文地址:https://word.gw1770df.cc/2016-08-04/linux/supervisor-%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B/ Supe ...
- CSS-解决苹果点击高亮、安卓select灰色背景(select下拉框在IOS中背景变黑、出现阴影问题)
1.在苹果手机上,用点击事件后会出现一个高亮的阴影: 面对click事件的阴影,解决办法: *{ -webkit-tap-highlight-color: rgba(0,0,0,0); -webkit ...