Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)
F. SUM and REPLACE
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
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
outputCopy
30
13
4
22
https://codeforces.com/contest/920/problem/F
题意:
给你一个含有n个数的数组,和m个操作
操作1:将l~r中每一个数\(a[i]\)变成 \(d(a[i])\)
其中$ d(x)$ 是约数个数函数。
操作2: 求l~r的a[i] 的sum和。
思路:
$ d(x)$ 约数个数函数可以利用线性筛预处理处理。
又因为 \(d(2)=2\) 和 \(d(1)=1\) 操作1对a[i]等于1或者2没有影响。
那么我们可以对一个区间中全都是1或者2不更新操作。
同时 \(d(x)\) 是收敛函数, 在1e6 的范围内,最多不超过5次改变就会收敛到1或2.
所以更新操作可以暴力解决,
同时用线段树维护即可。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
// d(n)表示n的约数个数和
// prime[i]表示第i个质数
//num[i]表示i的最小质因子出现次数
int sshu[maxn];
int N = maxn;
int num[maxn];
int d[maxn];
bool no[maxn];
int tot;
void prepare()
{
d[1] = 1; num[1] = 1;
for (int i = 2; i < N; i++) {
if (!no[i]) {
sshu[++tot] = i;
d[i] = 2; num[i] = 1;
}
for (int j = 1; j <= tot && sshu[j]*i < N; j++) {
int v = sshu[j] * i;
no[v] = 1;
if (i % sshu[j] == 0) {
num[v] = num[i] + 1;
d[v] = d[i] / num[v] * (num[v] + 1);
break;
}
d[v] = d[i] << 1; num[v] = 1;
}
}
//for (int i=1;i<=10;i++) printf("%d\n",d[i]);
}
int a[maxn];
struct node {
int l, r;
int laze;
bool isall;
ll num;
} segment_tree[maxn << 2];
void pushup(int rt)
{
segment_tree[rt].num = segment_tree[rt << 1].num + segment_tree[rt << 1 | 1].num;
segment_tree[rt].isall = segment_tree[rt << 1].isall & segment_tree[rt << 1 | 1].isall;
}
void build(int rt, int l, int r)
{
segment_tree[rt].l = l;
segment_tree[rt].r = r;
if (l == r) {
segment_tree[rt].num =a[l];
if (segment_tree[rt].num == 1 || segment_tree[rt].num == 2) {
segment_tree[rt].isall = 1;
}
return ;
}
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
pushup(rt);
}
void update(int rt, int l, int r)
{
if (l <= segment_tree[rt].l && r >= segment_tree[rt].r && segment_tree[rt].isall) {
return;
}
if (segment_tree[rt].l == segment_tree[rt].r) {
segment_tree[rt].num = d[segment_tree[rt].num];
if (segment_tree[rt].num == 1 || segment_tree[rt].num == 2) {
segment_tree[rt].isall = 1;
}
return ;
} else {
int mid = (segment_tree[rt].l + segment_tree[rt].r) >> 1;
if (mid >= l) {
update(rt << 1, l, r);
}
if (mid < r) {
update(rt << 1 | 1, l, r);
}
pushup(rt);
}
}
ll query(int rt, int l, int r)
{
if (segment_tree[rt].l >= l && segment_tree[rt].r <= r) {
ll res = 0ll;
res += segment_tree[rt].num;
return res;
}
int mid = (segment_tree[rt].l + segment_tree[rt].r) >> 1;
ll res = 0ll;
if (mid >= l) {
res += query(rt << 1, l, r);
}
if (mid < r) {
res += query(rt << 1 | 1, l, r);
}
return res;
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
prepare();
int n, m;
du2(n, m);
repd(i, 1, n) {
du1(a[i]);
}
build(1, 1, n);
repd(i, 1, m) {
int op; int l, r;
du3(op, l, r);
if (op == 1) {
update(1, l, r);
} else {
printf("%lld\n", query(1, l, r));
}
}
return 0;
}
inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)的更多相关文章
- 【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 23 F. MEX Queries 离散化+线段树
F. MEX Queries time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 【Educational Codeforces Round 37 F】SUM and REPLACE
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 那个D函数它的下降速度是很快的. 也就是说到最后他会很快的变成2或者1 而D(2)==2,D(1)=1 也就是说,几次操作过后很多数 ...
- Educational Codeforces Round 64 (Rated for Div. 2) (线段树二分)
题目:http://codeforces.com/contest/1156/problem/E 题意:给你1-n n个数,然后求有多少个区间[l,r] 满足 a[l]+a[r]=max([l, ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
- Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements (思维,前缀和)
Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements time limit per test 1 se ...
- Educational Codeforces Round 40 F. Runner's Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
- Educational Codeforces Round 37 A B C D E F
A. water the garden Code #include <bits/stdc++.h> #define maxn 210 using namespace std; typede ...
- 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]Educational Codeforces Round 37 (Rated for Div. 2)
Water The Garden #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h ...
随机推荐
- RestHighLevelClient 之 Scroll
ES中默认最大查询结果为10000,大于10000时查不出结果,报错超过最大值,如把 from调到大于10000. 针对这个问题,有两种解决办法. 第一种,修改 max_result_window 很 ...
- Matlab JPEG详细介绍
作为一个基本的图像压缩方式,JPEG 已经得到了广泛的运用,但 JPEG 相关的基本原理,却经常被忽视,或解释得很不确切.这里我们详细讨论一下 JPEG 的编码原理,并结合实例来给出一个更加感性的认识 ...
- spring中@Conditional注解
@Conditional是Spring4新提供的注解,它的作用是根据某个条件加载特定的bean. 我们需要创建实现类来实现Condition接口,这是Condition的源码 public inter ...
- 前端手势控制图片插件书写三(将transform变化应用在图片和canvas画布上)
注意:transform的scale为负数时,图片会垂直翻转 一.在使用transform将计算得到的变化应用到图片上后,需要考虑到我们每次计算的都是touchmove中本次的差量.在第一次移动过后. ...
- Linux 工作管理 (job control)
fg , bg 有时,命令需要很长的时间才能执行完成.对于这种情况,我们使用‘bg’命令可以将任务放在后台执行,而用‘fg’可以调到前台来使用. 我们可以通过‘&’在后台启动一个程序: fin ...
- 洛谷 题解 P4158 【[SCOI2009]粉刷匠】
状态: dp[i][j][k][0/1]: 到达第i行时, 到达第j列时, 刷到第k次时, 这一格有没有刷对 转移 换一块木板时肯定要多刷一次 dp[i][j][k][0]=max(dp[i-1][m ...
- 《MIT 6.828 Lab 1 Exercise 7》实验报告
本实验链接:mit 6.828 lab1 Exercise 7. 题目 Exercise 7. Use QEMU and GDB to trace into the JOS kernel and st ...
- 葡萄城首席架构师:前端开发与Web表格控件技术解读
讲师:Issam Elbaytam,葡萄城集团全球首席架构师(Chief Software Architect of GrapeCity Global).曾任 Data Dynamics.Inc 创始 ...
- DDL数据库对象管理
DDL数据库对象管理 约束的分类: 主键约束:primary key 要求主键列数据唯一,并且不允许为空. 外键约束:foreign key 用于在两表之间建立关系,需要指定引用主表的哪一列. 检查约 ...
- Ural 1250 Sea Burial 题解
目录 Ural 1250 Sea Burial 题解 题意 输入 题解 程序 Ural 1250 Sea Burial 题解 题意 给定一个\(n\times m\)的地图,\(.\)为水,\(\#\ ...