結果
問題 | No.1036 Make One With GCD 2 |
ユーザー | kcvlex |
提出日時 | 2020-04-24 21:53:17 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 5,545 bytes |
コンパイル時間 | 1,457 ms |
コンパイル使用メモリ | 151,468 KB |
実行使用メモリ | 28,880 KB |
最終ジャッジ日時 | 2024-11-07 02:13:58 |
合計ジャッジ時間 | 9,499 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | TLE | - |
testcase_01 | -- | - |
testcase_02 | -- | - |
testcase_03 | -- | - |
testcase_04 | -- | - |
testcase_05 | -- | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
ソースコード
#include <limits> #include <initializer_list> #include <utility> #include <bitset> #include <tuple> #include <type_traits> #include <functional> #include <string> #include <array> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <iterator> #include <algorithm> #include <complex> #include <random> #include <numeric> #include <iostream> #include <iomanip> #include <sstream> #include <regex> #include <cassert> #include <cstddef> #include <variant> #define endl codeforces #define ALL(v) std::begin(v), std::end(v) #define ALLR(v) std::rbegin(v), std::rend(v) using ll = std::int64_t; using ull = std::uint64_t; using pii = std::pair<int, int>; using tii = std::tuple<int, int, int>; using pll = std::pair<ll, ll>; using tll = std::tuple<ll, ll, ll>; template <typename T> using vec = std::vector<T>; template <typename T> using vvec = vec<vec<T>>; template <typename T> const T& var_min(const T &t) { return t; } template <typename T> const T& var_max(const T &t) { return t; } template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); } template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); } template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); } template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); } template <typename T> T make_v(T init) { return init; } template <typename T, typename... Tail> auto make_v(T init, std::size_t s, Tail... tail) { auto v = std::move(make_v(init, tail...)); return vec<decltype(v)>(s, v); } template <typename T, std::size_t Head, std::size_t... Tail> struct multi_dem_array { using type = std::array<typename multi_dem_array<T, Tail...>::type, Head>; }; template <typename T, std::size_t Head> struct multi_dem_array<T, Head> { using type = std::array<T, Head>; }; template <typename T, std::size_t... Args> using mdarray = typename multi_dem_array<T, Args...>::type; namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; } ssize_t ceil_pow2(ssize_t s) { ssize_t ret = 1; while (ret < s) ret *= 2; return ret; } namespace segtree { template <typename T, typename Op> struct SegmentTree { vec<T> nodes; Op op; T id_ele; auto get_children_idx(ssize_t idx) const -> std::pair<ssize_t, ssize_t> { return std::make_pair(2 * idx + 1, 2 * idx + 2); } T get_merged(ssize_t idx) const { ssize_t a, b; std::tie(a, b) = get_children_idx(idx); return op(nodes[a], nodes[b]); } public: SegmentTree(const vec<T> &init_v, Op op, T id_ele) : op(op), id_ele(id_ele) { build(init_v); } void build(const vec<T> &v) { nodes.resize(ceil_pow2(v.size()) * 2 - 1, id_ele); ssize_t sz = size(); std::copy(ALL(v), nodes.begin() + sz - 1); build_parents(); } void build_parents() { ssize_t sz = size(); for (ssize_t i = sz - 2; 0 <= i; i--) nodes[i] = get_merged(i); } ssize_t size() const { return (nodes.size() + 1) / 2; } const T& operator [](ll idx) const { return nodes[idx + size() - 1]; } T get_query(ll ql, ll qr) const { T ret = id_ele; ssize_t lnode = ql + size(), rnode = qr + size(); while (lnode < rnode) { if (lnode & 1) { ret = std::move(op(nodes[lnode - 1], ret)); lnode++; } if (rnode & 1) { rnode--; ret = std::move(op(ret, nodes[rnode - 1])); } lnode /= 2; rnode /= 2; } return ret; } void update_query(ll idx, T val) { idx += size() - 1; nodes[idx] = val; while (idx) { ll pidx = (idx - 1) / 2; nodes[pidx] = get_merged(pidx); idx = pidx; } } template <typename F> ssize_t lower_bound(F f) const { T sum = id_ele; ssize_t l = 0, r = size(), idx = 0, bidx = 0; if (!f(nodes[0])) return -1; while (true) { if (r - l == 1) { if (!f(sum)) bidx++; break; } auto lidx = 2 * idx + 1; auto ridx = lidx + 1; T nsum = op(sum, nodes[lidx]); ssize_t m = (l + r) / 2; if (f(nsum)) { r = m; idx = lidx; } else { l = m; idx = ridx; bidx = m; sum = std::move(nsum); } } return bidx; } }; } int main() { ll n; std::cin >> n; vec<ll> av(n); for (ll &e : av) std::cin >> e; auto op = [](ll a, ll b) { return (a && b ? std::gcd(a, b) : std::max(a, b)); }; segtree::SegmentTree<ll, decltype(op)> seg(av, op, 0); ll ans = 0; for (ll i = 0; i < n; i++) { if (1 < seg.get_query(i, n)) break; ll ok = n, ng = i; while (1 < std::abs(ok - ng)) { ll mid = (ok + ng) / 2; (seg.get_query(i, mid) == 1 ? ok : ng) = mid; } ans += n - ng; } std::cout << ans << '\n'; return 0; }