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. 如何成为一名合格的CTO?(转)

    不会走出去公众演说的的攻城狮不是好CTO. 本文来源于微信公众号“线性资本”(ID:LinearVenture) 成为一名合格 CTO 我们投过很多技术型的公司,对于什么是合格的 CTO 有过自己的一 ...

  2. jackson JsonPropertyOrder和@JsonIgnoreProperties注解

    有些时候,我们在和外部系统交互的时候使用了json作为标准的数据交换格式,同时为了安全性考虑,增加了对报文的校验,因此我们需要确保序列化的时候参数有序且不多不少刚好,因为对外的API不像后台和前端交互 ...

  3. ArrayList集合、String[]数组、String字符串

    数组初始化时候必须指定长度,而ArrayList是动态数组,可以根据实际内容改变 //声明stsArr数组并初始化 String[] strArr = new String[]{ "aaa& ...

  4. bzoj 1614 Telephone Lines架设电话线 - 二分答案 - 最短路

    Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N ...

  5. Selenium Webdriver弹出框的种种类型

    普通弹出框 div 一般这种 弹出框都属于dom的一部分,我们查看一下页面源码就可以定位到改弹出框了,然后定位下右上角的那个关闭, 这里有id,所以很方便通过 dr.findElement(By.id ...

  6. uniGUI出新版本了,0.97.0.1081

    uniGUI出新版本了,0.97.0.1081,试用版0.97.0.1075,支持Delphi2006~XE7.下载地址是: http://www.unigui.com/downloads 已在XE6 ...

  7. Python3 tkinter基础 Checkbutton anchor for生成多个控件并西对齐

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. js 根据对象属性对数组进行按字母排序

    $scope.input.sort(compare('ticked','name')); var compare = function(ticked, name){ return function(a ...

  9. Windows FindFirstFile利用

    目前大多数程序都会对上传的文件名加入时间戳等字符再进行MD5,然后下载文件的时候通过保存在数据库里的文件ID读取文件路径,一样也实现了文件下载,这样我们就无法直接得到我们上传的webshell文件路径 ...

  10. Winform选择目录路径与选择文件路径

    https://blog.csdn.net/zaocha321/article/details/52528279 using System.Collections.Generic; using Sys ...