diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index e268551..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "files.associations": { - "array": "cpp", - "deque": "cpp", - "list": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "string_view": "cpp", - "initializer_list": "cpp", - "valarray": "cpp" - } -} \ No newline at end of file diff --git a/cpp/segtree.hpp b/cpp/segtree.hpp index 406af71..0b891bb 100644 --- a/cpp/segtree.hpp +++ b/cpp/segtree.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -299,6 +300,10 @@ namespace segtree { struct Min { const S operator() (const S& a, const S& b) const { return std::min(a, b); } }; + template >* = nullptr> + struct Gcd { + constexpr S operator()(const S& a, const S& b) const { return std::gcd(a, b); } + }; template >* = nullptr> struct MaxLimit { constexpr S operator() () const { return std::numeric_limits::max(); } @@ -311,6 +316,14 @@ namespace segtree { struct Zero { S operator() () const { return S(0); } }; + template + struct One { + S operator()() const { return S(1); } + }; + template + struct None { + S operator()() const { return S{}; } + }; } /** * @brief RangeMaxQuery @@ -333,3 +346,17 @@ using RMinQ = StaticSegTree, segtree::MaxLimit>; */ template using RSumQ = StaticSegTree, segtree::Zero>; +/** + * @brief RangeProdQuery + * + * @tparam S 型 + */ +template +using RProdQ = StaticSegTree, segtree::One>; +/** + * @brief RangeGcdQuery + * + * @tparam S 型 + */ +template +using RGcdQ = StaticSegTree, segtree::Zero>; diff --git a/test/atcoder-abc254-f.text.cpp b/test/atcoder-abc254-f.text.cpp new file mode 100644 index 0000000..36de6ad --- /dev/null +++ b/test/atcoder-abc254-f.text.cpp @@ -0,0 +1,32 @@ +#define PROBLEM "https://atcoder.jp/contests/abc254/tasks/abc254_f" + +#include + +#include "../cpp/segtree.hpp" + +int main(void) { + int N, Q; + std::cin >> N >> Q; + std::vector A(N), B(N), dA, dB; + for (size_t i = 0; i < N; i++) { + std::cin >> A[i]; + } + for (size_t i = 0; i < N; i++) { + std::cin >> B[i]; + } + dA.reserve(N - 1); + dB.reserve(N - 1); + for (size_t i = 0; i < N - 1; i++) { + dA.push_back(A[i + 1] - A[i]); + dB.push_back(B[i + 1] - B[i]); + } + RGcdQ seg1(dA), seg2(dB); + while (Q--) { + int h1, h2, w1, w2; + std::cin >> h1 >> h2 >> w1 >> w2; + int g = A[h1 - 1] + B[w1 - 1]; + g = std::gcd(g, seg1.prod(h1 - 1, h2 - 1)); + g = std::gcd(g, seg2.prod(w1 - 1, w2 - 1)); + std::cout << g << std::endl; + } +} \ No newline at end of file