#include <bits/stdc++.h> using namespace std; using u64 = uint64_t; using i64 = int64_t; // print tuples {{{ template <typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1, T2>& t) { return os << '[' << t.first << ',' << t.second << ']'; } // }}} // print containers {{{ template <typename It> void print(ostream& os, It begin, It end, u64 len, u64 limit = 30) { u64 count = 0; os << "{"; while (begin != end && count < limit) { os << "(" << *begin << ")"; count++; begin++; } if (begin != end) os << "... " << len << " total"; os << "}"; } #define MAKE_PRINTER_1(container) \ template <typename T> ostream& operator<<(ostream& os, const container<T>& t) { print(os, t.begin(), t.end(), t.size()); return os; } #define MAKE_PRINTER_2(container) \ template <typename T1, typename T2> \ ostream& operator<<(ostream& os, const container<T1, T2>& t) { \ print(os, t.begin(), t.end(), t.size()); \ return os; \ } MAKE_PRINTER_1(vector) MAKE_PRINTER_2(map) MAKE_PRINTER_1(set) MAKE_PRINTER_2(unordered_map) MAKE_PRINTER_1(unordered_set) #undef MAKE_PRINTER_1 #undef MAKE_PRINTER_2 // }}} // read/write {{{ template <typename T> T read() { T e; cin >> e; return e; } void read() {} template <typename T, typename ...Ts> void read(T& v, Ts& ...ts) { v = read<T>(); read(ts...); } template <typename T> vector<T> readv(u64 n) { vector<T> v; for (u64 i = 0; i < n; i++) v.push_back(read<T>()); return v; } template <typename T> struct identity { const T& operator()(const T& t) const { return t; } }; #define PRINTERS(FNAME, OUTP) \ template <typename T> void FNAME(const T& t) { OUTP << t << ' '; } \ void FNAME##ln() { OUTP << '\n'; } \ template <typename T> void FNAME##ln(const T& t) { OUTP << t << '\n'; } \ template <typename T, typename F = identity<typename T::value_type>> \ void FNAME##v(const T& t, F f = F()) { for (const auto& e : t) FNAME(f(e)); FNAME##ln(); } PRINTERS(print, cout) #ifdef DEBUG_PRINTS PRINTERS(dprint, cerr) #else # define dprint(...) # define dprintv(...) # define dprintln(...) #endif /// }}} struct Value { // {{{ u64 v; }; // }}} class Interval { // {{{ public: Interval(Value begin, Value end) : begin_(begin), end_(end), l_(0), t_(0) { } void merge_with(const Interval& other) { assert(other.begin_.v == end_.v); l_ += other.l_ + 1u; t_ += other.t_ + (end_.v - begin_.v) * (other.l_ + 1u); end_ = other.end_; } u64 rank() const { return (end_.v - begin_.v) / (l_ + 1u); } u64 l() const { return l_ * (l_ + 1u) / 2u; } u64 t() const { return t_; } private: Value begin_, end_; u64 l_, t_; }; // }}} class Global { // {{{ public: Global() : sum_l_(0), sum_t_(0) {} void add_interval(const Interval& interv) { sum_l_ += interv.l(); sum_t_ += interv.t(); } void remove_interval(const Interval& interv) { sum_l_ -= interv.l(); sum_t_ -= interv.t(); } u64 calc_k(u64 d) { assert(d * sum_l_ >= sum_t_); return d * sum_l_ - sum_t_; } private: u64 sum_l_, sum_t_; }; // }}} class IntervalQueue { // {{{ public: IntervalQueue(u64 number_limit) : is_invalidated_(number_limit, false) {} void add(u64 rank, u64 number) { queue_.insert(make_pair(rank, number)); } void remove(u64 number) { is_invalidated_[number] = true; } bool has_next(u64 d) { flush(); return (!queue_.empty() && queue_.begin()->first < d); } u64 next(u64 d) { assert(has_next(d)); flush(); auto v = queue_.begin()->second; queue_.erase(queue_.begin()); return v; } private: void flush() { while (!queue_.empty() && is_invalidated_[queue_.begin()->second]) queue_.erase(queue_.begin()); } set<pair<u64, u64>> queue_; vector<bool> is_invalidated_; }; // }}} class Structures { // {{{ public: Structures(const vector<u64>& ts) : global_(), intervals_(), interval_queue_(ts.size()) { u64 prev = 0; for (auto t : ts) { intervals_.emplace_back(Value{prev}, Value{t}); prev = t; } for (u64 i = 0; i < intervals_.size(); i++) { interval_queue_.add(intervals_[i].rank(), i); nexts_.push_back(i + 1); } } u64 calc_k_for_d(u64 d) { while (interval_queue_.has_next(d)) { const u64 interv_num = interval_queue_.next(d); const u64 interv_next = nexts_[interv_num]; nexts_[interv_num] = nexts_[interv_next]; global_.remove_interval(intervals_[interv_num]); global_.remove_interval(intervals_[interv_next]); interval_queue_.remove(interv_next); intervals_[interv_num].merge_with(intervals_[interv_next]); interval_queue_.add(intervals_[interv_num].rank(), interv_num); global_.add_interval(intervals_[interv_num]); } return global_.calc_k(d); } private: Global global_; vector<Interval> intervals_; vector<u64> nexts_; IntervalQueue interval_queue_; }; // }}} void go() { // {{{ u64 n, m; read(n, m); auto t = readv<u64>(n); t.push_back(std::numeric_limits<u64>::max()); Structures structs(t); using DsItem = pair<u64, pair<u64, u64>>; vector<DsItem> ds; for (u64 i = 0; i < m; i++) { ds.emplace_back(read<u64>(), make_pair(i, 0)); } sort(ds.begin(), ds.end()); for (auto& d : ds) d.second.second = structs.calc_k_for_d(d.first); sort(ds.begin(), ds.end(), [](const DsItem& a, const DsItem& b) -> bool { return a.second < b.second; }); for (const auto& d : ds) println(d.second.second); } // }}} int main () { // {{{ ios_base::sync_with_stdio(0); cin.tie(0); go(); } //
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | #include <bits/stdc++.h> using namespace std; using u64 = uint64_t; using i64 = int64_t; // print tuples {{{ template <typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1, T2>& t) { return os << '[' << t.first << ',' << t.second << ']'; } // }}} // print containers {{{ template <typename It> void print(ostream& os, It begin, It end, u64 len, u64 limit = 30) { u64 count = 0; os << "{"; while (begin != end && count < limit) { os << "(" << *begin << ")"; count++; begin++; } if (begin != end) os << "... " << len << " total"; os << "}"; } #define MAKE_PRINTER_1(container) \ template <typename T> ostream& operator<<(ostream& os, const container<T>& t) { print(os, t.begin(), t.end(), t.size()); return os; } #define MAKE_PRINTER_2(container) \ template <typename T1, typename T2> \ ostream& operator<<(ostream& os, const container<T1, T2>& t) { \ print(os, t.begin(), t.end(), t.size()); \ return os; \ } MAKE_PRINTER_1(vector) MAKE_PRINTER_2(map) MAKE_PRINTER_1(set) MAKE_PRINTER_2(unordered_map) MAKE_PRINTER_1(unordered_set) #undef MAKE_PRINTER_1 #undef MAKE_PRINTER_2 // }}} // read/write {{{ template <typename T> T read() { T e; cin >> e; return e; } void read() {} template <typename T, typename ...Ts> void read(T& v, Ts& ...ts) { v = read<T>(); read(ts...); } template <typename T> vector<T> readv(u64 n) { vector<T> v; for (u64 i = 0; i < n; i++) v.push_back(read<T>()); return v; } template <typename T> struct identity { const T& operator()(const T& t) const { return t; } }; #define PRINTERS(FNAME, OUTP) \ template <typename T> void FNAME(const T& t) { OUTP << t << ' '; } \ void FNAME##ln() { OUTP << '\n'; } \ template <typename T> void FNAME##ln(const T& t) { OUTP << t << '\n'; } \ template <typename T, typename F = identity<typename T::value_type>> \ void FNAME##v(const T& t, F f = F()) { for (const auto& e : t) FNAME(f(e)); FNAME##ln(); } PRINTERS(print, cout) #ifdef DEBUG_PRINTS PRINTERS(dprint, cerr) #else # define dprint(...) # define dprintv(...) # define dprintln(...) #endif /// }}} struct Value { // {{{ u64 v; }; // }}} class Interval { // {{{ public: Interval(Value begin, Value end) : begin_(begin), end_(end), l_(0), t_(0) { } void merge_with(const Interval& other) { assert(other.begin_.v == end_.v); l_ += other.l_ + 1u; t_ += other.t_ + (end_.v - begin_.v) * (other.l_ + 1u); end_ = other.end_; } u64 rank() const { return (end_.v - begin_.v) / (l_ + 1u); } u64 l() const { return l_ * (l_ + 1u) / 2u; } u64 t() const { return t_; } private: Value begin_, end_; u64 l_, t_; }; // }}} class Global { // {{{ public: Global() : sum_l_(0), sum_t_(0) {} void add_interval(const Interval& interv) { sum_l_ += interv.l(); sum_t_ += interv.t(); } void remove_interval(const Interval& interv) { sum_l_ -= interv.l(); sum_t_ -= interv.t(); } u64 calc_k(u64 d) { assert(d * sum_l_ >= sum_t_); return d * sum_l_ - sum_t_; } private: u64 sum_l_, sum_t_; }; // }}} class IntervalQueue { // {{{ public: IntervalQueue(u64 number_limit) : is_invalidated_(number_limit, false) {} void add(u64 rank, u64 number) { queue_.insert(make_pair(rank, number)); } void remove(u64 number) { is_invalidated_[number] = true; } bool has_next(u64 d) { flush(); return (!queue_.empty() && queue_.begin()->first < d); } u64 next(u64 d) { assert(has_next(d)); flush(); auto v = queue_.begin()->second; queue_.erase(queue_.begin()); return v; } private: void flush() { while (!queue_.empty() && is_invalidated_[queue_.begin()->second]) queue_.erase(queue_.begin()); } set<pair<u64, u64>> queue_; vector<bool> is_invalidated_; }; // }}} class Structures { // {{{ public: Structures(const vector<u64>& ts) : global_(), intervals_(), interval_queue_(ts.size()) { u64 prev = 0; for (auto t : ts) { intervals_.emplace_back(Value{prev}, Value{t}); prev = t; } for (u64 i = 0; i < intervals_.size(); i++) { interval_queue_.add(intervals_[i].rank(), i); nexts_.push_back(i + 1); } } u64 calc_k_for_d(u64 d) { while (interval_queue_.has_next(d)) { const u64 interv_num = interval_queue_.next(d); const u64 interv_next = nexts_[interv_num]; nexts_[interv_num] = nexts_[interv_next]; global_.remove_interval(intervals_[interv_num]); global_.remove_interval(intervals_[interv_next]); interval_queue_.remove(interv_next); intervals_[interv_num].merge_with(intervals_[interv_next]); interval_queue_.add(intervals_[interv_num].rank(), interv_num); global_.add_interval(intervals_[interv_num]); } return global_.calc_k(d); } private: Global global_; vector<Interval> intervals_; vector<u64> nexts_; IntervalQueue interval_queue_; }; // }}} void go() { // {{{ u64 n, m; read(n, m); auto t = readv<u64>(n); t.push_back(std::numeric_limits<u64>::max()); Structures structs(t); using DsItem = pair<u64, pair<u64, u64>>; vector<DsItem> ds; for (u64 i = 0; i < m; i++) { ds.emplace_back(read<u64>(), make_pair(i, 0)); } sort(ds.begin(), ds.end()); for (auto& d : ds) d.second.second = structs.calc_k_for_d(d.first); sort(ds.begin(), ds.end(), [](const DsItem& a, const DsItem& b) -> bool { return a.second < b.second; }); for (const auto& d : ds) println(d.second.second); } // }}} int main () { // {{{ ios_base::sync_with_stdio(0); cin.tie(0); go(); } // |