結果
| 問題 | 
                            No.1036 Make One With GCD 2
                             | 
                    
| コンテスト | |
| ユーザー | 
                             veqcc
                         | 
                    
| 提出日時 | 2020-04-24 22:08:45 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1,112 ms / 2,000 ms | 
| コード長 | 1,666 bytes | 
| コンパイル時間 | 1,081 ms | 
| コンパイル使用メモリ | 109,388 KB | 
| 実行使用メモリ | 15,488 KB | 
| 最終ジャッジ日時 | 2024-09-16 13:16:54 | 
| 合計ジャッジ時間 | 19,035 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 41 | 
ソースコード
#include <functional>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;
template <typename T>
class SegmentTree {
    using F = function<T(T, T)>;
    int n;
    F f;
    T t;
    vector <T> dat;
public:
    SegmentTree(F f, T t) : f(f), t(t) {}
    void init(int n_) {
        n = 1;
        while (n < n_) n <<= 1;
        dat.assign(n << 1, t);
    }
    void build(const vector <T> &v) {
        auto n_ = (int)v.size();
        init(n_);
        for (int i = 0; i < n_; i++) dat[n + i] = v[i];
        for (int i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
    }
    void set_val(int k, T x) {
        dat[k += n] = x;
        while (k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
    }
    T query(int a, int b) {
        T vl = t, vr = t;
        for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) vl = f(vl, dat[l++]);
            if (r & 1) vr = f(dat[--r], vr);
        }
        return f(vl, vr);
    }
};
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); }
int main() {
    int n;
    cin >> n;
    vector <ll> a(n + 1, 1);
    for (int i = 0; i < n; i++) cin >> a[i];
    SegmentTree<ll> seg(gcd, 0);
    seg.build(a);
    ll ans = 0;
    int r = 1;
    for (int l = 0; l < n; l++) {
        while (r <= n && seg.query(l, r) != 1) r++;
        ans += n - r + 1;
    }
    cout << ans << "\n";
    return 0;
}
            
            
            
        
            
veqcc