Skip to content

Commit

Permalink
seg-improve
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo314 committed Feb 12, 2024
1 parent 5b722d1 commit 84365ad
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
13 changes: 0 additions & 13 deletions .vscode/settings.json

This file was deleted.

27 changes: 27 additions & 0 deletions cpp/segtree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cassert>
#include <functional>
#include <limits>
#include <numeric>
#include <ostream>
#include <vector>

Expand Down Expand Up @@ -299,6 +300,10 @@ namespace segtree {
struct Min {
const S operator() (const S& a, const S& b) const { return std::min(a, b); }
};
template <typename S, std::enable_if_t<std::is_integral_v<S>>* = nullptr>
struct Gcd {
constexpr S operator()(const S& a, const S& b) const { return std::gcd(a, b); }
};
template <typename S, std::enable_if_t<std::is_scalar_v<S>>* = nullptr>
struct MaxLimit {
constexpr S operator() () const { return std::numeric_limits<S>::max(); }
Expand All @@ -311,6 +316,14 @@ namespace segtree {
struct Zero {
S operator() () const { return S(0); }
};
template <typename S>
struct One {
S operator()() const { return S(1); }
};
template <typename S>
struct None {
S operator()() const { return S{}; }
};
}
/**
* @brief RangeMaxQuery
Expand All @@ -333,3 +346,17 @@ using RMinQ = StaticSegTree<S, segtree::Min<S>, segtree::MaxLimit<S>>;
*/
template <typename S>
using RSumQ = StaticSegTree<S, std::plus<S>, segtree::Zero<S>>;
/**
* @brief RangeProdQuery
*
* @tparam S 型
*/
template <typename S>
using RProdQ = StaticSegTree<S, std::multiplies<S>, segtree::One<S>>;
/**
* @brief RangeGcdQuery
*
* @tparam S 型
*/
template <typename S>
using RGcdQ = StaticSegTree<S, segtree::Gcd<S>, segtree::Zero<S>>;
32 changes: 32 additions & 0 deletions test/atcoder-abc254-f.text.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#define PROBLEM "https://atcoder.jp/contests/abc254/tasks/abc254_f"

#include <iostream>

#include "../cpp/segtree.hpp"

int main(void) {
int N, Q;
std::cin >> N >> Q;
std::vector<int> 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<int> 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;
}
}

0 comments on commit 84365ad

Please sign in to comment.