結果

問題 No.1036 Make One With GCD 2
ユーザー sykwersykwer
提出日時 2020-04-24 22:48:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,456 ms / 2,000 ms
コード長 3,583 bytes
コンパイル時間 1,198 ms
コンパイル使用メモリ 120,976 KB
実行使用メモリ 15,364 KB
最終ジャッジ日時 2023-10-14 19:32:42
合計ジャッジ時間 22,063 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 805 ms
15,300 KB
testcase_01 AC 377 ms
15,144 KB
testcase_02 AC 352 ms
15,056 KB
testcase_03 AC 115 ms
8,584 KB
testcase_04 AC 211 ms
13,620 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 151 ms
8,904 KB
testcase_08 AC 122 ms
8,640 KB
testcase_09 AC 494 ms
15,148 KB
testcase_10 AC 464 ms
14,780 KB
testcase_11 AC 508 ms
15,144 KB
testcase_12 AC 466 ms
14,740 KB
testcase_13 AC 634 ms
14,876 KB
testcase_14 AC 639 ms
14,980 KB
testcase_15 AC 601 ms
14,600 KB
testcase_16 AC 601 ms
14,788 KB
testcase_17 AC 619 ms
14,796 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 4 ms
4,352 KB
testcase_21 AC 3 ms
4,352 KB
testcase_22 AC 611 ms
14,768 KB
testcase_23 AC 443 ms
13,864 KB
testcase_24 AC 622 ms
15,032 KB
testcase_25 AC 574 ms
14,612 KB
testcase_26 AC 598 ms
14,616 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 1 ms
4,356 KB
testcase_36 AC 1 ms
4,348 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 600 ms
15,192 KB
testcase_39 AC 1,456 ms
15,364 KB
testcase_40 AC 438 ms
13,880 KB
testcase_41 AC 1,109 ms
15,120 KB
testcase_42 AC 1,099 ms
15,232 KB
testcase_43 AC 1,053 ms
15,236 KB
testcase_44 AC 1,111 ms
15,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>

#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>

#include <cmath>
#include <numeric>
#include <random>
#include <cstring>
#include <cassert>

// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#include <stack>

// type
#include <cstdint>
#include <functional>

/* ---------- Namespace ---------- */
using namespace std;

/* ---------- Type ---------- */
using ll = long long;
#define int ll
template <class T> using MaxHeap = priority_queue<T>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
#define P pair<ll, ll>

/* ---------- Constants  */
const double PI = 3.141592653589793238462643383279;
const ll MOD = 1e9 + 7;
const int INF = 1LL << 55;

/* ---------- Functions */
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }


/* v-v-v-v-v-v-v-v-v Main Part v-v-v-v-v-v-v-v-v */
int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a%b);
}

template <class T> class SegTree {
    int N;
    vector<T> data;
    T ide; // Identity element (Initial value)
    function<T(T, T)> operation; // Function for range query
    function<T(T, T)> updater; // Function for point update

    T _query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return ide;
        if (a <= l && r <= b) {
            return data[k];
        } else {
            T left = _query(a, b, 2 * k + 1, l, (l + r) / 2);
            T right = _query(a, b, 2 * k + 2, (l + r) / 2, r);
            return operation(left, right);
        }
    }

public:
    // int n:                       Size of array
    // T ide:                       Identity element
    // function<T(T, T)> operation: Function to merge elements
    // function<T(T, T)> updater:    (old data[i], some value x) -> new data[i]
    SegTree(size_t n, T ide, function<T(T, T)> operation,
            function<T(T, T)> updater) : ide(ide), operation(operation), updater(updater) {
        N = 1;
        while (N < n) N *= 2;
        data.resize(2 * N - 1, ide);
    }

    void update(int i, T x) {
        i += N - 1;
        data[i] = updater(data[i], x);
        while (i > 0) {
            i = (i - 1) / 2;
            data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
        }
    }

    T query(int a, int b) {
        return _query(a, b, 0, 0, N);
    }

    T operator[](int i) {
        return data[i + N - 1];
    }
};

signed main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int N;
    cin >> N;
    vector<int> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];

    ll ide = INF;
    auto operation = [](int left, int right) -> int {
        if (left==INF) return right;
        if (right == INF) return left;
        return gcd(left, right);
    };
    auto updater = [](int old, int x) -> int {return x;};
    auto seg = new SegTree<int>(N, ide, operation, updater);
    for (int i = 0; i < N; i++) seg->update(i, A[i]);

    int ret= 0;
    // [left, right]
    int right = 0;
    for (int left = 0; left < N; left++) {
        while (right < N && seg->query(left, right+1) != 1) right++;
        if (right == N) break;
        ret += N-right;
    }

    cout << ret << endl;
    return 0;
}
0