#line 1 "a.cpp" #define PROBLEM "https://yukicoder.me/problems/no/924" #line 2 "/home/kuhaku/atcoder/github/algo/lib/template/template.hpp" #pragma GCC target("sse4.2,avx2,bmi2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include template bool chmax(T &a, const U &b) { return a < (T)b ? a = (T)b, true : false; } template bool chmin(T &a, const U &b) { return (T)b < a ? a = (T)b, true : false; } constexpr std::int64_t INF = 1000000000000000003; constexpr int Inf = 1000000003; constexpr int MOD = 1000000007; constexpr int MOD_N = 998244353; constexpr double EPS = 1e-7; constexpr double PI = M_PI; #line 2 "/home/kuhaku/atcoder/github/algo/lib/algorithm/rollback_mo.hpp" /** * @brief Mo's algorithm (rollback) * @see https://ei1333.hateblo.jp/entry/2017/09/11/211011 * @see https://snuke.hatenablog.com/entry/2016/07/01/000000 */ struct rollback_mo { rollback_mo(int n) : _left(), _right(), _order(), _size(n) {} void input(int q, int bias = 1, int closed = 0) { for (int i = 0; i < q; ++i) { int l, r; std::cin >> l >> r; this->add(l - bias, r - bias + closed); } } void add(int l, int r) { this->_left.emplace_back(l); this->_right.emplace_back(r); } void emplace(int l, int r) { return this->add(l, r); } void insert(int l, int r) { return this->add(l, r); } template void solve(F rem, G save, H load, I add) { return solve(rem, save, load, add, add); } template void solve(F rem, G save, H load, I addl, J addr) { int q = this->_left.size(); int width = std::max(1, int(this->_size / std::sqrt(q))); this->_order.resize(q); std::iota(this->_order.begin(), this->_order.end(), 0); std::sort(this->_order.begin(), this->_order.end(), [&](int a, int b) -> bool { if (this->_left[a] / width != this->_left[b] / width) return this->_left[a] < this->_left[b]; return this->_right[a] < this->_right[b]; }); auto reset = save(); for (auto idx : this->_order) { if (this->_right[idx] - this->_left[idx] < width) { for (int i = this->_left[idx]; i < this->_right[idx]; i++) addr(i); rem(idx); load(reset); } } int right = 0, last_block = -1; for (auto idx : this->_order) { if (this->_right[idx] - this->_left[idx] < width) continue; int block = this->_left[idx] / width; if (block != last_block) { load(reset); last_block = block; right = (block + 1) * width; } while (right < this->_right[idx]) addr(right++); auto snapshot = save(); for (int j = (block + 1) * width - 1; j >= this->_left[idx]; --j) addl(j); rem(idx); load(snapshot); } } private: std::vector _left, _right, _order; int _size; }; #line 2 "/home/kuhaku/atcoder/github/algo/lib/data_structure/erasable_priority_queue.hpp" /** * @brief 削除可能優先度付きキュー * * @tparam T */ template > struct erasable_priority_queue { bool empty() const { return a.empty(); } int size() const { return a.size() - b.size(); } T top() const { return a.top(); } void insert(const T &x) { a.push(x); } void insert(T &&x) { a.push(std::move(x)); } void push(const T &x) { a.push(x); } void push(T &&x) { a.push(std::move(x)); } template void emplace(Args &&...args) { a.emplace(std::forward(args)...); } void pop() { erase(a.top()); } void erase(T x) { b.emplace(x); while (!b.empty() && a.top() == b.top()) { a.pop(), b.pop(); } } private: std::priority_queue, Comp> a, b; }; #line 2 "/home/kuhaku/atcoder/github/algo/lib/math/undo_slope_trick.hpp" /** * @brief slope trick * * @tparam T */ template struct undo_slope_trick { undo_slope_trick() : min_f(), l(), r() {} T get_x() { return l.top(); } T get() { return min_f; } T get_y() { return get(); } /** * @brief Add f(x) = a * * @param a */ void add(T a) { history.emplace(0, min_f); min_f += a; } /** * @brief Add f(x) = max(0, x - a) * * @param a */ void add_f(T a) { if (!l.empty()) { history.emplace(0, min_f); min_f += std::max(T(), l.top() - a); } history.emplace(1, a); l.emplace(a); auto x = l.top(); history.emplace(2, x); l.pop(); history.emplace(3, x); r.emplace(x); } /** * @brief Add f(x) = max(0, a - x) * * @param a */ void add_g(T a) { if (!r.empty()) { history.emplace(0, min_f); min_f += std::max(T(), a - r.top()); } history.emplace(3, a); r.emplace(a); auto x = r.top(); history.emplace(4, x); r.pop(); history.emplace(1, x); l.emplace(x); } /** * @brief Add f(x) = abs(x - a) = max(0, x - a) + max(0, a - x) * * @param a */ void add_abs(T a) { add_f(a); add_g(a); } void undo() { auto [x, y] = history.top(); history.pop(); if (x == 0) min_f = y; else if (x == 1) l.erase(y); else if (x == 2) l.emplace(y); else if (x == 3) r.erase(y); else r.emplace(y); } int snapshot() const { return history.size(); } void rollback(int x = 0) { while (x < (int)history.size()) undo(); } private: T min_f; erasable_priority_queue l; erasable_priority_queue> r; std::stack> history; }; #line 3 "/home/kuhaku/atcoder/github/algo/lib/template/macro.hpp" #define FOR(i, m, n) for (int i = (m); i < int(n); ++i) #define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i) #define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i) #define rep(i, n) FOR (i, 0, n) #define repn(i, n) FOR (i, 1, n + 1) #define repr(i, n) FORR (i, n, 0) #define repnr(i, n) FORR (i, n + 1, 1) #define all(s) (s).begin(), (s).end() #line 3 "/home/kuhaku/atcoder/github/algo/lib/template/sonic.hpp" struct Sonic { Sonic() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); } constexpr void operator()() const {} } sonic; #line 5 "/home/kuhaku/atcoder/github/algo/lib/template/atcoder.hpp" using namespace std; using ll = std::int64_t; using ld = long double; template std::istream &operator>>(std::istream &is, std::pair &p) { return is >> p.first >> p.second; } template std::istream &operator>>(std::istream &is, std::vector &v) { for (T &i : v) is >> i; return is; } template std::ostream &operator<<(std::ostream &os, const std::pair &p) { return os << '(' << p.first << ',' << p.second << ')'; } template std::ostream &operator<<(std::ostream &os, const std::vector &v) { for (auto it = v.begin(); it != v.end(); ++it) { os << (it == v.begin() ? "" : " ") << *it; } return os; } template void co(Head &&head, Tail &&...tail) { if constexpr (sizeof...(tail) == 0) std::cout << head << '\n'; else std::cout << head << ' ', co(std::forward(tail)...); } template void ce(Head &&head, Tail &&...tail) { if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n'; else std::cerr << head << ' ', ce(std::forward(tail)...); } template auto make_vector(T x, int arg, Args... args) { if constexpr (sizeof...(args) == 0) return std::vector(arg, x); else return std::vector(arg, make_vector(x, args...)); } void setp(int n) { std::cout << std::fixed << std::setprecision(n); } void Yes(bool is_correct = true) { std::cout << (is_correct ? "Yes" : "No") << '\n'; } void No(bool is_not_correct = true) { Yes(!is_not_correct); } void YES(bool is_correct = true) { std::cout << (is_correct ? "YES" : "NO") << '\n'; } void NO(bool is_not_correct = true) { YES(!is_not_correct); } void Takahashi(bool is_correct = true) { std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n'; } void Aoki(bool is_not_correct = true) { Takahashi(!is_not_correct); } #line 5 "a.cpp" int main(void) { int n, q; cin >> n >> q; vector a(n); cin >> a; rollback_mo mo(n); vector ans(q); while (q--) { int l, r; cin >> l >> r; mo.add(l - 1, r); } undo_slope_trick st; auto rem = [&](int idx) { ans[idx] = st.get(); }; auto save = [&]() { return st.snapshot(); }; auto load = [&](int idx) { st.rollback(idx); }; auto add = [&](int idx) { st.add_abs(a[idx]); }; mo.solve(rem, save, load, add); for (auto x : ans) co(x); return 0; }