F - SUM and REPLACE(线段树)

文章描述了一个编程问题,涉及对数组执行两种类型的查询:替换(将区间内所有元素替换为其正因子个数)和求和。线段树数据结构被用来高效地处理这些查询,同时维护区间内的最大值,因为每个元素在经过六次替换后会变成1或2。代码示例展示了如何构建和操作线段树来解决这个问题。
摘要由CSDN通过智能技术生成

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:

  1. REPLACE l r — for every 

     replace ai with D(ai);
  2. 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 tiliri 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.

Sample 1

InputcopyOutputcopy
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

 

 

 线段树维护,每个值经过六次变化后就会是1或者2,所以再维护一个最大值就可以了 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
using namespace std;
#define int long long
typedef pair<int,int>PII;
constexpr int N=1e6+7;
int n,m,a[N];

struct node{
    int l,r;
    int sum;
    int maxx;
}tr[N*4];
int d[N];
void pushup(int u){
    tr[u].sum=tr[u<<1].sum+tr[u<<1|1].sum;
    tr[u].maxx=max(tr[u<<1|1].maxx,tr[u<<1].maxx);
}
void build(int u,int l,int r){
    tr[u].l=l,tr[u].r=r;
    if(tr[u].l==tr[u].r){
        tr[u]={l,r,a[l],a[l]};
        return;
    }
    int mid=(tr[u].l+tr[u].r)>>1;
    build(u<<1,l,mid);
    build(u<<1|1,mid+1,r);
    pushup(u);
}
void modify(int u,int l,int r){
    if(tr[u].maxx<=2) return;
    if(tr[u].l==tr[u].r){
        tr[u].sum=tr[u].maxx= d[tr[u].sum];
        return;
    }
    int mid=(tr[u].l+tr[u].r)>>1;
    if(l<=mid) modify(u<<1 , l , r);
    if(r>mid) modify(u<<1|1 , l , r );
    pushup(u);

}
int query(int u,int l,int r) {
    if (l <= tr[u].l && r >= tr[u].r) return tr[u].sum;
    int res = 0;
    int mid = tr[u].l + tr[u].r >> 1;
    if (l <= mid) res += query(u << 1, l, r);
    if (r > mid) res += query(u << 1 | 1, l, r);
    return res;
}
signed main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    for(int i=1;i<=1000000;i++)
    {
        for(int j=i;j<=1000000;j+=i) {
            d[j]++;
        }
    }
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    build(1,1,n);
    while(m--){
        int op,l,r;
        cin>>op>>l>>r;
        if(op==1){
            modify(1,l,r);
        }
        else{
            cout<<query(1,l,r)<<endl;
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

q619718

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值