This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ngthanhtrung23/ACM_Notebook_new
#define PROBLEM "https://judge.yosupo.jp/problem/staticrmq" #include "../../template.h" #include "../Fenwick/PartiallyPersistentFenwick.h" struct Data { int x; Data() : x(INT_MAX) {} Data(int _x) : x(_x) {} }; Data operator + (const Data& a, const Data& b) { return Data{min(a.x, b.x)}; } bool operator < (const Data&, const Data&) { return false; } void solve() { int n, q; cin >> n >> q; vector<int> a(n); REP(i,n) cin >> a[i]; PartiallyPersistentFenwick<Data> fen(n); FORD(i,n-1,0) fen.update(n-i, i, Data{a[i]}); while (q--) { int l, r; cin >> l >> r; auto res = fen.get(n-l, r); cout << res.x << '\n'; } }
#line 1 "DataStructure/test/persistent_fenwick_tree_rmq.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/staticrmq" #line 1 "template.h" #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++) #define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--) #define REP(i,a) for(int i=0,_a=(a); i<_a; i++) #define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) #define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; } #define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; } #define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; } #define sqr(x) ((x) * (x)) // For printing pair, container, etc. // Copied from https://quangloc99.github.io/2021/07/30/my-CP-debugging-template.html template<class U, class V> ostream& operator << (ostream& out, const pair<U, V>& p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class Con, class = decltype(begin(declval<Con>()))> typename enable_if<!is_same<Con, string>::value, ostream&>::type operator << (ostream& out, const Con& con) { out << '{'; for (auto beg = con.begin(), it = beg; it != con.end(); it++) { out << (it == beg ? "" : ", ") << *it; } return out << '}'; } template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template<class ...U> ostream& operator << (ostream& out, const tuple<U...>& t) { return print_tuple_utils<0, tuple<U...>>(out, t); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); long long get_rand(long long r) { return uniform_int_distribution<long long> (0, r-1)(rng); } template<typename T> vector<T> read_vector(int n) { vector<T> res(n); for (int& x : res) cin >> x; return res; } void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); solve(); return 0; } #line 1 "DataStructure/Fenwick/PartiallyPersistentFenwick.h" // NOTE: // - 0-based index // - for updates: time must be in increasing order // - Update: O(log), Get: O(log^2) // // Partially Persistent FenwickTree {{{ template< typename T // need to support operators + - < > struct PartiallyPersistentFenwick { PartiallyPersistentFenwick(int _n) : n(_n), f(_n + 1) { for (int i = 0; i <= n; ++i) { f[i].emplace_back(INT_MIN, T{}); } } // a[u] += val void update(int time, int u, T val) { assert(0 <= u && u < n); assert(last_updated_time <= time); last_updated_time = time; ++u; for (; u <= n; u += u & -u) { f[u].emplace_back(time, f[u].back().second + val); } } // return a[0] + .. + a[u-1] T get(int time, int u) const { assert(0 <= u && u <= n); T res{}; for (; u > 0; u -= u & -u) { auto it = lower_bound(f[u].begin(), f[u].end(), make_pair(time+1, T{})); res = res + prev(it)->second; } return res; } // return a[l] + .. + a[r-1] T get(int time, int l, int r) const { assert(0 <= l && l <= r && r <= n); if (l == r) return T{}; // empty return get(time, r) - get(time, l); } int n; int last_updated_time = INT_MIN; vector<vector<pair<int, T>>> f; // (time, data) }; // }}} #line 4 "DataStructure/test/persistent_fenwick_tree_rmq.test.cpp" struct Data { int x; Data() : x(INT_MAX) {} Data(int _x) : x(_x) {} }; Data operator + (const Data& a, const Data& b) { return Data{min(a.x, b.x)}; } bool operator < (const Data&, const Data&) { return false; } void solve() { int n, q; cin >> n >> q; vector<int> a(n); REP(i,n) cin >> a[i]; PartiallyPersistentFenwick<Data> fen(n); FORD(i,n-1,0) fen.update(n-i, i, Data{a[i]}); while (q--) { int l, r; cin >> l >> r; auto res = fen.get(n-l, r); cout << res.x << '\n'; } }