目录:
题目:
分析:
如果是单点修改的话相信大家都会,所以这题的方法也是要转化为单点修改。根据求
gcd
g
c
d
的辗转相减法,
gcd(x,y)=gcd(x,y−x)
g
c
d
(
x
,
y
)
=
g
c
d
(
x
,
y
−
x
)
,可以推广到多个数,所以可以求原序列差分后的
gcd
g
c
d
,这样就可以做到单点修改。
然后还要用一个树状数组维护原序列。
最后有个很玄学的地方,小编的代码如果用printf输出,在本地会错,但cout就可以AC
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<queue>
#include<vector>
#include<map>
#include<list>
#include<ctime>
#include<iomanip>
#include<string>
#include<bitset>
#define LL long long
using namespace std;
inline LL read() {
LL d=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
return d*f;
}
LL a[500001],b[500001],s[500001];
int n,q;
void add(int x,LL w)
{
for(;x<=n;x+=x&-x) s[x]+=w;
return;
}
LL ask_sz(int x)
{
LL ans=0;
for(;x;x-=x&-x) ans+=s[x];
return ans;
}
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
struct segment_tree{
int l,r;
LL g;
}t[20000001];
void build(int p,int l,int r)
{
t[p].l=l;t[p].r=r;
if(l==r) {t[p].g=b[l];return;}
int mid=(l+r)>>1;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
t[p].g=gcd(t[p*2].g,t[p*2+1].g);
return;
}
void change(int p,int k,LL w)
{
if(t[p].l==t[p].r) {t[p].g+=w;return;}
int mid=(t[p].l+t[p].r)>>1;
if(k<=mid) change(p*2,k,w);
else change(p*2+1,k,w);
t[p].g=gcd(t[p*2].g,t[p*2+1].g);
return;
}
LL ask(int p,int l,int r)
{
if(l>r) return 1;
if(t[p].l==l&&t[p].r==r) return t[p].g;
int mid=(t[p].r+t[p].l)>>1;
if(r<=mid) return ask(p*2,l,r);
else if(l>mid) return ask(p*2+1,l,r);
else return gcd(ask(p*2,l,mid),ask(p*2+1,mid+1,r));
}
int main()
{
n=read();q=read();
a[1]=read();add(1,a[1]);
for(int i=2;i<=n;i++) a[i]=read(),b[i-1]=a[i]-a[i-1],add(i,b[i-1]);
build(1,1,n-1);
while(q--)
{
char g;
cin>>g;
int l=read(),r=read();
if(g=='C')
{
LL w=read();
add(l,w);
if(l>1) change(1,l-1,w);
if(r<n) {add(r+1,-w);change(1,r,-w);}
}
else
{
LL x=ask_sz(l),y=ask(1,l,r-1);
cout<<abs(gcd(x,y))<<endl;
//printf("%lld\n",abs(gcd(ask_sz(l),ask(1,l,r-1))));
}
}
return 0;
}