結果

問題 No.1036 Make One With GCD 2
ユーザー sykwer
提出日時 2020-04-24 22:48:26
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,456 ms / 2,000 ms
コード長 3,583 bytes
コンパイル時間 1,342 ms
コンパイル使用メモリ 121,080 KB
実行使用メモリ 15,360 KB
最終ジャッジ日時 2024-09-16 13:28:50
合計ジャッジ時間 22,106 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

/* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>
// algorithm library
#include <algorithm>
#include <cmath>
#include <numeric>
#include <random>
#include <cstring>
#include <cassert>
// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#include <stack>
// type
#include <cstdint>
#include <functional>
/* ---------- Namespace ---------- */
using namespace std;
/* ---------- Type ---------- */
using ll = long long;
#define int ll
template <class T> using MaxHeap = priority_queue<T>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
#define P pair<ll, ll>
/* ---------- Constants */
const double PI = 3.141592653589793238462643383279;
const ll MOD = 1e9 + 7;
const int INF = 1LL << 55;
/* ---------- Functions */
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
/* v-v-v-v-v-v-v-v-v Main Part v-v-v-v-v-v-v-v-v */
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a%b);
}
template <class T> class SegTree {
int N;
vector<T> data;
T ide; // Identity element (Initial value)
function<T(T, T)> operation; // Function for range query
function<T(T, T)> updater; // Function for point update
T _query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l) return ide;
if (a <= l && r <= b) {
return data[k];
} else {
T left = _query(a, b, 2 * k + 1, l, (l + r) / 2);
T right = _query(a, b, 2 * k + 2, (l + r) / 2, r);
return operation(left, right);
}
}
public:
// int n: Size of array
// T ide: Identity element
// function<T(T, T)> operation: Function to merge elements
// function<T(T, T)> updater: (old data[i], some value x) -> new data[i]
SegTree(size_t n, T ide, function<T(T, T)> operation,
function<T(T, T)> updater) : ide(ide), operation(operation), updater(updater) {
N = 1;
while (N < n) N *= 2;
data.resize(2 * N - 1, ide);
}
void update(int i, T x) {
i += N - 1;
data[i] = updater(data[i], x);
while (i > 0) {
i = (i - 1) / 2;
data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
}
}
T query(int a, int b) {
return _query(a, b, 0, 0, N);
}
T operator[](int i) {
return data[i + N - 1];
}
};
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
ll ide = INF;
auto operation = [](int left, int right) -> int {
if (left==INF) return right;
if (right == INF) return left;
return gcd(left, right);
};
auto updater = [](int old, int x) -> int {return x;};
auto seg = new SegTree<int>(N, ide, operation, updater);
for (int i = 0; i < N; i++) seg->update(i, A[i]);
int ret= 0;
// [left, right]
int right = 0;
for (int left = 0; left < N; left++) {
while (right < N && seg->query(left, right+1) != 1) right++;
if (right == N) break;
ret += N-right;
}
cout << ret << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0