Codeforces 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的更多相关文章
- Codeforces 920F - SUM and REPLACE 【线段树】
<题目链接> 题目大意: 给你一个序列,有两个操作,一个是求区间 l - r 的和,另一个是对区间l-r的元素修改值,x=d(x),d(x)为x的因子个数. 解题分析: 因为可能有多次修改 ...
- 2018.12.15 codeforces 920F. SUM and REPLACE(线段树)
传送门 线段树入门题. 给你一个序列:支持区间修改成自己的约数个数,区间求和. 实际上跟区间开方一个道理. 2的约数个数为2,1的约数个数为1,因此只要区间的最大值小于3就不用修改否则就暴力修改. 因 ...
- 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 是 ...
- Codefroces 920F SUM and REPLACE(线段树)
SUM and REPLACE 题意:给你n个数,进行m次操作,分别是将区间[l,r]内的所有数替换成自己的因子数 和 对区间[l,r]进行求和. 题解:可以发现2的因子个数还是2,1的因子个数还是1 ...
- Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)
F. SUM and REPLACE time limit per test2 seconds memory limit per test256 megabytes inputstandard inp ...
- Codeforces 85D Sum of Medians(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...
- Educational Codeforces Round 37-F.SUM and REPLACE题解
一.题目 二.题目链接 http://codeforces.com/contest/920/problem/F 三.题意 给定$N$个范围在$[1, 1e6)$的数字和$M$个操作.操作有两种类型: ...
- 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) = ...
随机推荐
- 上传代码到github的步骤
在你的电脑上装好git 大致流程是: 1.在github上创建项目 2.使用git clone https://github.com/xxxxxxx/xxxxx.git克隆到本地 3.编辑项目 4.g ...
- 20145212 罗天晨 Web安全基础实践
一.实验后回答问题 (1)SQL注入攻击原理,如何防御 原理:SQL注入攻击是攻击者在web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,把SQL语句当做用户名等输入正常网页中以获取数据 ...
- Linux下查看网卡驱动和版本信息
Linux下查看网卡驱动和版本信息 查看网卡生产厂商和信号 查看基本信息:lspci 查看详细信息:lspci -vvv # 3个小写的v 查看网卡信息:lspci | grep Ethernet 查 ...
- STM32之独立看门狗(IWDG)与窗口看门狗(WWDG)总结
一.独立看门狗 STM32 的独立看门狗由内部专门的 40Khz 低速时钟驱动,即使主时钟发生故障,它也仍然有效. 看门狗的原理:单片机系统在外界的干扰下会出现程序跑飞的现象导致出现死循环,看门狗电路 ...
- 【Python025-字典】
一.字典 1.创建和访问字典(字典是大括号表示,字典是映射类型) 语法类型:键:key,值:value,用冒号隔开 --- >>> dict1 = {'李宁':'一切皆有可能','耐 ...
- ODAC(V9.5.15) 学习笔记(四)TOraQuery (1)
TOraQuery是ODAC中常用的一个组件,其继承关系如下: TDataSet ---TMemDataSet ---TCustomDADataSet ---TOraDataSet ---TCusto ...
- P4782 【模板】2-SAT 问题
https://www.luogu.org/problemnew/show/P4782 链接 https://www.luogu.org/problemnew/show/P4782 思路 选a就必须选 ...
- SP6779 GSS7
GSS7解题报告 前言 唔,有点恶心哪,废了两个多小时debug 思路 很容易看出傻子都知道,这个是树链剖分+线段树的裸题,只不过是恶心了点,这里重点讲一下细节问题 线段树 做过GSS系列的都应该很熟 ...
- NodeJs 在window中安装使用
Nodejs: 官网下载长期版本zip格式解压 D:\Program Files\nodejs 查看版本 D:\Git\SpringBootDemo (master) $ node -v v8.11. ...
- c# 进阶之 WebAPI
REST是设计风格而不是标准. webapi有自己的路由. webservice和wcf的协议都是soap协议,数据的序列化和反序列化都是soap的格式.而webapi是Json的数据传递 webap ...