题解:P17638 [ICPC 2019 Yinchuan R] Pot!!

· · 题解

浅析一下题意,设 f(x) 表示 x 幂次最高的质因数的出现次数,要求的就是 \max_{l\le x\le r} f(x)。

考虑怎么维护,注意到每次变化的 x\le 10,说明只含有质因子 2,3,5,7,幂次次数的维护是加法,很容易用线段树来维护。

开四棵就好,剩下的是区间加、区间 \max 的模板。

#include<bits/stdc++.h>
using namespace std;
#define int long long
struct node{
    int tree[1000005];
    int lazy[1000005];
    inline void update(int p){
        tree[p]=max(tree[p<<1],tree[p<<1|1]);
        return;
    }
    inline void Lazy(int p){
        if (lazy[p]){
            tree[p<<1]+=lazy[p];
            tree[p<<1|1]+=lazy[p];
            lazy[p<<1]+=lazy[p];
            lazy[p<<1|1]+=lazy[p];
            lazy[p]=0;
        }
        return;
    }
    inline void change(int p,int l,int r,int L,int R,int val){
        if (L<=l && r<=R){
            tree[p]+=val;
            lazy[p]+=val;
            return;
        }
        Lazy(p);
        int mid=(l+r)/2;
        if (L<=mid) change(p<<1,l,mid,L,R,val);
        if (mid<R)  change(p<<1|1,mid+1,r,L,R,val);
        update(p);
        return;
    }
    inline int query(int p,int l,int r,int L,int R){
        if (L<=l && r<=R){
            return tree[p];
        }
        int mid=(l+r)/2;

        Lazy(p);
        int res=0;
        if (L<=mid){
            res=max(res,query(p<<1,l,mid,L,R));
        } 
        if (mid<R){
            res=max(res,query(p<<1|1,mid+1,r,L,R));
        }
        return res;
    }
}T[4];

int n,m;

signed main(){
    ios::sync_with_stdio(0);
    cin>>n>>m;
    while (m--){
        string op;
        int l,r,x;
        cin>>op;
        if (op=="MULTIPLY"){
            cin>>l>>r>>x;
            int cnt=0;
            while (x%2==0){
                x/=2;
                cnt++;
            }
            T[0].change(1,1,n,l,r,cnt);

            cnt=0;
            while (x%3==0){
                x/=3;
                cnt++;
            }
            T[1].change(1,1,n,l,r,cnt);

            cnt=0;
            while (x%5==0){
                x/=5;
                cnt++;
            }
            T[2].change(1,1,n,l,r,cnt);

            cnt=0;
            while (x%7==0){
                x/=7;
                cnt++;
            }
            T[3].change(1,1,n,l,r,cnt);
        }
        else{
            cin>>l>>r;
            int ans=0;
            for (int i=0;i<4;i++){
                ans=max(ans,T[i].query(1,1,n,l,r));
            }
            cout<<"ANSWER "<<ans<<"\n";
        }
    }



    return 0;
}