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. 初识 GitHub

    初识 GitHub 一.注册账号 GitHub 官网:https://github.com/ 点击右上角sign up,进行注册,注册界面如下: 填写用户名,邮箱地址,密码,下滑点击绿色按钮:Crea ...

  2. opencv学习之路(14)、形态学之膨胀腐蚀

    一.膨胀腐蚀概述(对高亮部分进行操作) 二.膨胀 三.腐蚀 四.代码 1.查看结构元素 #include<opencv2/opencv.hpp> #include<iostream& ...

  3. kaggle竞赛_mnist_10%

      主要是通过mnist了解kaggle的操作细节,最终这里的结果为: 引入必须的库¶ import pandas as pd import numpy as np import matplotlib ...

  4. NT1_keras下搭建一个3层模型并且修改。

    In [1]: import keraskeras.__version__ C:\ProgramData\Anaconda3\lib\site-packages\h5py\__init__.py:36 ...

  5. Objective-C 【init/initWithFrame调用机制】

    这是一个自定义view: @implementation MyView - (instancetype)init { if (self = [super init]) { NSLog(@"调 ...

  6. 如何评价 React 实现的前端 UI 库 material-ui?

    作者:知乎用户链接:https://www.zhihu.com/question/51040975/answer/208582603来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  7. Windows10状态栏右下角的上升三角号没有了

    闲着没事装了360和电脑管家,捣鼓了下里面的功能,好像是弄了桌面整理和主题之后出现了这个问题,刚开始还以为因为清理卸载软件把系统的这项功能给卸载了,其实原因很简单,相信看完解决这个问题的方案你就明白了 ...

  8. AndroidStudio Gradle下载速度慢解决方法

    1.在软件里点开工程文件下的 build.gradle 2..在 buildscript 和 allprojects 的 repositories 中分别注释掉 jcenter() 3.在 build ...

  9. 文件IO(2)

    Lseek:       ***************************************************************************    实验一:     ...

  10. Codeforces 855C. Helga Hufflepuff's Cup----树形DP

    z最近在学习树形DP...好难啊. 在cf上找到了一题c题当模版马克一下. 题目不贴了..>>http://codeforces.com/problemset/problem/855/C& ...