結果
問題 | No.1036 Make One With GCD 2 |
ユーザー |
![]() |
提出日時 | 2020-04-24 21:58:47 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,527 ms / 2,000 ms |
コード長 | 2,947 bytes |
コンパイル時間 | 2,973 ms |
コンパイル使用メモリ | 198,048 KB |
最終ジャッジ日時 | 2025-01-09 23:43:27 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T, class U> using Pa = pair<T, U>; template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; template<typename Monoid,typename F> class SegmentTree{ private: int sz; vector<Monoid> seg; const F op; const Monoid e; public: SegmentTree(int n,const F op,const Monoid &e):op(op),e(e){ sz = 1; while(sz<=n) sz <<= 1; seg.assign(2*sz,e); } void set(int k, const Monoid &x){ seg[k+sz] = x; } void build(){ for(int i=sz-1;i>0;i--){ seg[i] = op(seg[2*i],seg[2*i+1]); } } void update(int k,const Monoid &x){ k += sz; seg[k] = x; while(k>>=1){ seg[k] = op(seg[2*k],seg[2*k+1]); } } Monoid query(int l,int r){ Monoid L = e,R = e; for(l+=sz,r+=sz;l<r;l>>=1,r>>=1){ if(l&1) L = op(L,seg[l++]); if(r&1) R = op(seg[--r],R); } return op(L,R); } Monoid operator[](const int &k)const{ return seg[k+sz]; } template<typename C> int find_subtree(int a,const C &check,Monoid &M,bool type) { while(a<sz) { Monoid nxt = type ? op(seg[2*a+type],M) : op(M,seg[2*a+type]); if(check(nxt)) a = 2*a+type; else M = nxt, a = 2*a+1-type; } return a - sz; } template<typename C> int find_first(int a,const C &check) { Monoid L = e; if(a <= 0) { if(check(op(L,seg[1]))) return find_subtree(1,check,L,false); return -1; } int b = sz; for(a+=sz,b+=sz;a<b;a>>=1,b>>=1) { if(a&1) { Monoid nxt = op(L,seg[a]); if(check(nxt)) return find_subtree(a,check,L,false); L = nxt; ++a; } } return -1; } template<typename C> int find_last(int b,const C &check) { Monoid R = e; if(b >= sz) { if(check(op(seg[1], R))) return find_subtree(1, check, R, true); return -1; } int a = sz; for(b+=sz; a<b; a>>=1,b>>=1) { if(b&1) { Monoid nxt = op(seg[--b],R); if(check(nxt)) return find_subtree(b,check,R,true); R = nxt; } } return -1; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vec<ll> A(N); for(int i=0;i<N;i++) cin >> A[i]; auto op = [&](ll a,ll b){return gcd(a,b);}; SegmentTree<ll,decltype(op)> seg(N,op,0); for(int i=0;i<N;i++) seg.set(i,A[i]); seg.build(); ll ans = 0; for(int i=0;i<N;i++){ if(seg.query(i,N)>1) continue; int r = seg.find_first(i,[&](ll b){return gcd(A[i],b)==1;}); ans += N-r; } cout << ans << "\n"; }