結果

問題 No.1036 Make One With GCD 2
ユーザー TAISA_TAISA_
提出日時 2020-04-24 22:01:07
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,286 ms / 2,000 ms
コード長 3,430 bytes
コンパイル時間 2,689 ms
コンパイル使用メモリ 205,384 KB
実行使用メモリ 11,400 KB
最終ジャッジ日時 2023-10-14 19:18:08
合計ジャッジ時間 21,185 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 710 ms
11,296 KB
testcase_01 AC 306 ms
11,260 KB
testcase_02 AC 302 ms
11,220 KB
testcase_03 AC 70 ms
7,112 KB
testcase_04 AC 124 ms
11,240 KB
testcase_05 AC 2 ms
4,356 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 105 ms
7,136 KB
testcase_08 AC 88 ms
7,108 KB
testcase_09 AC 422 ms
11,236 KB
testcase_10 AC 393 ms
11,224 KB
testcase_11 AC 428 ms
11,220 KB
testcase_12 AC 377 ms
11,220 KB
testcase_13 AC 542 ms
11,400 KB
testcase_14 AC 549 ms
11,216 KB
testcase_15 AC 515 ms
11,236 KB
testcase_16 AC 519 ms
11,216 KB
testcase_17 AC 533 ms
11,280 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 3 ms
4,352 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 496 ms
11,264 KB
testcase_23 AC 361 ms
11,220 KB
testcase_24 AC 529 ms
11,200 KB
testcase_25 AC 474 ms
11,196 KB
testcase_26 AC 486 ms
11,216 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 1 ms
4,352 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 1 ms
4,356 KB
testcase_38 AC 504 ms
11,188 KB
testcase_39 AC 1,286 ms
11,276 KB
testcase_40 AC 385 ms
11,196 KB
testcase_41 AC 943 ms
11,204 KB
testcase_42 AC 926 ms
11,220 KB
testcase_43 AC 902 ms
11,200 KB
testcase_44 AC 959 ms
11,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll MOD = 998244353LL;
constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
template <class T>
void chmin(T &a, T b) { a = min(a, b); }
template <class T>
void chmax(T &a, T b) { a = max(a, b); }
void debug() { cerr << "ok" << endl; }
template <class T>
void printv(const vector<T> &v) {
    for (int i = 0; i < v.size(); i++) cout << v[i] << (i + 1 == v.size() ? '\n' : ' ');
}
template <class T>
void readv(vector<T> &v) {
    for (int i = 0; i < v.size(); i++) cin >> v[i];
}
//Point Update Range Get
template <class T>
struct Segtree {
    int n;
    vector<T> dat;
    Segtree(int n_) {
        n = 1;
        while (n < n_) {
            n <<= 1;
        }
        dat.resize(2 * n, T::e);
    }
    Segtree(int n_, const vector<T> &a) {
        n = 1;
        while (n < n_) {
            n <<= 1;
        }
        dat.resize(2 * n, T::e);
        for (int i = 0; i < a.size(); i++) {
            dat[i + n] = a[i];
        }
        for (int i = n - 1; i > 0; i--) {
            dat[i] = T::f(dat[i << 1], dat[i << 1 | 1]);
        }
    }
    void upd(int k, const T &x) {
        k += n;
        T::g(dat[k], x);
        k >>= 1;
        while (k > 0) {
            dat[k] = T::f(dat[k << 1], dat[k << 1 | 1]);
            k >>= 1;
        }
    }
    T get(const int &a, const int &b, int k, int l, int r) {
        if (b <= l || r <= a) {
            return T::e;
        }
        if (a <= l && r <= b) {
            return dat[k];
        }
        return T::f(get(a, b, k << 1, l, (l + r) >> 1),
                    get(a, b, k << 1 | 1, (l + r) >> 1, r));
    }
    inline T get(const int &a, const int &b) { //[a,b)
        if (a >= b) {
            return T::e;
        }
        return get(a, b, 1, 0, n);
    }
    int find(const int &a, const int &b, const T &x, int k, int l, int r) {
        if (b <= l || r <= a || dat[k] > x) {
            return -1;
        }
        if (k >= n) {
            return k - n;
        }
        int il = find(a, b, x, k << 1, l, (l + r) >> 1);
        if (il != -1) {
            return il;
        }
        return find(a, b, x, k << 1 | 1, (l + r) >> 1, r);
    }
    inline int find(const int &a, const int &b, const T &x) { //[a,b)における、値<=x なる最左のindexを求める
        if (a >= b) {
            return -1;
        }
        return find(a, b, x, 1, 0, n);
    }
};
struct T {
    ll a;
    inline static T f(const T &x, const T &y) {
        if (x.a == -1) return y;
        if (y.a == -1) return x;
        return T{__gcd(x.a, y.a)};
    }
    inline static void g(T &a, const T &b) {
        a = b;
    }
    static T e;
};
T T::e = T{-1};
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    Segtree<T> seg(n);
    for (int i = 0; i < n; i++) {
        ll a;
        cin >> a;
        seg.upd(i, T{a});
    }
    int r = 0;
    ll res = 0;
    for (int l = 0; l < n; l++) {
        chmax(r, l);
        while (r < n && seg.get(l, r + 1).a > 1) {
            r++;
        }
        res += n - r;
        //   cout << l << " " << r << " " << seg.get(l, r + 1).a << endl;
    }
    cout << res << '\n';
}
0