解:线段树的应用,一个关键点是需要将序列的区间加转化为单点加,因此可以用线段树来维护序列的差分数组信息,由此把区间加变为单点加;另一个关键点是,求区间最大的gcd,整个序列的gcd=差分数组的gcd,对于区间[l,r],我们需要query(1,1,l)这是根据差分数组的性质来求序列中第l个元素,,而query(1,l+1,r)是对这个差分数组的区间求gcd,然后再求二者的gcd即是询问的答案。
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i = a;i<n;i++)
#define per(i,a,n) for(int i = n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define yes cout<<"YES"<<'\n';
#define no cout<<"NO"<<'\n';
#define endl '\n';
typedef vector<int> VI;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll MOD=1000000007;
int rnd(int x) {return mrand() % x;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;};
ll lcm(int a,int b){return a*b/gcd(a,b);};
const int N=500010;
int n,m;
struct node{
int l,r;
ll sum,d;
}tr[N*4];
ll a[N];
void pushup(node &u,node &l,node &r){
u.sum=l.sum+r.sum;
u.d=gcd(l.d,r.d);
}
void pushup(int u){
pushup(tr[u],tr[u<<1],tr[u<<1|1]);
}
void build(int u,int l,int r){
if(l==r){
ll v=a[l]-a[l-1];
tr[u]={l,r,v,v};
return;
}
tr[u].l=l,tr[u].r=r;
int m=(l+r)>>1;
build(u<<1,l,m);
build(u<<1|1,m+1,r);
pushup(u);
}
void modify(int u,int x,ll v){
if(tr[u].l==x&&tr[u].r==x){
ll t=tr[u].sum+v;
tr[u]={x,x,t,t};
return;
}
int m=(tr[u].l+tr[u].r)>>1;
if(x<=m) modify(u<<1,x,v);
else modify(u<<1|1,x,v);
pushup(u);
}
node query(int u,int l,int r){
if(l>r) return {0};
if(tr[u].l>=l&&tr[u].r<=r) return tr[u];
int m=(tr[u].l+tr[u].r)>>1;
if(r<=m) return query(u<<1,l,r);
else if(l>m) return query(u<<1|1,l,r);
else{
auto left=query(u<<1,l,r);
auto right=query(u<<1|1,l,r);
node res;
pushup(res,left,right);
return res;
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m;
rep(i,1,n+1) cin>>a[i];
build(1,1,n);
rep(i,0,m){
int l,r;
ll d;
char op;
cin>>op>>l>>r;
if(op=='Q'){
auto left=query(1,1,l);
node right={0,0,0,0};
if(l+1<=r) right=query(1,l+1,r);
cout<<abs(gcd(left.sum,right.d))<<endl;
}else{
cin>>d;
modify(1,l,d);
if(r+1<=n) modify(1,r+1,-d);
}
}
return 0;
}