Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)
Posted qieqiemin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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), '