浅析一下题意,设 表示 幂次最高的质因数的出现次数,要求的就是 。
考虑怎么维护,注意到每次变化的 ,说明只含有质因子 ,幂次次数的维护是加法,很容易用线段树来维护。
开四棵就好,剩下的是区间加、区间 的模板。
#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;
}