Codeforces Round #736 (Div. 2) D ST表维护区间gcd,二分

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=2e5+10,mod=1e9+7;

#define PII pair<int,int>
// #define x first
// #define y second
#define PB push_back
bool vis[N];
ll ST[N][100];
ll b[N],a[N];
int n;
void ST_init()
{
    for(int i=1;i<=n;++i)
        ST[i][0]=a[i];
    for(int j=1;j<32;++j)
    {
        for(int i=1;i<=n;++i)
        {
            if(i+(1<<j)-1>n) break;
            if((!ST[i+(1<<(j-1))][j-1])){
                if(!ST[i][j-1]){
                    ST[i][j]=0;
                }
                else{
                    ST[i][j]=1;
                }
                continue;
            }
            ST[i][j]=__gcd(ST[i][j-1],ST[i+(1<<(j-1))][j-1]);
        }
    }
}
 
ll ST_query(int s,int e)
{
    
    int k=(int)((log(e-s+1.0)/log(2.0)));
    return __gcd(ST[s][k],ST[e-(1<<k)+1][k]);
}

void solve()
{
	cin>>n;
    
    for(int i=1;i<=n;i++) cin>>b[i];
    for(int i=1;i<n;i++){
        a[i]=abs(b[i+1]-b[i]);
    }
    if(n==1){
        cout<<1<<endl;return;
    }
    if(n==2){
        if(b[1]==1||b[2]==1){
            cout<<1<<endl;
        }
        else cout<<2<<endl;
        return;
    }
    ST_init();
    // if(n==200000){
    //     cout<<ST_query(1,n-1)<<endl;
    // }
    int ans=1;
    for(int i=1;i<n;i++){
       // cout<<ST_query(i,n-1)<<endl;
        int l=i,r=n-1;
        int mid;
        while(l<=r){
            mid=(l+r)>>1;
            //cout<<ST_query(i,mid)<<endl;
            if(ST_query(i,mid)>1||ST_query(i,mid)==0){
                l=mid+1;
            }
            else{
                r=mid-1;
            }
        }
        if(ST_query(i,r)>1||(ST_query(i,mid)==0))
        ans=max(r-i+2,ans);
    }
    cout<<ans<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("a.txt", "r", stdin);
	freopen("aout.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	while (t--)
		solve();
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂的码泰君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值