Skip to content

Commit

Permalink
Merge pull request #103 from rainbou-kpr/seg-improve
Browse files Browse the repository at this point in the history
seg-improve
  • Loading branch information
KowerKoint authored Mar 6, 2024
2 parents 476e5dd + 1ca1648 commit e6e7545
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 20 deletions.
14 changes: 8 additions & 6 deletions .verify-helper/timestamps.remote.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"test/aoj-cgl-3-c.test.cpp": "2023-09-07 23:01:33 +0900",
"test/aoj-cgl-4-a.test.cpp": "2023-09-07 23:01:33 +0900",
"test/aoj-cgl-5-a.test.cpp": "2023-09-07 23:01:33 +0900",
"test/aoj-dsl-2-a.test.cpp": "2023-08-01 18:34:30 +0900",
"test/aoj-dsl-2-a.test.cpp": "2024-03-06 15:05:55 +0900",
"test/aoj-dsl-2-a.test.py": "2023-09-07 14:26:13 +0900",
"test/aoj-dsl-2-b.test.cpp": "2023-08-01 18:34:30 +0900",
"test/aoj-dsl-2-b.test.cpp": "2024-03-06 15:05:55 +0900",
"test/aoj-dsl-2-b.test.py": "2023-09-07 14:26:13 +0900",
"test/aoj-dsl-2-f.test.cpp": "2023-08-01 17:59:54 +0900",
"test/aoj-dsl-2-g.test.cpp": "2023-08-01 17:59:54 +0900",
Expand All @@ -20,11 +20,13 @@
"test/aoj-grl-5-b.test.cpp": "2023-06-16 15:41:49 +0900",
"test/atcoder-abc177-f.1.test.cpp": "2023-08-01 17:59:54 +0900",
"test/atcoder-abc177-f.2.test.cpp": "2023-08-01 17:59:54 +0900",
"test/atcoder-abc185-f.test.cpp": "2024-03-06 15:05:55 +0900",
"test/atcoder-abc254-f.test.cpp": "2024-03-06 15:05:55 +0900",
"test/atcoder-abc282-d.test.cpp": "2023-11-30 23:29:28 +0900",
"test/atcoder-abc300-b.test.cpp": "2023-10-12 08:50:40 +0900",
"test/atcoder-abc331-f.test.cpp": "2024-02-12 01:35:20 +0900",
"test/atcoder-edpc-g.test.cpp": "2023-11-30 23:29:28 +0900",
"test/atcoder-past202012-n.test.cpp": "2023-08-01 18:34:30 +0900",
"test/atcoder-past202012-n.test.cpp": "2024-03-06 15:05:55 +0900",
"test/atcoder-past202012-n.test.py": "2023-09-07 14:26:13 +0900",
"test/yosupo-binomial-coefficient.test.cpp": "2023-09-16 00:25:06 +0900",
"test/yosupo-convolution-mod-1000000007.test.cpp": "2023-09-16 00:07:15 +0900",
Expand All @@ -36,14 +38,14 @@
"test/yosupo-exp-of-fps.test.cpp": "2024-01-20 00:58:37 +0900",
"test/yosupo-inv-of-fps.test.cpp": "2024-01-20 00:58:37 +0900",
"test/yosupo-lca.1.test.cpp": "2023-06-16 15:41:49 +0900",
"test/yosupo-lca.2.test.cpp": "2023-08-01 18:34:30 +0900",
"test/yosupo-lca.2.test.cpp": "2024-03-06 15:05:55 +0900",
"test/yosupo-line-add-get-min.test.cpp": "2023-06-19 07:11:52 +0900",
"test/yosupo-log-of-fps.test.cpp": "2024-01-20 00:58:37 +0900",
"test/yosupo-partition-function_1.test.cpp": "2024-01-20 00:58:37 +0900",
"test/yosupo-partition-function_2.test.cpp": "2024-01-20 00:58:37 +0900",
"test/yosupo-point-add-rectangle-sum.test.cpp": "2023-06-25 14:14:02 +0900",
"test/yosupo-point-set-range-composite.1.test.cpp": "2023-08-29 23:08:45 +0900",
"test/yosupo-point-set-range-composite.2.test.cpp": "2023-08-29 23:08:45 +0900",
"test/yosupo-point-set-range-composite.1.test.cpp": "2024-03-06 15:05:55 +0900",
"test/yosupo-point-set-range-composite.2.test.cpp": "2024-03-06 15:05:55 +0900",
"test/yosupo-point-set-range-composite.test.py": "2023-09-07 14:26:13 +0900",
"test/yosupo-polynomial-taylor-shift.test.cpp": "2024-01-20 00:58:37 +0900",
"test/yosupo-pow-of-fps.test.cpp": "2024-01-20 00:58:37 +0900",
Expand Down
13 changes: 0 additions & 13 deletions .vscode/settings.json

This file was deleted.

36 changes: 35 additions & 1 deletion 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 @@ -332,4 +345,25 @@ using RMinQ = StaticSegTree<S, segtree::Min<S>, segtree::MaxLimit<S>>;
* @tparam S 型
*/
template <typename S>
using RSumQ = StaticSegTree<S, std::plus<S>, segtree::Zero<S>>;
using RSumQ = StaticSegTree<S, std::plus<S>, segtree::None<S>>;
/**
* @brief RangeProdQuery
*
* @tparam S 型
*/
template <typename S>
using RProdQ = StaticSegTree<S, std::multiplies<S>, segtree::One<S>>;
/**
* @brief RangeXorQuery
*
* @tparam S 型
*/
template <typename S>
using RXorQ = StaticSegTree<S, std::bit_xor<S>, segtree::Zero<S>>;
/**
* @brief RangeGcdQuery
*
* @tparam S 型
*/
template <typename S>
using RGcdQ = StaticSegTree<S, segtree::Gcd<S>, segtree::Zero<S>>;
26 changes: 26 additions & 0 deletions test/atcoder-abc185-f.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#define PROBLEM "https://atcoder.jp/contests/abc185/tasks/abc185_f"

#include <iostream>

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

int main(void) {
int N, Q;
std::cin >> N >> Q;
std::vector<int> A(N);
for (int& a : A) {
std::cin >> a;
}
RXorQ<int> seg(A);
while (Q--) {
int T, X, Y;
std::cin >> T >> X >> Y;
if (T == 1) {
seg.apply(X - 1, Y);
} else {
int ans;
ans = seg.prod(X - 1, Y);
std::cout << ans << std::endl;
}
}
}
32 changes: 32 additions & 0 deletions test/atcoder-abc254-f.test.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 (int& a : A) {
std::cin >> a;
}
for (int& b : B) {
std::cin >> b;
}
dA.reserve(N - 1);
dB.reserve(N - 1);
for (int 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 e6e7545

Please sign in to comment.