結果

問題 No.1036 Make One With GCD 2
ユーザー ningenMeningenMe
提出日時 2020-04-25 23:49:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,361 ms / 2,000 ms
コード長 4,525 bytes
コンパイル時間 1,783 ms
コンパイル使用メモリ 176,548 KB
実行使用メモリ 31,744 KB
最終ジャッジ日時 2024-09-16 14:03:47
合計ジャッジ時間 18,986 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,361 ms
31,744 KB
testcase_01 AC 186 ms
31,744 KB
testcase_02 AC 196 ms
31,616 KB
testcase_03 AC 23 ms
17,024 KB
testcase_04 AC 41 ms
30,336 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 63 ms
17,024 KB
testcase_08 AC 56 ms
16,768 KB
testcase_09 AC 345 ms
31,488 KB
testcase_10 AC 322 ms
31,232 KB
testcase_11 AC 349 ms
31,744 KB
testcase_12 AC 320 ms
31,232 KB
testcase_13 AC 528 ms
31,488 KB
testcase_14 AC 527 ms
31,488 KB
testcase_15 AC 489 ms
31,360 KB
testcase_16 AC 529 ms
31,360 KB
testcase_17 AC 516 ms
31,488 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 486 ms
31,232 KB
testcase_23 AC 382 ms
30,208 KB
testcase_24 AC 541 ms
31,488 KB
testcase_25 AC 496 ms
31,232 KB
testcase_26 AC 520 ms
31,232 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 205 ms
31,744 KB
testcase_39 AC 718 ms
31,744 KB
testcase_40 AC 383 ms
30,336 KB
testcase_41 AC 842 ms
31,616 KB
testcase_42 AC 800 ms
31,616 KB
testcase_43 AC 915 ms
31,744 KB
testcase_44 AC 961 ms
31,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


class gcd{
public:
	static inline 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);
	}
	static inline 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);
	}
	static inline long long num(long long n, long long m) {
		return (n>m ? pre(n,m) : pre(m,n));
	}
};

template<class Operator> class SegmentTree {
	using typeNode = typename Operator::type; 
	size_t length;
	size_t num;
	typeNode unitNode;
	vector<typeNode> node;
	vector<pair<size_t,size_t>> range;
public:

	//unitで初期化
	SegmentTree(const size_t num, typeNode unitNode): num(num),unitNode(unitNode) {
		for (length = 1; length < num; length *= 2);
		node.resize(2 * length, unitNode);
		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, typeNode unitNode) : num(vec.size()),unitNode(unitNode) {
		for (length = 1; length < vec.size(); length *= 2);
		node.resize(2 * length, unitNode);
		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, typeNode unitNode, const typeNode init) : num(num),unitNode(unitNode) {
		for (length = 1; length < num; length *= 2);
		node.resize(2 * length, unitNode);
		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 unitNode;
		typeNode vl = unitNode, vr = unitNode;
		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 = unitNode;
		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 = unitNode;
		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;
	}
};


template<class typeNode> struct nodeGCDPointUpdate {
	using type = typeNode;
	static typeNode funcNode(typeNode l,typeNode r){return gcd::num(l,r);}
	static typeNode funcMerge(typeNode l,typeNode r){return r;}
	static 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,0);
	ll ans = 0;
	for(int i = 0; i < N; ++i) {
		ans += N - seg.BinarySearch(i,N,1);
	}
	cout << ans << endl;
}
0