//由于当时不知道位压缩导致TLE看了beat anyone if necessary的博客才知道怎么做 //线段树的风格与not only success风格很相似 #include<iostream> #include<cstdio> #define lson l,mid,lrt #define rson mid+1,r,rrt #define mid ((l+r)>>1) #define lrt rt<<1 #define rrt rt<<1|1 const int N=100005; int lcm[N<<2],gcd[N<<2]; int prime[]={2, 3, 5, 7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; int dpos[]={28,25,23,21,20,19,18,17,16,15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; int a[]={1,2,4,8,16,32,64}; int b[]={1,3,9,27,81}; int c[]={1,5,25}; int d[]={1,7,49}; inline int _max(int x,int y){return x>y?x:y;} inline int max(int x,int y){ return _max(x&0x70000000,y&0x70000000)|_max(x&0x0e000000,y&0x0e000000)|_max(x&0x01800000,y&0x01800000) |_max(x&0x00600000,y&0x00600000)|( (x|y)&0x001fffff ); } int turn(int x){ int cnt,y=0; for(int i=0;prime[i]<=x&&i!=25;++i){ for(cnt=0;x%prime[i]==0;x/=prime[i],++cnt); y|=cnt<<dpos[i]; } return y; } int back(int x,int p){ __int64 y=1; int k=x>>dpos[0];y=y*a[k]%p;x^=k<<dpos[0]; k=x>>dpos[1];y=y*b[k]%p;x^=k<<dpos[1]; k=x>>dpos[2];y=y*c[k]%p;x^=k<<dpos[2]; k=x>>dpos[3];y=y*d[k]%p;x^=k<<dpos[3]; for(int i=4;i!=25;++i){ if(x&(1<<dpos[i])) y=y*prime[i]%p; } return y; } int GCD(int a,int b){ return b?GCD(b,a%b):a; } void PushUp(int rt){ lcm[rt]=max(lcm[lrt],lcm[rrt]); gcd[rt]=GCD(gcd[lrt],gcd[rrt]); } void build(int l,int r,int rt){ if(l==r){ scanf("%d",&gcd[rt]); lcm[rt]=turn(gcd[rt]); return; } build(lson); build(rson); PushUp(rt); } void update(int s,int t,int l,int r,int rt){ if(l==r){ gcd[rt]=t; lcm[rt]=turn(t); return; } if(s<=mid) update(s,t,lson); else update(s,t,rson); PushUp(rt); } int query_lcm(int L,int R,int l,int r,int rt){ if(L<=l&&R>=r) return lcm[rt]; if(L<=mid&&R>mid) return max(query_lcm(L,R,lson),query_lcm(L,R,rson)); if(L<=mid) return query_lcm(L,R,lson); if(R>mid) return query_lcm(L,R,rson); } int query_gcd(int L,int R,int l,int r,int rt){ if(L<=l&&R>=r) return gcd[rt]; if(L<=mid&&R>mid) return GCD(query_gcd(L,R,lson),query_gcd(L,R,rson)); if(L<=mid) return query_gcd(L,R,lson); if(R>mid) return query_gcd(L,R,rson); } int main(){ int n,q; while(scanf("%d%d",&n,&q)!=EOF){ build(1,n,1); char s[2]; while(q--){ scanf("%s",s); int x,y,p; if(s[0]=='C'){ scanf("%d%d",&x,&y); update(x,y,1,n,1); }else{ scanf("%d%d%d",&x,&y,&p); if(s[0]=='L') printf("%d\n",back(query_lcm(x,y,1,n,1),p)); else printf("%d\n",query_gcd(x,y,1,n,1)%p); } } } return 0; }
hdu3071
最新推荐文章于 2021-09-14 21:47:18 发布