結果

問題 No.1036 Make One With GCD 2
ユーザー KoDKoD
提出日時 2020-04-24 21:57:35
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,566 bytes
コンパイル時間 878 ms
コンパイル使用メモリ 80,028 KB
実行使用メモリ 15,496 KB
最終ジャッジ日時 2023-08-06 23:08:19
合計ジャッジ時間 48,821 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,962 ms
15,312 KB
testcase_01 AC 1,501 ms
15,208 KB
testcase_02 AC 168 ms
15,268 KB
testcase_03 AC 42 ms
8,532 KB
testcase_04 AC 75 ms
13,676 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 386 ms
8,740 KB
testcase_08 AC 310 ms
8,524 KB
testcase_09 AC 1,526 ms
15,248 KB
testcase_10 AC 1,418 ms
14,688 KB
testcase_11 AC 1,570 ms
15,240 KB
testcase_12 AC 1,432 ms
14,836 KB
testcase_13 AC 1,874 ms
15,000 KB
testcase_14 AC 1,894 ms
14,984 KB
testcase_15 AC 1,760 ms
14,920 KB
testcase_16 AC 1,771 ms
14,780 KB
testcase_17 AC 1,844 ms
15,144 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 6 ms
4,380 KB
testcase_22 AC 1,738 ms
14,672 KB
testcase_23 AC 1,236 ms
13,616 KB
testcase_24 AC 1,818 ms
14,980 KB
testcase_25 AC 1,639 ms
14,404 KB
testcase_26 AC 1,715 ms
14,604 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1,787 ms
15,100 KB
testcase_39 TLE -
testcase_40 AC 1,238 ms
14,060 KB
testcase_41 TLE -
testcase_42 TLE -
testcase_43 TLE -
testcase_44 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_): l(l_), r(std::max(l_, r_)) { }
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int l_, int r_): l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

template <class T>
class segment_tree {
public:
  using value_type = typename T::value_type;
  using effector_type = typename T::effector_type;
  using value_operation = typename T::value_operation;
  using merge_operation = typename T::merge_operation;

private:
  int size;
  const value_operation op1;
  const merge_operation op2;
  std::vector<value_type> node;

  void update() {
    for (int k = size - 1; k > 0; --k) {
      node[k] = op1(node[k << 1 | 0], node[k << 1 | 1]);
    }
  }

public:
  segment_tree(): op1(value_operation()), op2(merge_operation()) { }
  segment_tree(int size_, const value_type &initial_ = value_operation().identity):
    op1(value_operation()), op2(merge_operation())
  { init(size_, initial_); }
  segment_tree(const std::vector<value_type> &node_):
    op1(value_operation()), op2(merge_operation())
  { build(node_); }

  void init(int size_, const value_type &initial_ = value_operation().identity) {
    size = 1;
    while (size < size_) {
      size <<= 1;
    }
    node.assign(size << 1, initial_);
    update();
  }
  void build(const std::vector<value_type> &node_) {
    init(node_.size());
    assign(node_.begin(), node_.end(), 0);
  }

  void modify(int i, const effector_type &x) {
    i += size;
    node[i] = op2(node[i], x);
    while (i > 1) {
      i >>= 1;
      node[i] = op1(node[i << 1 | 0], node[i << 1 | 1]);
    }
  }
  template <class U>
  void modify(U begin, U end, int i) {
    i += size;
    while (begin != end) {
      node[i] = op2(node[i], *begin);
      ++i;
      ++begin;
    }
    update();
  }

  void assign(int i, const value_type &x) {
    i += size;
    node[i] = x;
    while (i > 1) {
      i >>= 1;
      node[i] = op1(node[i << 1 | 0], node[i << 1 | 1]);
    } 
  }
  template <class U>
  void assign(U begin, U end, int i) {
    i += size;
    while (begin != end) {
      node[i] = *begin;
      ++i;
      ++begin;
    }
    update();
  }

  value_type operator [] (int i) const { 
    return node[i + size];
  }
  value_type fold(int l, int r) const {
    l += size;
    r += size;
    value_type resl = op1.identity;
    value_type resr = op1.identity;
    while (l < r) {
      if (l & 1) {
        resl = op1(resl, node[l++]);
      }
      if (r & 1) {
        resr = op1(node[--r], resr);
      }
      l >>= 1;
      r >>= 1;
    }
    return op1(resl, resr);
  }

};


struct monoid {
  using value_type = long long;
  using effector_type = long long;
  struct value_operation {
    value_type identity = 0;
    value_type operator () (const value_type &x, const value_type &y) const {
      return std::gcd(x, y);
    }
  };
  struct merge_operation {
    value_type operator () (const value_type &x, const effector_type &y) const {
      return y;
    }
  };
};


int main() {
  int N;
  scanf("%d", &N);
  std::vector<long long> A(N);
  for (long long &x: A) {
    scanf("%lld", &x);
  }
  segment_tree<monoid> seg(A);
  long long ans = 0;
  for (int i: range(0, N)) {
    if (seg.fold(0, i + 1) != 1) {
      continue;
    }
    if (A[i] == 1) {
      ans += i + 1;
      continue;
    }
    int ok = i, ng = 0;
    while (ok - ng > 1) {
      int md = (ok + ng) >> 1;
      (seg.fold(md, i + 1) == 1 ? ng : ok) = md;
    }
    ans += ok;
  }
  printf("%lld\n", ans);
  return 0;
}
0