Educational Codeforces Round 37 F - SUM and REPLACE (线段树)

弱的一笔,想到这是个线段树 ,不会打表,不敢敲。知道最后变成两个就不能在减少了,这个剪枝当时没想出来,每个数都变不了几次,复杂度没仔细考虑。

#include<bits/stdc++.h>
using namespace std;
const int MAXV=1e6+5;
const int MAXN=300005;
typedef long long ll;
int d[MAXV]={0};
int a[MAXN],mx[MAXN<<2];
ll sum[MAXN<<2];
void build(int l,int r,int root)
{
    if(l==r)
    {
        mx[root]=sum[root]=a[l];
        return;
    }
    int m=(l+r)/2;
    build(l,m,root<<1);
    build(m+1,r,root*2+1);
    sum[root]=sum[root<<1]+sum[root*2+1];
    mx[root]=max(mx[root<<1],mx[root*2+1]);
}
void update(int l,int r,int L,int R,int root)
{
    if(mx[root]<=2)return;
    if(L==R)
    {
        mx[root]=sum[root]=d[mx[root]];
        return;
    }
    int m=(L+R)/2;
    if(m>=l)
        update(l,r,L,m,root<<1);
    if(m<r)update(l,r,m+1,R,root<<1|1);
     sum[root]=sum[root<<1]+sum[root*2+1];
    mx[root]=max(mx[root<<1],mx[root*2+1]);
}
ll query(int l,int r,int L,int R,int root)
{
    if(l<=L&&r>=R)return sum[root];
    int m=(L+R)/2;
    ll ans=0;
     if(m>=l)
        ans+=query(l,r,L,m,root<<1);
      if(m<r) ans+=query(l,r,m+1,R,root<<1|1);
      return ans;
}
int main()
{
    int n,m;
    cin>>n>>m;
   for(int i=1;i<MAXV;i++)
    for(int j=i;j<MAXV;j+=i)
    d[j]++;
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
        build(1,n,1);
        int op,l,r;
       while(m--)
       {
           scanf("%d%d%d",&op,&l,&r);
           if(op==1)update(l,r,1,n,1);
            else
            printf("%I64d\n",query(l,r,1,n,1));
       }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值