#define PROBLEM "https://judge.yosupo.jp/problem/static_range_inversions_query" #include <iostream> #include <vector> #include "Mylib/IO/input_tuples.cpp" #include "Mylib/IO/input_vector.cpp" #include "Mylib/Typical/range_inversions_query.cpp" namespace hl = haar_lib; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int N, Q; std::cin >> N >> Q; auto A = hl::input_vector<int>(N); hl::range_inversions_query riq(A); for (auto [l, r] : hl::input_tuples<int, int>(Q)) { riq.add(l, r); } auto ans = riq.solve(); for (auto x : ans) { std::cout << x << "\n"; } return 0; }
#line 1 "test/yosupo-judge/static_range_inversions_query/main.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/static_range_inversions_query" #include <iostream> #include <vector> #line 2 "Mylib/IO/input_tuples.cpp" #include <initializer_list> #line 4 "Mylib/IO/input_tuples.cpp" #include <tuple> #include <utility> #line 6 "Mylib/IO/input_tuple.cpp" namespace haar_lib { template <typename T, size_t... I> static void input_tuple_helper(std::istream &s, T &val, std::index_sequence<I...>) { (void) std::initializer_list<int>{(void(s >> std::get<I>(val)), 0)...}; } template <typename T, typename U> std::istream &operator>>(std::istream &s, std::pair<T, U> &value) { s >> value.first >> value.second; return s; } template <typename... Args> std::istream &operator>>(std::istream &s, std::tuple<Args...> &value) { input_tuple_helper(s, value, std::make_index_sequence<sizeof...(Args)>()); return s; } } // namespace haar_lib #line 8 "Mylib/IO/input_tuples.cpp" namespace haar_lib { template <typename... Args> class InputTuples { struct iter { using value_type = std::tuple<Args...>; value_type value; bool fetched = false; int N, c = 0; value_type operator*() { if (not fetched) { std::cin >> value; } return value; } void operator++() { ++c; fetched = false; } bool operator!=(iter &) const { return c < N; } iter(int N) : N(N) {} }; int N; public: InputTuples(int N) : N(N) {} iter begin() const { return iter(N); } iter end() const { return iter(N); } }; template <typename... Args> auto input_tuples(int N) { return InputTuples<Args...>(N); } } // namespace haar_lib #line 4 "Mylib/IO/input_vector.cpp" namespace haar_lib { template <typename T> std::vector<T> input_vector(int N) { std::vector<T> ret(N); for (int i = 0; i < N; ++i) std::cin >> ret[i]; return ret; } template <typename T> std::vector<std::vector<T>> input_vector(int N, int M) { std::vector<std::vector<T>> ret(N); for (int i = 0; i < N; ++i) ret[i] = input_vector<T>(M); return ret; } } // namespace haar_lib #line 2 "Mylib/Typical/range_inversions_query.cpp" #include <algorithm> #line 3 "Mylib/Algorithm/mo_algorithm.cpp" #include <cassert> #include <cmath> #line 6 "Mylib/Algorithm/mo_algorithm.cpp" namespace haar_lib { template <typename AppendLeft, typename AppendRight, typename RemoveLeft, typename RemoveRight, typename Query> class mo_algorithm { int N_, Q_, index_, width_; std::vector<int> left_, right_, ord_; AppendLeft append_left_; AppendRight append_right_; RemoveLeft remove_left_; RemoveRight remove_right_; Query query_; bool is_built_ = false; public: mo_algorithm() {} mo_algorithm( int N, int Q, const AppendLeft &append_left, const AppendRight &append_right, const RemoveLeft &remove_left, const RemoveRight &remove_right, const Query &query) : N_(N), Q_(Q), index_(0), width_(std::sqrt(N)), left_(Q), right_(Q), ord_(Q), append_left_(append_left), append_right_(append_right), remove_left_(remove_left), remove_right_(remove_right), query_(query) {} // [l, r) void add(int l, int r) { left_[index_] = l; right_[index_] = r; ord_[index_] = index_; ++index_; } void run() { std::sort( ord_.begin(), ord_.end(), [&](int i, int j) { const int a = left_[i] / width_, b = left_[j] / width_; if (a == b) { if (a & 1) return right_[i] < right_[j]; else return right_[i] > right_[j]; } else { return a < b; } }); int q = 0; int l = left_[ord_[0]], r = left_[ord_[0]]; for (int i = 0; i < Q_; ++i) { int id = ord_[q++]; while (l != left_[id] or r != right_[id]) { if (l > left_[id]) append_left_(--l); if (l < left_[id]) remove_left_(l++); if (r < right_[id]) append_right_(r++); if (r > right_[id]) remove_right_(--r); } query_(id); } } }; } // namespace haar_lib #line 4 "Mylib/DataStructure/FenwickTree/fenwick_tree_add.cpp" namespace haar_lib { template <typename T> class fenwick_tree_add { public: using value_type = T; private: int size_; std::vector<value_type> data_; public: fenwick_tree_add() {} fenwick_tree_add(int size) : size_(size), data_(size + 1, 0) {} void update(int i, value_type val) { assert(0 <= i and i < size_); i += 1; // 1-index while (i <= size_) { data_[i] = data_[i] + val; i += i & (-i); } } value_type fold(int i) const { // [0, i) assert(0 <= i and i <= size_); value_type ret = 0; while (i > 0) { ret = ret + data_[i]; i -= i & (-i); } return ret; } value_type fold(int l, int r) const { // [l, r) assert(0 <= l and l <= r and r <= size_); return fold(r) - fold(l); } value_type operator[](int x) const { return fold(x, x + 1); } }; } // namespace haar_lib #line 7 "Mylib/Typical/range_inversions_query.cpp" namespace haar_lib { template <typename T> class range_inversions_query { std::vector<int> data_; int N_; std::vector<std::pair<int, int>> qs_; public: range_inversions_query() {} range_inversions_query(std::vector<T> a) : N_(a.size()) { auto b = a; std::sort(b.begin(), b.end()); b.erase(std::unique(b.begin(), b.end()), b.end()); for (auto x : a) { const int i = std::lower_bound(b.begin(), b.end(), x) - b.begin(); data_.push_back(i); } } void add(int l, int r) { // [l, r) qs_.emplace_back(l, r); } auto solve() { const int Q = qs_.size(); fenwick_tree_add<int64_t> b(N_); int64_t t = 0; std::vector<int64_t> ans(Q); auto append_left = [&](int i) { t += b.fold(0, data_[i]); b.update(data_[i], 1); }; auto append_right = [&](int i) { t += b.fold(data_[i] + 1, N_); b.update(data_[i], 1); }; auto remove_left = [&](int i) { t -= b.fold(0, data_[i]); b.update(data_[i], -1); }; auto remove_right = [&](int i) { t -= b.fold(data_[i] + 1, N_); b.update(data_[i], -1); }; auto query = [&](int i) { ans[i] = t; }; auto mo = mo_algorithm( N_, Q, append_left, append_right, remove_left, remove_right, query); for (auto [l, r] : qs_) { mo.add(l, r); } mo.run(); return ans; } }; } // namespace haar_lib #line 8 "test/yosupo-judge/static_range_inversions_query/main.test.cpp" namespace hl = haar_lib; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int N, Q; std::cin >> N >> Q; auto A = hl::input_vector<int>(N); hl::range_inversions_query riq(A); for (auto [l, r] : hl::input_tuples<int, int>(Q)) { riq.add(l, r); } auto ans = riq.solve(); for (auto x : ans) { std::cout << x << "\n"; } return 0; }