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) = 2 (2 is divisible by 1 and 2), D(6) = 4 (6 is divisible by 1, 2, 3 and 6).
You are given an array a of n integers. You have to process two types of queries:
- REPLACE l r — for every
replace ai with D(ai);
- SUM l r — calculate
.
Print the answer for each SUM query.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 3·105) — the number of elements in the array and the number of queries to process, respectively.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the elements of the array.
Then m lines follow, each containing 3 integers ti, li, ri denoting i-th query. If ti = 1, then i-th query is REPLACE li ri, otherwise it's SUM li ri (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n).
There is at least one SUM query.
Output
For each SUM query print the answer to it.
Example
7 6
6 4 1 10 3 2 4
2 1 7
2 4 5
1 3 5
2 4 4
1 5 7
2 1 7
30
13
4
22
可以发现每个数被改若干次之后就会变成2(或者这个数本来就为1它也只能是1),而这个次数的最大值也不会很大,所以我们就可以维护一个还没有变成≤2的位置集合,然后暴力单点修改。又因为是求前缀和,所以我们再维护一个树状数组就好了,时间复杂度O(N*log²N)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#define ll long long
#define maxn 300005
#define maxm 1000000
using namespace std;
set<int> s;
set<int> ::iterator it;
int zs[maxn],t=0,low[maxm+5];
int d[maxm+5],n,m,le,ri,opt;
bool v[maxm+5];
int a[maxn];
ll f[maxn],qz[maxn]; inline void init(){
d[1]=1,low[1]=1;
for(int i=2;i<=maxm;i++){
if(!v[i]) zs[++t]=i,d[i]=2,low[i]=i;
for(int j=1,u;j<=t&&(u=zs[j]*i)<=maxm;j++){
v[u]=1;
if(!(i%zs[j])){
low[u]=low[i]*zs[j];
if(low[i]==i) d[u]=d[i]+1;
else d[u]=d[low[u]]*d[i/low[i]];
break;
}
low[u]=zs[j],d[u]=d[i]<<1;
}
} } inline void update(int x,int y){
for(;x<=n;x+=x&-x) f[x]+=(ll)y;
} inline ll query(ll x){
ll an=0;
for(;x;x-=x&-x) an+=f[x];
return an;
} inline void solve(){
int pre=*s.lower_bound(le);
while(pre<=ri){
update(pre,d[a[pre]]-a[pre]);
a[pre]=d[a[pre]];
if(a[pre]<=2) s.erase(pre);
pre=*s.upper_bound(pre);
}
} int main(){
init();
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",a+i);
qz[i]=qz[i-1]+(ll)a[i];
s.insert(i);
}
s.insert(n+1); while(m--){
scanf("%d%d%d",&opt,&le,&ri);
if(opt==2) printf("%lld\n",qz[ri]-qz[le-1]+query(ri)-query(le-1));
else solve();
} return 0;
}
Codeforces 920 F SUM and REPLACE的更多相关文章
- Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)
F. SUM and REPLACE time limit per test2 seconds memory limit per test256 megabytes inputstandard inp ...
- 【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) ...
- Educational Codeforces Round 37-F.SUM and REPLACE题解
一.题目 二.题目链接 http://codeforces.com/contest/920/problem/F 三.题意 给定$N$个范围在$[1, 1e6)$的数字和$M$个操作.操作有两种类型: ...
- codeforces 920 EFG 题解合集 ( Educational Codeforces Round 37 )
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 920F - SUM and REPLACE
920F - SUM and REPLACE 思路1: 线段树(982 ms) 每个点最多更新6次 代码: #include<bits/stdc++.h> using namespace ...
- Codeforces 396B On Sum of Fractions 数论
题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...
- codeforces 963A Alternating Sum
codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^k\) ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
随机推荐
- 【BZOJ 4556】[Tjoi2016&Heoi2016]字符串 SAM+二分+主席树
这道题市面上就两种法:一种是SA+二分+主席树,一种是SAM+二分+主席树(有不少人打线段树合并???)(除此之外还有一种利用炒鸡水的数据的暴力SA,贼快.....)(当时学SA的时候没做这道题,现在 ...
- MySQL使用笔记(四)数据的操作
By francis_hao Dec 14,2016 数据的操作包括插入数据记录.更新数据记录和删除数据记录. 插入数据记录 插入单条数据记录 field表示的字段名和value表示数据要一一对 ...
- [MySQL] explain执行计划解读
Explain语法 EXPLAIN SELECT …… 变体: 1. EXPLAIN EXTENDED SELECT …… 将执行计划“反编译”成SELECT语句,运行SHOW WARNINGS 可得 ...
- 于是他错误的点名开始了 [Trie]
于是他错误的点名开始了 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边搓炉石一边点名以至于有一天他连续点到了某个同学两次,然后正好被路过的校长发现了然后就是一顿欧拉欧拉欧拉(详情请见已 ...
- HAOI2006 均分数据 [模拟退火]
题目描述 已知N个正整数:A1.A2.--.An .今要将它们分成M组,使得各组数据的数值和最平均,即各组的均方差最小.均方差公式如下: 输入输出格式 输入格式: 输入文件data.in包括: 第一行 ...
- 第九届蓝桥杯C/C++B组题解附代码
1.标题:第几天 2000年的1月1日,是那一年的第1天.那么,2000年的5月4日,是那一年的第几天? 125天 打开日历就ok 2. 标题:明码 汉字的字形存在于字库中,即便在今天,16点阵的字库 ...
- 怎么把centos虚拟机zip文件导入vm虚拟机中
执行以上三步就可以将一个压缩的centoszip文件导入到虚拟机中
- php中的split函数
字符串分割函数:split函数 <?php $email='microsoft@exam!ple.com'; $domain = split('\.|@|!',$email);//split分割 ...
- django中管理程序1
为了解决启动关闭程序方便,在django中启动结束任务的问题. urls.py ################DJANGO start kill job####################### ...
- memcached启动脚本(class练习)
说明:使用类的方式编写程序启动脚本(memcached) import time import os from subprocess import Popen,PIPE class Process(o ...