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;
}
代码二:
//#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],c[N];
//int n,q;
//
////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,c[x]};
// return;
// }
// st[p]={x,y};
// int mid=(x+y)>>1;
// build(lc,x,mid);
// build(rc,mid+1,y);
// pushup(p);
//}
//ll query(int p,int x,int y){
// if(st[p].l>=x&&st[p].r<=y) return st[p].gcc;
//// int ans1=0,ans2=0;
// ll ans=0;
// int mid=(st[p].l+st[p].r)>>1;
//// if(x<=mid) ans1=query(lc,x,y);
//// if(y>=mid) ans2=query(rc,x,y);
//// return _gcd(ans1,ans2);
// if(x<=mid) ans=__gcd(ans,query(lc,x,y));
// if(y>mid) ans=__gcd(ans,query(rc,x,y));
// return ans;
//
//}
//void solve(){
// cin>>n>>q;
// for(int i=1;i<=n;i++){
// cin>>a[i];
// }
// for(int i=1;i<n;i++)
// c[i]=abs(a[i+1]-a[i]);
// if(n==1) { //n等于1特判,build(1,1,n-1)建不了树
// while(q--){
// int l,r;cin>>l>>r;
// cout<<0<<" ";
// }
// cout<<endl;
// return;
// }
// build(1,1,n-1);
// while(q--){
// int l,r;cin>>l>>r;
// if(l==r) cout<<0<<" ";
// else cout<<query(1,l,r-1)<<" ";
// }
// 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;
//}
//
//
//
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库