結果

問題 No.1036 Make One With GCD 2
ユーザー ningenMeningenMe
提出日時 2020-09-12 08:24:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,569 ms / 2,000 ms
コード長 5,318 bytes
コンパイル時間 5,379 ms
コンパイル使用メモリ 206,432 KB
実行使用メモリ 23,548 KB
最終ジャッジ日時 2023-10-14 20:40:22
合計ジャッジ時間 20,830 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,569 ms
23,368 KB
testcase_01 AC 200 ms
23,360 KB
testcase_02 AC 190 ms
23,548 KB
testcase_03 AC 20 ms
12,748 KB
testcase_04 AC 31 ms
21,964 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 69 ms
12,996 KB
testcase_08 AC 57 ms
12,620 KB
testcase_09 AC 369 ms
23,124 KB
testcase_10 AC 347 ms
22,984 KB
testcase_11 AC 378 ms
23,480 KB
testcase_12 AC 349 ms
22,860 KB
testcase_13 AC 578 ms
23,176 KB
testcase_14 AC 584 ms
23,108 KB
testcase_15 AC 547 ms
22,960 KB
testcase_16 AC 553 ms
23,040 KB
testcase_17 AC 570 ms
23,172 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 540 ms
22,924 KB
testcase_23 AC 401 ms
21,968 KB
testcase_24 AC 561 ms
23,128 KB
testcase_25 AC 514 ms
22,768 KB
testcase_26 AC 531 ms
22,964 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 2 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 1 ms
4,348 KB
testcase_35 AC 2 ms
4,352 KB
testcase_36 AC 2 ms
4,352 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 188 ms
23,544 KB
testcase_39 AC 600 ms
23,396 KB
testcase_40 AC 401 ms
22,156 KB
testcase_41 AC 760 ms
23,368 KB
testcase_42 AC 733 ms
23,384 KB
testcase_43 AC 809 ms
23,488 KB
testcase_44 AC 841 ms
23,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define SPEED cin.tie(0);ios::sync_with_stdio(false);


/*
 * @title SegmentTree
 */
template<class Operator> class SegmentTree {
	using TypeNode = typename Operator::TypeNode; 
	size_t length;
	size_t num;
	vector<TypeNode> node;
	vector<pair<int,int>> range;
    inline void init() {
		for (int i = length - 1; i >= 0; --i) node[i] = Operator::func_node(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);
    }
public:

	//unitで初期化
	SegmentTree(const size_t num): num(num) {
		for (length = 1; length <= num; length *= 2);
		node.resize(2 * length, Operator::unit_node);
        init();
	}

	//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];
        init();
	}
 
	//同じ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);
		for (int i = 0; i < length; ++i) node[i+length] = init;
        init();
	}
	
	//[idx,idx+1)
	void update(size_t idx, const TypeNode var) {
		if(idx < 0 || length <= idx) return;
		idx += length;
		node[idx] = Operator::func_merge(node[idx],var);
		while(idx >>= 1) node[idx] = Operator::func_node(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::func_node(vl,node[l++]);
			if(r&1) vr = Operator::func_node(node[--r],vr);
		}
		return Operator::func_node(vl,vr);
	}

	//range[l,r) return [l,r] search max right
	int prefix_binary_search(int l, int r, TypeNode var) {
		assert(0 <= l && l < length && 0 < r && r <= length);
		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::func_check(Operator::func_node(ret,node[idx]),var)) {
				ret = Operator::func_node(ret,node[idx]);
				off = range[idx++].second;
				if(!(idx&1)) idx >>= 1;			
			}
			else{
				idx <<=1;
			}
		}
		return off;
	}

	//range(l,r] return [l,r] search max left
	int suffix_binary_search(const int l, const int r, const TypeNode var) {
		assert(-1 <= l && l < (int)length-1 && 0 <= r && r < length);
		TypeNode ret = Operator::unit_node;
		int off = r;
		for(size_t idx = r+length; idx < 2*length && l < off; ){
			if(l < range[idx].first && !Operator::func_check(Operator::func_node(ret,node[idx]),var)) {
				ret = Operator::func_node(node[idx],ret);
				off = range[idx--].first-1;
				if(idx&1) idx >>= 1;
			}
			else{
				idx = (idx<<1)+1;
			}
		}
		return off;
	}

	void print(){
		cout << "node" << endl;
		for(int i = 1,j = 1; i < 2*length; ++i) {
			cout << node[i] << " ";
			if(i==((1<<j)-1) && ++j) cout << endl;
		}
		cout << "vector" << endl;
		cout << "{ " << get(0,1);
		for(int i = 1; i < length; ++i) cout << ", " << get(i,i+1);
		cout << " }" << endl;
	}
};

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 func_node(const TypeNode l,const TypeNode r){return Gcd::gcd(l,r);}
	static constexpr TypeNode func_merge(const TypeNode l,const TypeNode r){return r;}
	static constexpr bool func_check(const TypeNode nodeVal,const 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 = N-1; 0 <= i; --i) {
	// 	ans += seg.suffix_binary_search(-1,i,1) + 1;
	// }
	for(int i = 0; i < N; ++i) {
        ans += N - seg.prefix_binary_search(i,N,1);
	}
	cout << ans << endl;
}
0