-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from NyaanNyaan/add_misc
add misc
- Loading branch information
Showing
27 changed files
with
922 additions
and
61 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
using namespace std; | ||
|
||
#include "../data-structure/binary-indexed-tree.hpp" | ||
|
||
// https://hitonanode.github.io/cplib-cpp/data_structure/rectangle_add_rectangle_sum.hpp | ||
template <class I, class T> | ||
class RectangleAddRectangleSum { | ||
struct AddQuery { | ||
I xl, xr, yl, yr; | ||
T val; | ||
}; | ||
struct SumQuery { | ||
I xl, xr, yl, yr; | ||
}; | ||
vector<AddQuery> add_queries; | ||
vector<SumQuery> sum_queries; | ||
|
||
public: | ||
RectangleAddRectangleSum() = default; | ||
|
||
void add_rectangle(I xl, I xr, I yl, I yr, T val) { | ||
add_queries.push_back(AddQuery{xl, xr, yl, yr, val}); | ||
} | ||
void add_query(I xl, I xr, I yl, I yr) { | ||
sum_queries.push_back(SumQuery{xl, xr, yl, yr}); | ||
} | ||
|
||
vector<T> solve() const { | ||
vector<I> ys; | ||
for (const auto &a : add_queries) { | ||
ys.push_back(a.yl); | ||
ys.push_back(a.yr); | ||
} | ||
sort(ys.begin(), ys.end()); | ||
ys.erase(unique(ys.begin(), ys.end()), ys.end()); | ||
|
||
const int Y = ys.size(); | ||
|
||
vector<tuple<I, int, int>> ops; | ||
for (int q = 0; q < int(sum_queries.size()); ++q) { | ||
ops.emplace_back(sum_queries[q].xl, 0, q); | ||
ops.emplace_back(sum_queries[q].xr, 1, q); | ||
} | ||
for (int q = 0; q < int(add_queries.size()); ++q) { | ||
ops.emplace_back(add_queries[q].xl, 2, q); | ||
ops.emplace_back(add_queries[q].xr, 3, q); | ||
} | ||
sort(ops.begin(), ops.end()); | ||
|
||
BinaryIndexedTree<T> b00(Y), b01(Y), b10(Y), b11(Y); | ||
vector<T> ret(sum_queries.size()); | ||
for (auto o : ops) { | ||
int qtype = get<1>(o), q = get<2>(o); | ||
if (qtype >= 2) { | ||
const AddQuery &query = add_queries.at(q); | ||
int i = lower_bound(ys.begin(), ys.end(), query.yl) - ys.begin(); | ||
int j = lower_bound(ys.begin(), ys.end(), query.yr) - ys.begin(); | ||
T x = get<0>(o); | ||
T yi = query.yl, yj = query.yr; | ||
if (qtype & 1) swap(i, j), swap(yi, yj); | ||
|
||
b00.add(i, x * yi * query.val); | ||
b01.add(i, -x * query.val); | ||
b10.add(i, -yi * query.val); | ||
b11.add(i, query.val); | ||
b00.add(j, -x * yj * query.val); | ||
b01.add(j, x * query.val); | ||
b10.add(j, yj * query.val); | ||
b11.add(j, -query.val); | ||
} else { | ||
const SumQuery &query = sum_queries.at(q); | ||
int i = lower_bound(ys.begin(), ys.end(), query.yl) - ys.begin(); | ||
int j = lower_bound(ys.begin(), ys.end(), query.yr) - ys.begin(); | ||
T x = get<0>(o); | ||
T yi = query.yl, yj = query.yr; | ||
if (qtype & 1) swap(i, j), swap(yi, yj); | ||
|
||
i--, j--; | ||
ret[q] += | ||
b00.sum(i) + b01.sum(i) * yi + b10.sum(i) * x + b11.sum(i) * x * yi; | ||
ret[q] -= | ||
b00.sum(j) + b01.sum(j) * yj + b10.sum(j) * x + b11.sum(j) * x * yj; | ||
} | ||
} | ||
return ret; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#pragma once | ||
|
||
#include <cassert> | ||
#include <utility> | ||
#include <vector> | ||
using namespace std; | ||
|
||
#include "../data-structure/union-find.hpp" | ||
#include "../internal/internal-type-traits.hpp" | ||
#include "graph-template.hpp" | ||
|
||
namespace FunctionalGraphImpl { | ||
|
||
ENABLE_HAS_VAR(cost) | ||
|
||
template <typename T = int> | ||
struct FunctionalGraph { | ||
int N; | ||
WeightedGraph<T> g; | ||
vector<int> to, represent; | ||
vector<T> weight; | ||
|
||
FunctionalGraph() = default; | ||
|
||
FunctionalGraph(int n, const vector<int>& adj, | ||
const vector<T>& w = vector<int>{}) | ||
: N(n), g(N + 1), to(N + 1, -1), represent(N + 1, -1), weight(N + 1) { | ||
assert((int)adj.size() == N); | ||
assert((int)w.size() == N or w.empty()); | ||
for (auto& x : adj) assert(0 <= x and x < N); | ||
|
||
UnionFind uf(N); | ||
for (int i = 0; i < N; i++) { | ||
int j = adj[i]; | ||
to[i] = j, weight[i] = w.empty() ? T{1} : w[i]; | ||
if (uf.same(i, j)) { | ||
g[N].emplace_back(N, i, weight[i]); | ||
} else { | ||
uf.unite(i, j); | ||
g[j].emplace_back(j, i, weight[i]); | ||
} | ||
} | ||
calc_represent(N, -1); | ||
} | ||
|
||
// _g は無向グラフ | ||
template <typename G> | ||
FunctionalGraph(int n, const G& _g) | ||
: N(n), g(N + 1), to(N + 1, -1), represent(N + 1, -1), weight(N + 1) { | ||
constexpr bool cost_flag = has_cost_v<typename G::value_type::value_type>; | ||
WeightedGraph<T> h(n); | ||
UnionFind uf(N); | ||
for (int i = 0; i < N; i++) { | ||
for (auto& j : _g[i]) { | ||
if (i > j) continue; | ||
T cost; | ||
if constexpr (cost_flag) { | ||
cost = j.cost; | ||
} else { | ||
cost = 1; | ||
} | ||
if (uf.same(i, j)) { | ||
// i -> j 向きということにして, i を根とする | ||
g[N].emplace_back(N, i, 0); | ||
to[i] = j, weight[i] = cost; | ||
} else { | ||
uf.unite(i, j); | ||
h[i].emplace_back(i, j, cost); | ||
h[j].emplace_back(j, i, cost); | ||
} | ||
} | ||
} | ||
|
||
auto dfs = [&](auto rc, int c, int p) -> void { | ||
for (auto& d : h[c]) { | ||
if (d == p) continue; | ||
T cost; | ||
if constexpr (cost_flag) { | ||
cost = d.cost; | ||
} else { | ||
cost = 1; | ||
} | ||
to[d] = c, weight[d] = cost; | ||
g[c].emplace_back(c, d, cost); | ||
rc(rc, d, c); | ||
} | ||
}; | ||
for (auto& r : g[N]) dfs(dfs, r, -1); | ||
|
||
calc_represent(N, -1); | ||
} | ||
|
||
vector<vector<int>> get_loops() const { | ||
vector<vector<int>> res; | ||
for (auto r : g[N]) { | ||
vector<int> cur{r}; | ||
for (int i = to[r]; i != r; i = to[i]) { | ||
cur.push_back(i); | ||
} | ||
res.push_back(cur); | ||
} | ||
return res; | ||
} | ||
|
||
// (u, {weight of u-v}) (v, {weight of v-w}), (w, ...) ... | ||
vector<vector<pair<int, T>>> get_loops_with_weight() const { | ||
vector<vector<pair<int, T>>> res; | ||
for (auto r : g[N]) { | ||
vector<pair<int, T>> cur{make_pair(r, weight[r])}; | ||
for (int i = to[r]; i != r; i = to[i]) { | ||
cur.emplace_back(i, weight[i]); | ||
} | ||
res.push_back(cur); | ||
} | ||
return res; | ||
} | ||
|
||
private: | ||
void calc_represent(int c, int r) { | ||
represent[c] = r; | ||
for (auto& d : g[c]) calc_represent(d, r == -1 ? d : r); | ||
} | ||
}; | ||
|
||
} // namespace FunctionalGraphImpl | ||
|
||
using FunctionalGraphImpl::FunctionalGraph; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include <cassert> | ||
#include <vector> | ||
using namespace std; | ||
|
||
#include "../math/constexpr-primitive-root.hpp" | ||
|
||
template <typename fps> | ||
fps multiplicative_convolution_mod_p(const fps& a, const fps& b, int p) { | ||
assert((int)a.size() == p); | ||
assert((int)b.size() == p); | ||
using mint = typename fps::value_type; | ||
int r = constexpr_primitive_root(p); | ||
vector<int> exp(p - 1), log(p); | ||
exp[0] = 1; | ||
for (int i = 1; i < p - 1; i++) exp[i] = 1LL * exp[i - 1] * r % p; | ||
for (int i = 0; i < p - 1; i++) log[exp[i]] = i; | ||
fps s(p - 1), t(p - 1); | ||
for (int i = 0; i < p - 1; i++) s[i] = a[exp[i]], t[i] = b[exp[i]]; | ||
fps u = s * t; | ||
for (int i = p - 1; i < (int)u.size(); i++) u[i % (p - 1)] += u[i]; | ||
fps c(p); | ||
for (int i = 1; i < p; i++) c[i] = u[log[i]]; | ||
|
||
mint sa = accumulate(begin(a), end(a), mint{}); | ||
mint sb = accumulate(begin(b), end(b), mint{}); | ||
c[0] = sa * b[0] + sb * a[0] - a[0] * b[0]; | ||
return c; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2891" | ||
// | ||
#include "../../template/template.hpp" | ||
// | ||
#include "../../data-structure/union-find.hpp" | ||
#include "../../graph/functional-graph.hpp" | ||
#include "../../graph/graph-template.hpp" | ||
|
||
using namespace Nyaan; | ||
|
||
void q() { | ||
ini(N); | ||
auto g = graph(N, N); | ||
FunctionalGraph fg{N, g}; | ||
UnionFind uf(N); | ||
auto loops = fg.get_loops(); | ||
each(loop, loops) { | ||
int x = loop[0]; | ||
each(y, loop) uf.unite(x, y); | ||
} | ||
ini(Q); | ||
while (Q--) { | ||
ini(a, b); | ||
--a, --b; | ||
out(uf.same(a, b) ? 2 : 1); | ||
} | ||
} | ||
|
||
void Nyaan::solve() { | ||
int t = 1; | ||
// in(t); | ||
while (t--) q(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.