From 53f807d287606d26d07e77908a171030466c236e Mon Sep 17 00:00:00 2001 From: arielherself Date: Tue, 17 Dec 2024 00:16:57 +0800 Subject: [PATCH] Add dp/mo.cc --- dp/mo.cc | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 dp/mo.cc diff --git a/dp/mo.cc b/dp/mo.cc new file mode 100644 index 0000000..ceedf27 --- /dev/null +++ b/dp/mo.cc @@ -0,0 +1,45 @@ +template +vector mo(record_t& solver, const vector& queries, int n) { + int m = queries.size(); + int block = sqrt(ld(n)); + vector idx(m); + iota(idx.begin(), idx.end(), 0); + sort(idx.begin(), idx.end(), [&] (int i, int j) { + auto&& lhs = queries[i]; + auto&& rhs = queries[j]; + if (lhs.first / block != rhs.first / block) { + return lhs.first < rhs.first; + } + if (lhs.first / block % 2) { + return lhs.second < rhs.second; + } + return lhs.second > rhs.second; + }); + vector res(m); + for (auto&& i : idx) { + auto&& [tl, tr] = queries[i]; + while (solver.l > tl) solver.movell(); + while (solver.r < tr) solver.moverr(); + while (solver.l < tl) solver.movelr(); + while (solver.r > tr) solver.moverl(); + res[i] = solver.get(); + } + return res; +} + +struct { + using return_t = int; + int l = 0, r = 0; + return_t res = 0; + return_t get(void) const { + return res; + } + void movell(void) { + } + void movelr(void) { + } + void moverr(void) { + } + void moverl(void) { + } +} solver;