結果
問題 | No.1036 Make One With GCD 2 |
ユーザー |
![]() |
提出日時 | 2020-04-24 22:26:32 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,266 ms / 2,000 ms |
コード長 | 1,941 bytes |
コンパイル時間 | 1,037 ms |
コンパイル使用メモリ | 81,916 KB |
実行使用メモリ | 15,360 KB |
最終ジャッジ日時 | 2024-09-16 13:21:51 |
合計ジャッジ時間 | 24,011 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge6 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
#include <iostream>#include <vector>#include <algorithm>#include <functional>using namespace std; typedef long long ll; const int INF=1e9;typedef pair<int,int> P;ll gcd(ll a, ll b) {if (b==0) return a; else return gcd(b, a%b);}template <typename T>struct seg_pL {// 一点更新、区間取得 1~Nusing F = function<T(T,T)>;// f: [](int x, int y){return min(x,y);}// g: [](int x, int y){return y;} // 新しい者が常に正しい!int n0, n1; const F f, g; const T first_t; vector<T> seg;// f: 子の計算 g: 親と子(f)の計算seg_pL(int n, const F _f, const F _g, const T& _t) : f(_f), g(_g), first_t(_t) {n0 = n, n1 = 1; while(n0 > n1) n1<<=1; seg.assign(2*n1, first_t);}void set(int k, const T& _t) {seg[k+n1]=_t;} // 1~Nのk番目に値を入れるvoid bulid() { for(int k=n1-1; k>=0; k--) seg[k] = f(seg[2*k], seg[2*k+1]);}void update(int k, const T& t) {k += n1; seg[k] = g(seg[k], t); k /= 2;while(k > 0) {seg[k] = g(seg[k], f(seg[2*k], seg[2*k+1])); k /= 2;}//cout << seg << endl;}T query(int a, int b, int k, int l, int r) { // (0, 4+1)if (r<=a || l>=b) return first_t;if (a<=l && r<=b) return seg[k];else {T q1 = query(a, b, 2*k, l, (l+r)/2);T q2 = query(a, b, 2*k+1, (l+r)/2, r);//cout << q1 << " " << q2 << endl;return f(q1, q2);}}T query(int a, int b) {return query(a, b, 1, 0, n1);}};int main() {int n; cin>>n;ll a[n]; for(int i=0;i<n;i++) cin>>a[i];seg_pL<ll> seg(n, [](ll x, ll y){return gcd(x,y);}, [](ll x,ll y){return y;}, 0);for(int i=0;i<n;i++) seg.set(i, a[i]);seg.bulid();ll an=0, ii=0, jj=0;while(jj<n) {if (ii == jj) {if (seg.query(ii, jj+1) == 1) an++;jj++;}else if (seg.query(ii, jj+1) == 1) {an += n - jj; ii++;} else {jj++;}//cout<<ii<<" "<<jj<<" "<<an<<endl;}cout<<an<<endl;}