题解:P9607 [CERC2019] Be Geeks!

· · 题解

P9607 [CERC2019] Be Geeks!

题目传送门

思路

考虑直接对原序列分治,每次将当前分治区域处理完后再处理左右两边,分治树总大小为 n \log n。

设当前分治区间左右端点为 l 和 r,中点为 mid,思考如何处理跨过分治中点的答案。

把左区间(mid 到 l)的后缀的 \gcd 和右区间(mid+1 到 r)前缀的 \gcd 处理出来,因为 \gcd 每一次改变都至少减半,所以两个区间处理出的 \gcd 分别最多有 \log V 个。(V 为值域上界)

对于两个区间中的每一种 \gcd,我们都开一个 vector,然后插入该区间中前缀或后缀的 \gcd 等于对应值位置的区间最大值。然后在另一半区间的每一个位置,枚举所有的 \gcd,去对应的 vector 里面二分有多少个最大值小于它的数位置,最后把 ans 加上两个位置的 \gcd 乘上最大值再乘上二分出的个数即可。

Code

#include<bits/stdc++.h>
namespace fast_IO{
    #define IOSIZE 100000
        char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
    #define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
    #define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
    #define isdigit(ch) (ch>47&&ch<58)
    #define isspace(ch) (ch<33)
        template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
        template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
        template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
        inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
        inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
        inline void print(char x) { putchar(x); }
        inline void print(char *x) { while (*x) putchar(*x++); }
        inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
        inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
        inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
        inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
        inline void print(bool b) { putchar(b+48); }
        template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
        template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
        struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
        template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
        template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
    #define cout io
    #define cin io
}using namespace fast_IO;
using namespace std;
const int mod=1e9+7,N=2e5+10;
int n,g[N],a[N],ans,len,p[N];
vector<int>v[N];
#define pb push_back
inline int check(int x,int val){
    return lower_bound(v[x].begin(),v[x].end(),val)-v[x].begin();
}
inline void solve(int l,int r){
    if(l==r){
      ans=(ans+1ll*a[l]*a[l]%mod)%mod;
      return;
    }
    int mid=(l+r)>>1;
    g[mid]=a[mid];
    for(int i=mid-1;i>=l;--i)g[i]=__gcd(g[i+1],a[i]);
    g[mid+1]=a[mid+1];
    for(int i=mid+2;i<=r;++i)g[i]=__gcd(g[i-1],a[i]);
    int ma=0;
    len=0;
    for(int i=mid;i>=l;--i){
      ma=max(ma,a[i]);
      if(g[i]!=p[len]){
        p[++len]=g[i];
        v[len].clear();
      }
      v[len].pb(ma);
    }
    ma=0;
    for(int i=mid+1;i<=r;++i){
      ma=max(ma,a[i]);
      for(int j=1;j<=len;++j)
        ans=(ans+1ll*check(j,ma+1)*__gcd(g[i],p[j])%mod*ma%mod)%mod;
    }
    ma=0,len=0;
    for(int i=mid+1;i<=r;++i){
      ma=max(ma,a[i]);
      if(g[i]!=p[len]){
        p[++len]=g[i];
        v[len].clear();
      }
      v[len].pb(ma);
    }
    ma=0;
    for(int i=mid;i>=l;--i){
      ma=max(ma,a[i]);
      for(int j=1;j<=len;++j)
        ans=(ans+1ll*check(j,ma)*__gcd(g[i],p[j])%mod*ma%mod)%mod;
    }
    solve(l,mid),solve(mid+1,r);
}
signed main(){
    cin>>n;
    for(int i=1;i<=n;++i)cin>>a[i];
    solve(1,n);
    cout<<ans<<"\n";
    return 0;
}

提交记录