1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| #include<bits/stdc++.h> using namespace std;
typedef long long ll; namespace math{ const int maxn=1e7+7; bool no_pri[maxn]={0,1,0}; int pri[664579+100],low[maxn],phi[maxn]; void f_ini(){ for(int i=2;i<maxn;i++){ if(!no_pri[i]) low[i]=pri[++pri[0]]=i; for(int j=1;1ll*pri[j]*i<maxn;j++){ no_pri[pri[j]*i]=1; if(i%pri[j]==0) { low[pri[j]*i]=low[i]*pri[j]; break; } else low[pri[j]*i]=pri[j]; } }
phi[1]=1; for(int i=1;i<=pri[0];i++){ for(ll mul=pri[i],ct=1;mul<maxn;mul*=pri[i],ct++){ phi[mul]=mul/pri[i]*(pri[i]-1); } }
for(int i=2;i<maxn;i++){ for(int j=1;1ll*pri[j]*i<maxn;j++){ int x=low[i*pri[j]], y=i*pri[j]/x; phi[x*y]=phi[x]*phi[y]; if(i%pri[j]==0) break; } } } }
#define ml ((l+r)>>1) #define mr (ml+1) const int maxn=3e5+5; int a[maxn]; int ls[maxn*2],rs[maxn*2],tot; int cov[maxn*2]; ll sum[maxn*2];int mi[maxn*2],mx[maxn*2];
inline void modify(int&u,int l,int r,int cov_){ if(cov_!=-1){ cov[u]=cov_; sum[u]=1ll*cov_*(r-l+1); mi[u]=mx[u]=cov_; } }
inline void push_down(int u,int l,int r){ modify(ls[u],l,ml,cov[u]); modify(rs[u],mr,r,cov[u]); cov[u]=-1; }
inline void pushup(int u,int l,int r){ mi[u]=min(mi[ls[u]],mi[rs[u]]); mx[u]=max(mx[ls[u]],mx[rs[u]]); sum[u]=sum[ls[u]]+sum[rs[u]]; }
void updatecov(int u,int l,int r,int ql,int qr,int d){ if(ql<=l&&r<=qr){ modify(u,l,r,d); return; } push_down(u,l,r); if(ml>=ql) updatecov(ls[u],l,ml,ql,qr,d); if(mr<=qr) updatecov(rs[u],mr,r,ql,qr,d); pushup(u,l,r); }
void updatephi(int u,int l,int r,int ql,int qr){ if(ql<=l&&r<=qr&&mi[u]==mx[u]){ modify(u,l,r,math::phi[mi[u]]); return; } push_down(u,l,r); if(ml>=ql) updatephi(ls[u],l,ml,ql,qr); if(mr<=qr) updatephi(rs[u],mr,r,ql,qr); pushup(u,l,r); }
ll query(int u,int l,int r,int ql,int qr){ if(ql<=l&&r<=qr) return sum[u]; push_down(u,l,r); ll ret=0; if(ml>=ql) ret+=query(ls[u],l,ml,ql,qr); if(mr<=qr) ret+=query(rs[u],mr,r,ql,qr); return ret; }
void build(int&u,int l,int r){ u=++tot; cov[u]=-1; if(l==r) sum[u]=mi[u]=mx[u]=a[l]; else{ build(ls[u],l,ml); build(rs[u],mr,r); pushup(u,l,r); } }
int read(){int x;scanf("%d",&x);return x;}
int main(){ math::f_ini(); int t=read(); while(t--){ int n=read(),m=read(); for(int i=1;i<=n;i++) a[i]=read(); tot=0; int rt; build(rt,1,n); for(int i=1;i<=m;i++){ int op=read(),l=read(),r=read(); switch(op){ case 1:updatephi(rt,1,n,l,r);break; case 2:updatecov(rt,1,n,l,r,read());break; default:printf("%lld\n",query(rt,1,n,l,r)); } } } }
|