# CF920F SUM and REPLACE

2020-09-16 15:31:21  阅读：11  来源： 互联网

1. 将 $$i\in[l,r]$$ 中的所有 $$a_i$$ 替换为 $$d(a_i)$$。$$d(x)$$ 表示 $$x$$ 的正约数的个数。
2. 求 $$\displaystyle\sum_{i=l}^r a_i$$。

$$d$$是可以预处理出来的。

for(int i = 1; i <= 1000000; i ++){
for(int j = i; j <= 1000000; j += i){
d[j] ++;
}
}


#include <bits/stdc++.h>
#define ls (now << 1)
#define rs (now<<1|1)
#define mid ((l+r)>>1)
#define int long long
using namespace std;

template<typename temp>
x = 0;temp f = 1;char ch;
while(!isdigit(ch = getchar())) (ch == '-') and (f = -1);
for(x = ch^48; isdigit(ch = getchar()); x = (x<<1)+(x<<3)+(ch^48));
return x *= f;
}
template <typename temp, typename ...Args>
int x = 0, f = 1;char ch;
while(!isdigit(ch = getchar())) (ch == '-') and (f = -1);
for(x = ch^48; isdigit(ch = getchar()); x = (x<<1)+(x<<3)+(ch^48));
return x *= f;
}

const int maxn = 3e5+10;

int n, m, a[maxn], d[1000010];

struct seg_tree{
struct nodes{
int l, r, sum, max_num;
}node[maxn<<2];
void up(int now){return (void)(node[now].sum = node[ls].sum + node[rs].sum, node[now].max_num = max(node[ls].max_num, node[rs].max_num));}
void build(int l, int r, int now){
node[now].l = l, node[now].r = r;
if(l == r) return (void)(node[now].sum = a[l], node[now].max_num = a[l]);
build(l, mid, ls), build(mid+1, r, rs);
return up(now);
}
void chenge(int l, int r, int now){
if(r < node[now].l or node[now].r < l or node[now].max_num <= 2) return;
if(node[now].l == node[now].r) return(void) (node[now].max_num = node[now].sum = d[node[now].sum]);
chenge(l, r, ls), chenge(l, r, rs);
return up(now);
}
void quary(int l, int r, int now, int &ans){
if(r < node[now].l or node[now].r < l) return;
if(l <= node[now].l and node[now].r <= r) return (void)(ans += node[now].sum);
quary(l, r, ls, ans), quary(l, r, rs, ans);
return up(now);
}
}tree;

signed main(){
for(int i = 1; i <= 1000000; i ++){
for(int j = i; j <= 1000000; j += i){
d[j] ++;
}
}
for(int i = 1; i <= n; i ++) read(a[i]);
tree.build(1, n, 1);
for(int i = 1, x, y, z; i <= m; i ++){
if(x&1) tree.chenge(y, z, 1);
else{
int ans = 0;
tree.quary(y, z, 1, ans);
printf("%lld\n", ans);
}
}
return 0;
}