920F - SUM and REPLACE

思路1:

线段树(982 ms)

每个点最多更新6次

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ls rt<<1,l,m
#define rs rt<<1|1,m+1,r
#define mem(a,b) memset(a,b,sizeof(a)) const int N=1e6+;
const int INF=0x3f3f3f3f;
int a[N];
int d[N];
ll tree[N<<];
int mx[N<<];
void init(){
for(int i=;i<N;i++){
for(int j=i;j<N;j+=i)d[j]++;
}
}
void push_up(int rt){
mx[rt]=max(mx[rt<<],mx[rt<<|]);
tree[rt]=tree[rt<<]+tree[rt<<|];
}
void build(int rt,int l,int r){
if(l==r){
tree[rt]=mx[rt]=a[l];
return ;
}
int m=(l+r)>>;
build(ls);
build(rs);
push_up(rt);
}
void update(int L,int R,int rt,int l,int r){
if(mx[rt]<=)return ;
if(l==r){
tree[rt]=mx[rt]=d[tree[rt]];
return ;
}
int m=(l+r)>>;
if(L<=m)update(L,R,ls);
if(R>m)update(L,R,rs);
push_up(rt);
}
ll query(int L,int R,int rt,int l,int r){
if(L<=l&&r<=R)return tree[rt];
int m=(l+r)>>;
ll ans=;
if(L<=m)ans+=query(L,R,ls);
if(R>m)ans+=query(L,R,rs);
return ans;
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
int n,m,t,l,r;
init();
cin>>n>>m;
for(int i=;i<=n;i++)cin>>a[i];
build(,,n);
while(m--){
cin>>t>>l>>r;
if(t==)update(l,r,,,n);
else cout<<query(l,r,,,n)<<endl;
}
return ;
}

思路2:

分块(1326 ms)

每个块最多更新6次

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) const int N=1e6+;
int d[N];
int a[N],belong[N],blo,flag[];
ll sum[];
void init(){
for(int i=;i<N;i++){
for(int j=i;j<N;j+=i)d[j]++;
}
}
void solve(int x){
if(flag[x])return ;
flag[x]=;
sum[x]=;
for(int i=(x-)*blo+;i<=x*blo;i++){
a[i]=d[a[i]];
sum[x]+=a[i];
if(a[i]>)flag[x]=;
}
}
void update(int l,int r){
if(belong[l]==belong[r]){
for(int i=l;i<=r;i++){
sum[belong[i]]-=a[i];
a[i]=d[a[i]];
sum[belong[i]]+=a[i];
}
return ;
}
for(int i=l;i<=belong[l]*blo;i++){
sum[belong[i]]-=a[i];
a[i]=d[a[i]];
sum[belong[i]]+=a[i];
}
for(int i=belong[l]+;i<=belong[r]-;i++)solve(i);
for(int i=(belong[r]-)*blo+;i<=r;i++){
sum[belong[i]]-=a[i];
a[i]=d[a[i]];
sum[belong[i]]+=a[i];
}
}
ll query(int l,int r){
ll ans=;
if(belong[l]==belong[r]){
for(int i=l;i<=r;i++)ans+=a[i];
return ans;
}
for(int i=l;i<=belong[l]*blo;i++)ans+=a[i];
for(int i=belong[l]+;i<=belong[r]-;i++)ans+=sum[i];
for(int i=(belong[r]-)*blo+;i<=r;i++)ans+=a[i];
return ans;
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
init();
int n,m,t,l,r;
cin>>n>>m;
blo=sqrt(n);
for(int i=;i<=n;i++)cin>>a[i];
for(int i=;i<=n;i++){
belong[i]=(i-)/blo+;
sum[belong[i]]+=a[i];
}
while(m--){
cin>>t>>l>>r;
if(t==)update(l,r);
else cout<<query(l,r)<<endl;
}
return ;
}

Codeforces 920F - SUM and REPLACE的更多相关文章

  1. Codeforces 920F - SUM and REPLACE 【线段树】

    <题目链接> 题目大意: 给你一个序列,有两个操作,一个是求区间 l - r 的和,另一个是对区间l-r的元素修改值,x=d(x),d(x)为x的因子个数. 解题分析: 因为可能有多次修改 ...

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

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

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

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

  4. Codeforces 920F. SUM and REPLACE / bzoj 3211 花神游历各国

    题目大意: 一个数列 支持两种操作 1 把区间内的数变成他们自己的约数个数 2 求区间和 思路: 可以想到每个数最终都会变成2或1 然后我们可以线段树 修改的时候记录一下每段有没有全被修改成1或2 是 ...

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

    SUM and REPLACE 题意:给你n个数,进行m次操作,分别是将区间[l,r]内的所有数替换成自己的因子数 和 对区间[l,r]进行求和. 题解:可以发现2的因子个数还是2,1的因子个数还是1 ...

  6. Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)

    F. SUM and REPLACE time limit per test2 seconds memory limit per test256 megabytes inputstandard inp ...

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

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

  8. Educational Codeforces Round 37-F.SUM and REPLACE题解

    一.题目 二.题目链接 http://codeforces.com/contest/920/problem/F 三.题意 给定$N$个范围在$[1, 1e6)$的数字和$M$个操作.操作有两种类型: ...

  9. Codeforces 920 F SUM and REPLACE

    Dicription Let D(x) be the number of positive divisors of a positive integer x. For example, D(2) =  ...

随机推荐

  1. 树莓派安装cobbler,自动化安装CentOS

    安装python.相关python模块.apache sudo apt-get install python python2.7 python-django python-netaddr python ...

  2. 【题解】Luogu P2522 [HAOI2011]Problem b

    原题传送门 这题需要运用莫比乌斯反演(懵逼钨丝繁衍) 我们看题面,让求对于区间\([a,b]\)内的整数x和\([c,d]\)内的y,满足$ gcd(x,y)=k$的数对的个数 我们珂以跟容斥原理(二 ...

  3. opencv学习之路(7)、访问图像像素

    一.动态地址访问 #include <opencv2/opencv.hpp> #include<iostream> using namespace cv; using name ...

  4. matlab的三维绘图和四维绘图

    一.三维绘图1.曲线图plot3(X1,Y1,Z1,...):以默认线性属性绘制三维点集(X1,Y1,Z1)确定的曲线plot3(X1,Y1,Z1,LineSpec):以参数LineSpec确定的线性 ...

  5. rabbitmq install erlang faild

    我安装的时候莫名的出现这2个错误 No Presto metadata available for rabbitmq-erlangwarning: /var/cache/yum/x86_64/7/ra ...

  6. Python中对象的引用与复制

    在python进行像b = a这样的赋值时,只会创建一个对a的新引用,使a的引用计数加1,而不会创建新的对象: >>> a = 'xyz' >>> import s ...

  7. Mysqldump 参数大全

    参数 参数说明 --all-databases  , -A 导出全部数据库. mysqldump  -uroot -p --all-databases --all-tablespaces  , -Y ...

  8. linux下安装tomcat和jdk

    1.现在的linux服务器一般自带jdk,先查询是否已经安装jdk rpm -qa | grep java rpm -qa | grep jdk 如上则是没有安装,可以直接跳到步骤X,安装jdk.否则 ...

  9. 微生物增殖|2012年蓝桥杯B组题解析第一题-fishers

    (3')微生物增殖 假设有两种微生物 X 和 Y X出生后每隔3分钟分裂一次(数目加倍),Y出生后每隔2分钟分裂一次(数目加倍). 一个新出生的X,半分钟之后吃掉1个Y,并且,从此开始,每隔1分钟吃1 ...

  10. 今日头条 CEO 张一鸣:面试了 2000 个年轻人,混得好的都有这 5 种特质

    https://blog.csdn.net/qq_35246620/article/details/72801285 博主说:多了解了解总是好的. 正文 张一鸣算是 80 后中绝对的佼佼者.1983 ...