https://codeforces.com/contest/2050/problem/F
#include<bits/stdc++.h>
#define lc p<<1
#define rc p<<1|1
#define INF 2e9
using namespace std;
#define lowbit(x) x&(-x)
#define endl '\n'
using ll = long long;
using pii = pair<ll,ll>;
const double PI = acos(-1);
const int N=2e5+10;
int a[N];
int _gcd(int x,int y){
while(y){
y^=x^=y^=x%=y;
}
return x;
}
struct node{
int l,r;
int gcc;
}st[N*4];
void pushup(int p){
st[p].gcc=_gcd(st[lc].gcc,st[rc].gcc);
}
void build(int p,int x,int y){
if(x==y){
st[p]={x,y,0};
return;
}
st[p]={x,y};
int mid=(x+y)>>1;
build(lc,x,mid);
build(rc,mid+1,y);
pushup(p);
}
int query(int p,int x,int y){
if(st[p].l>=x&&st[p].r<=y) return st[p].gcc;
int mid=(st[p].l+st[p].r)>>1;
int ans=0;
if(x<=mid) ans=_gcd(ans,query(lc,x,y));
if(y>mid) ans=_gcd(ans,query(rc,x,y));
return ans;
}
void modify(int p,int x,int k){
if(st[p].l==x&&st[p].r==x){
st[p].gcc=k;
return;
}
else{
int mid=(st[p].l+st[p].r)>>1;
if(x<=mid) modify(lc,x,k);
else modify(rc,x,k);
pushup(p);
}
}
void solve(){
int n,q;cin>>n>>q;
for(int i=1;i<=n;i++)
cin>>a[i];
build(1,1,n);
for(int i=2;i<=n;i++){
modify(1,i,abs(a[i]-a[i-1]));
}
// for(int i=1;i<=n;i++){
// cout<<st[i].gcc;
// }
while(q--){
int l, r;cin>>l>>r;
if(l==r) cout<<0<<" ";
else cout<<query(1,l+1,r)<<" ";
}
cout<<endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
cin>>T;
while (T--) {
solve();
}
return 0;
}
标签:gcc,return,GCD,int,线段,mid,st,ans,区间
From: https://www.cnblogs.com/laileou/p/18672913