competitive_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub knshnb/competitive_library

:heavy_check_mark: test/yukicoder/1036.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/1036"

#include <bits/stdc++.h>  // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef _MY_DEBUG
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Sat Apr 25 17:19:30 JST 2020
 **/

#define CALL_FROM_TEST
#include "../../src/DataStructure/SlidingWindowAggregation.hpp"
#undef CALL_FROM_TEST

signed main() {
    Int n;
    std::cin >> n;
    std::vector<Int> a(n);
    REP(i, n) std::cin >> a[i];
    a.push_back(1);
    auto swag = make_swag<Int>([](Int x, Int y) { return std::gcd(x, y); }, 0);
    Int ans = 0, j = -1;
    REP(i, n) {
        while (swag.fold_all() != 1) {
            swag.push(a[++j]);
        }
        ans += n - j;
        swag.pop();
    }
    std::cout << ans << std::endl;
}
#line 1 "test/yukicoder/1036.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/1036"

#include <bits/stdc++.h>  // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef _MY_DEBUG
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Sat Apr 25 17:19:30 JST 2020
 **/

#define CALL_FROM_TEST
#line 1 "src/DataStructure/SlidingWindowAggregation.hpp"
/// @docs src/DataStructure/SlidingWindowAggregation.md
template <class T, class F> struct SlidingWindowAggregation {
    const F op;
    const T e;
    std::stack<std::pair<T, T>> st1, st2;  // それぞれ、{val, acc}を要素に持つような前方向と後ろ方向のstack
    SlidingWindowAggregation(F op_, T e_) : op(op_), e(e_) { st1.emplace(e, e), st2.emplace(e, e); }
    int size() { return st1.size() + st2.size() - 2; }
    void push(T x) {
        T acc = op(st2.top().second, x);
        st2.emplace(x, acc);
    }
    void pop() {
        assert(st1.size() > 1 || st2.size() > 1);
        if (st1.size() > 1) {
            st1.pop();
        } else {
            while (st2.size() > 2) {
                T acc = op(st1.top().second, st2.top().first);
                st1.emplace(st2.top().first, acc);
                st2.pop();
            }
            st2.pop();
        }
    }
    T fold_all() { return op(st1.top().second, st2.top().second); }
};
template <class T, class F> auto make_swag(F op, T e_) { return SlidingWindowAggregation<T, F>(op, e_); }
#line 19 "test/yukicoder/1036.test.cpp"
#undef CALL_FROM_TEST

signed main() {
    Int n;
    std::cin >> n;
    std::vector<Int> a(n);
    REP(i, n) std::cin >> a[i];
    a.push_back(1);
    auto swag = make_swag<Int>([](Int x, Int y) { return std::gcd(x, y); }, 0);
    Int ans = 0, j = -1;
    REP(i, n) {
        while (swag.fold_all() != 1) {
            swag.push(a[++j]);
        }
        ans += n - j;
        swag.pop();
    }
    std::cout << ans << std::endl;
}
Back to top page