結果

問題 No.1036 Make One With GCD 2
ユーザー ningenMeningenMe
提出日時 2020-04-25 22:33:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,283 bytes
コンパイル時間 1,759 ms
コンパイル使用メモリ 174,576 KB
実行使用メモリ 31,788 KB
最終ジャッジ日時 2023-10-14 20:10:00
合計ジャッジ時間 21,785 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 AC 246 ms
31,548 KB
testcase_02 AC 200 ms
31,580 KB
testcase_03 AC 20 ms
16,824 KB
testcase_04 AC 33 ms
30,124 KB
testcase_05 AC 1 ms
4,368 KB
testcase_06 AC 1 ms
4,368 KB
testcase_07 AC 79 ms
16,960 KB
testcase_08 AC 66 ms
16,784 KB
testcase_09 AC 314 ms
31,384 KB
testcase_10 AC 290 ms
31,124 KB
testcase_11 AC 319 ms
31,528 KB
testcase_12 AC 293 ms
31,392 KB
testcase_13 AC 434 ms
31,276 KB
testcase_14 AC 439 ms
31,264 KB
testcase_15 AC 412 ms
30,988 KB
testcase_16 AC 415 ms
31,128 KB
testcase_17 AC 426 ms
31,256 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,368 KB
testcase_20 AC 3 ms
4,372 KB
testcase_21 AC 3 ms
4,372 KB
testcase_22 AC 404 ms
31,128 KB
testcase_23 AC 301 ms
30,216 KB
testcase_24 AC 421 ms
31,380 KB
testcase_25 AC 386 ms
30,900 KB
testcase_26 AC 399 ms
31,096 KB
testcase_27 AC 1 ms
4,368 KB
testcase_28 AC 2 ms
4,372 KB
testcase_29 AC 1 ms
4,372 KB
testcase_30 AC 1 ms
4,372 KB
testcase_31 AC 2 ms
4,372 KB
testcase_32 AC 2 ms
4,372 KB
testcase_33 AC 2 ms
4,372 KB
testcase_34 AC 2 ms
4,368 KB
testcase_35 AC 2 ms
4,372 KB
testcase_36 AC 1 ms
4,368 KB
testcase_37 AC 1 ms
4,368 KB
testcase_38 AC 197 ms
31,788 KB
testcase_39 AC 1,102 ms
31,644 KB
testcase_40 AC 303 ms
30,016 KB
testcase_41 AC 1,200 ms
31,576 KB
testcase_42 AC 1,143 ms
31,644 KB
testcase_43 AC 1,296 ms
31,516 KB
testcase_44 AC 1,365 ms
31,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 {
	Operator Op;                            
	using typeNode = decltype(Op.unitNode); 
	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, Op.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) : num(vec.size()) {
		for (length = 1; length < vec.size(); length *= 2);
		node.resize(2 * length, Op.unitNode);
		for (int i = 0; i < vec.size(); ++i) node[i + length] = vec[i];
		for (int i = length - 1; i >= 0; --i) node[i] = Op.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, init);
		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);
	}
	
	//[idx,idx+1)
	void update(size_t idx, const typeNode var) {
		if(idx < 0 || length <= idx) return;
		idx += length;
		node[idx] = Op.funcMerge(node[idx],var);
		while(idx >>= 1) node[idx] = Op.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 Op.unitNode;
		typeNode vl = Op.unitNode, vr = Op.unitNode;
		for(l += length, r += length; l < r; l >>=1, r >>=1) {
			if(l&1) vl = Op.funcNode(vl,node[l++]);
			if(r&1) vr = Op.funcNode(node[--r],vr);
		}
		return Op.funcNode(vl,vr);
	}

	//return [0,length]
	int PrefixBinarySearch(typeNode var) {
		if(!Op.funcCheck(node[1],var)) return num;
		typeNode ret = Op.unitNode;
		size_t idx = 2;
		for(; idx < 2*length; idx<<=1){
			if(!Op.funcCheck(Op.funcNode(ret,node[idx]),var)) {
				ret = Op.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 = Op.unitNode;
		size_t off = l;
		for(size_t idx = l+length; idx < 2*length && off < r; ){
			if(range[idx].second<=r && !Op.funcCheck(Op.funcNode(ret,node[idx]),var)) {
				ret = Op.funcNode(ret,node[idx]);
				off = range[idx++].second;
				if(!(idx&1)) idx >>= 1;			
			}
			else{
				idx <<=1;
			}
		}
		return off;
	}
};

class gcd{
public:
	static inline long long impl(long long n, long long m) {
		static constexpr long long K = 5;
		for(int i = 0; i < 80; ++i) {
			long long t = n - m;
			long long s = n - m * K;
			bool q = t < m;
			bool p = t < m * K;
			n = q ? m : t;
			m = q ? t : m;
			if(m == 0) { return n; }
			n = p ? 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; i < 4; ++i) {
			t = n - m;
			if(t < m) n=m,m=t;
			else n=t;
			if(!m) return n;
		}
		return impl(n, m);
	}
	static inline long long g(long long n, long long m) {
		return pre(max(n,m),min(n,m));
	}
};


template<class typeNode> struct nodeGCDPointUpdate {
	typeNode unitNode = 0;
	typeNode funcNode(typeNode l,typeNode r){return gcd::g(l,r);}
	typeNode funcMerge(typeNode l,typeNode r){return r;}
	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;
}
0