結果
問題 | No.1036 Make One With GCD 2 |
ユーザー | ningenMe |
提出日時 | 2020-04-26 02:58:38 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 4,980 bytes |
コンパイル時間 | 1,988 ms |
コンパイル使用メモリ | 175,900 KB |
実行使用メモリ | 41,984 KB |
最終ジャッジ日時 | 2024-11-08 20:03:40 |
合計ジャッジ時間 | 10,101 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | -- | - |
コンパイルメッセージ
main.cpp:142:9: warning: inline variables are only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 142 | inline static constexpr TypeNode unit_node = 0; | ^~~~~~
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; #define SPEED cin.tie(0);ios::sync_with_stdio(false); template<class Operator> class SegmentTree { using TypeNode = typename Operator::TypeNode; size_t length; size_t num; vector<TypeNode> node; vector<pair<size_t,size_t>> range; public: //unitで初期化 SegmentTree(const size_t num): num(num) { for (length = 1; length < num; length *= 2); node.resize(2 * length, Operator::unit_node); range.resize(2 * length); for (int i = 0; i < length; ++i) range[i+length] = make_pair(i,i+1); for (int i = length - 1; i >= 0; --i) range[i] = make_pair(range[(i<<1)+0].first,range[(i<<1)+1].second); } //vectorで初期化 SegmentTree(const vector<TypeNode> & vec) : num(vec.size()) { for (length = 1; length < vec.size(); length *= 2); node.resize(2 * length, Operator::unit_node); for (int i = 0; i < vec.size(); ++i) node[i + length] = vec[i]; for (int i = length - 1; i >= 0; --i) node[i] = Operator::funcNode(node[(i<<1)+0],node[(i<<1)+1]); range.resize(2 * length); for (int i = 0; i < length; ++i) range[i+length] = make_pair(i,i+1); for (int i = length - 1; i >= 0; --i) range[i] = make_pair(range[(i<<1)+0].first,range[(i<<1)+1].second); } //同じinitで初期化 SegmentTree(const size_t num, const TypeNode init) : num(num) { for (length = 1; length < num; length *= 2); node.resize(2 * length, Operator::unit_node); range.resize(2 * length); for (int i = 0; i < length; ++i) node[i+length] = init; for (int i = 0; i < length; ++i) range[i+length] = make_pair(i,i+1); for (int i = length - 1; i >= 0; --i) range[i] = make_pair(range[(i<<1)+0].first,range[(i<<1)+1].second); } //[idx,idx+1) void update(size_t idx, const TypeNode var) { if(idx < 0 || length <= idx) return; idx += length; node[idx] = Operator::funcMerge(node[idx],var); while(idx >>= 1) node[idx] = Operator::funcNode(node[(idx<<1)+0],node[(idx<<1)+1]); } //[l,r) TypeNode get(int l, int r) { if (l < 0 || length <= l || r < 0 || length < r) return Operator::unit_node; TypeNode vl = Operator::unit_node, vr = Operator::unit_node; for(l += length, r += length; l < r; l >>=1, r >>=1) { if(l&1) vl = Operator::funcNode(vl,node[l++]); if(r&1) vr = Operator::funcNode(node[--r],vr); } return Operator::funcNode(vl,vr); } //return [0,length] int PrefixBinarySearch(TypeNode var) { if(!Operator::funcCheck(node[1],var)) return num; TypeNode ret = Operator::unit_node; size_t idx = 2; for(; idx < 2*length; idx<<=1){ if(!Operator::funcCheck(Operator::funcNode(ret,node[idx]),var)) { ret = Operator::funcNode(ret,node[idx]); idx++; } } return min((idx>>1) - length,num); } //range[l,r) return [l,r] int BinarySearch(size_t l, size_t r, TypeNode var) { if (l < 0 || length <= l || r < 0 || length < r) return -1; TypeNode ret = Operator::unit_node; size_t off = l; for(size_t idx = l+length; idx < 2*length && off < r; ){ if(range[idx].second<=r && !Operator::funcCheck(Operator::funcNode(ret,node[idx]),var)) { ret = Operator::funcNode(ret,node[idx]); off = range[idx++].second; if(!(idx&1)) idx >>= 1; } else{ idx <<=1; } } return off; } }; class Gcd{ public: inline static long long impl(long long n, long long m) { static constexpr long long K = 5; long long t,s; for(int i = 0; t = n - m, s = n - m * K, i < 80; ++i) { if(t<m){ if(!t) return m; n = m, m = t; } else{ if(!m) return t; n=t; if(t >= m * K) n = s; } } return impl(m, n % m); } inline static long long pre(long long n, long long m) { long long t; for(int i = 0; t = n - m, i < 4; ++i) { (t < m ? n=m,m=t : n=t); if(!m) return n; } return impl(n, m); } inline static long long gcd(long long n, long long m) { return (n>m ? pre(n,m) : pre(m,n)); } inline static constexpr long long pureGcd(long long a, long long b) { return (b ? pureGcd(b, a % b):a); } inline static constexpr long long lcm(long long a, long long b) { return (a*b ? (a / gcd(a, b)*b): 0); } inline static constexpr long long extGcd(long long a, long long b, long long &x, long long &y) { if (b == 0) return x = 1, y = 0, a; long long d = extGcd(b, a%b, y, x); return y -= a / b * x, d; } }; template<class T> struct nodeGcdPointUpdate { using TypeNode = T; inline static constexpr TypeNode unit_node = 0; static constexpr TypeNode funcNode(TypeNode l,TypeNode r){return r?funcNode(r,l%r):l;} static constexpr TypeNode funcMerge(TypeNode l,TypeNode r){return r;} static constexpr bool funcCheck(TypeNode nodeVal,TypeNode var){return var == nodeVal;} }; // solution by binary search in prefix range on segment tree int main() { SPEED ll N; cin >> N; vector<ll> A(N); for(int i = 0; i < N; ++i) cin >> A[i]; SegmentTree<nodeGcdPointUpdate<ll>> seg(A); ll ans = 0; for(int i = 0; i < N; ++i) { ans += N - seg.BinarySearch(i,N,1); } cout << ans << endl; }