#include "bits/stdc++.h" using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " using ll = long long; using ld = long double; constexpr ll Pot(ll a, int pot) { return (pot == 0) ? 1ll : a * Pot(a, pot - 1); } constexpr ll MaxN = 2000 * 1000 + 105; constexpr ll MaxD = 2000 * 1000 + 105; constexpr ll MaxT = Pot(10, 13) + 105; struct Block { ll min_t; ll sum_t; ll r; Block *left, *right; Block() : left(nullptr), right(nullptr) {} Block(ll t) : min_t(t), sum_t(t), r(1ll), left(nullptr), right(nullptr) {} }; debug& operator<<(debug& deb, const Block& b) { return deb << "Block{min_t = " << b.min_t << ", sum_t = " << b.sum_t << ", r = " << b.r << "}"; } struct Event { ll d; Block *left, *right; }; debug& operator<<(debug& deb, const Event& e) { return deb << "Event{d = " << e.d << ", left = " << *e.left << ", right = " << *e.right << "}"; } struct EventCmp { bool operator()(const Event& a, const Event& b) { if (a.d != b.d) return a.d < b.d; if (a.left != b.left) return less_(a.left, b.left); return less_(a.right, b.right); } less<Block*> less_; }; set<Event, EventCmp> events; ll sum_cost_a = 0; ll sum_cost_b = 0; void AddBlock(Block* b, int weight) { const ll cost_a = (b->r * (ll) (b->r - 1)) / 2; const ll cost_b = b->min_t * b->r - b->sum_t; sum_cost_a += cost_a * weight; sum_cost_b += cost_b * weight; } void Link(Block* l, Block* r) { l->right = r; r->left = l; assert(l->min_t <= r->min_t); // l->min_t - d + l->r * d >= r->min_t - d // l->min_t - r->min_t + l->r * d >= 0 // l->r * d >= r->min_t - l->min_t // d >= (r->min_t - l->min_t) / l->r // d >= ceil((r->min_t - l->min_t) / l->r) // d >= floor((r->min_t - l->min_t + l->r - 1) / l->r) Event event; event.d = (r->min_t - l->min_t + l->r - 1) / l->r; event.left = l; event.right = r; events.insert(event); } void Process(const Event& event) { if (event.left->right != event.right or event.left != event.right->left) { debug() << "Process(" imie(event) ") -- immediately out"; return; } debug() << "Process(" imie(event) ")"; AddBlock(event.left, -1); AddBlock(event.right, -1); Block* new_block = new Block(); assert(event.left->min_t <= event.right->min_t); new_block->min_t = event.left->min_t; new_block->sum_t = event.left->sum_t + event.right->sum_t; new_block->r = event.left->r + event.right->r; Link(event.left->left, new_block); Link(new_block, event.right->right); AddBlock(new_block, 1); event.left->left = event.left->right = nullptr; event.right->left = event.right->right = nullptr; } Block* first_block; void Wypisz() { #ifdef LOCAL debug deb; deb << "Wypisz() {\n"; Block* block = first_block; while (block != nullptr) { deb << " " << *block << "\n"; block = block->right; } deb << "}"; #endif } int n, m; int main() { scanf("%d%d", &n, &m); Block* left_block = new Block(-MaxD * MaxN); Block* zero_block = new Block(0); Block* right_block = new Block(MaxT + MaxD * MaxN); Link(left_block, zero_block); Block* last_block = zero_block; for (int i = 1; i <= n; i++) { ll t; scanf("%lld", &t); Block* block = new Block(t); Link(last_block, block); last_block = block; } Link(last_block, right_block); first_block = left_block; Wypisz(); debug() << imie(events); map<ll, ll> query_answers; vector<ll> queries; for (int i = 1; i <= m; i++) { ll d; scanf("%lld", &d); queries.push_back(d); } vector<ll> sorted_queries = queries; sort(sorted_queries.begin(), sorted_queries.end()); sorted_queries.resize( unique(sorted_queries.begin(), sorted_queries.end()) - sorted_queries.begin()); debug() << imie(sorted_queries); for (ll d : sorted_queries) { debug() << "Next d:" << imie(d); while (true) { assert(!events.empty()); Event event = *events.begin(); if (event.d > d) { break; } events.erase(events.begin()); Process(event); //Wypisz(); //debug() << imie(events); } Wypisz(); debug() << imie(events); query_answers[d] = sum_cost_a * d + sum_cost_b; } for (ll q : queries) { printf("%lld\n", query_answers[q]); } return 0; }
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 | #include "bits/stdc++.h" using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " using ll = long long; using ld = long double; constexpr ll Pot(ll a, int pot) { return (pot == 0) ? 1ll : a * Pot(a, pot - 1); } constexpr ll MaxN = 2000 * 1000 + 105; constexpr ll MaxD = 2000 * 1000 + 105; constexpr ll MaxT = Pot(10, 13) + 105; struct Block { ll min_t; ll sum_t; ll r; Block *left, *right; Block() : left(nullptr), right(nullptr) {} Block(ll t) : min_t(t), sum_t(t), r(1ll), left(nullptr), right(nullptr) {} }; debug& operator<<(debug& deb, const Block& b) { return deb << "Block{min_t = " << b.min_t << ", sum_t = " << b.sum_t << ", r = " << b.r << "}"; } struct Event { ll d; Block *left, *right; }; debug& operator<<(debug& deb, const Event& e) { return deb << "Event{d = " << e.d << ", left = " << *e.left << ", right = " << *e.right << "}"; } struct EventCmp { bool operator()(const Event& a, const Event& b) { if (a.d != b.d) return a.d < b.d; if (a.left != b.left) return less_(a.left, b.left); return less_(a.right, b.right); } less<Block*> less_; }; set<Event, EventCmp> events; ll sum_cost_a = 0; ll sum_cost_b = 0; void AddBlock(Block* b, int weight) { const ll cost_a = (b->r * (ll) (b->r - 1)) / 2; const ll cost_b = b->min_t * b->r - b->sum_t; sum_cost_a += cost_a * weight; sum_cost_b += cost_b * weight; } void Link(Block* l, Block* r) { l->right = r; r->left = l; assert(l->min_t <= r->min_t); // l->min_t - d + l->r * d >= r->min_t - d // l->min_t - r->min_t + l->r * d >= 0 // l->r * d >= r->min_t - l->min_t // d >= (r->min_t - l->min_t) / l->r // d >= ceil((r->min_t - l->min_t) / l->r) // d >= floor((r->min_t - l->min_t + l->r - 1) / l->r) Event event; event.d = (r->min_t - l->min_t + l->r - 1) / l->r; event.left = l; event.right = r; events.insert(event); } void Process(const Event& event) { if (event.left->right != event.right or event.left != event.right->left) { debug() << "Process(" imie(event) ") -- immediately out"; return; } debug() << "Process(" imie(event) ")"; AddBlock(event.left, -1); AddBlock(event.right, -1); Block* new_block = new Block(); assert(event.left->min_t <= event.right->min_t); new_block->min_t = event.left->min_t; new_block->sum_t = event.left->sum_t + event.right->sum_t; new_block->r = event.left->r + event.right->r; Link(event.left->left, new_block); Link(new_block, event.right->right); AddBlock(new_block, 1); event.left->left = event.left->right = nullptr; event.right->left = event.right->right = nullptr; } Block* first_block; void Wypisz() { #ifdef LOCAL debug deb; deb << "Wypisz() {\n"; Block* block = first_block; while (block != nullptr) { deb << " " << *block << "\n"; block = block->right; } deb << "}"; #endif } int n, m; int main() { scanf("%d%d", &n, &m); Block* left_block = new Block(-MaxD * MaxN); Block* zero_block = new Block(0); Block* right_block = new Block(MaxT + MaxD * MaxN); Link(left_block, zero_block); Block* last_block = zero_block; for (int i = 1; i <= n; i++) { ll t; scanf("%lld", &t); Block* block = new Block(t); Link(last_block, block); last_block = block; } Link(last_block, right_block); first_block = left_block; Wypisz(); debug() << imie(events); map<ll, ll> query_answers; vector<ll> queries; for (int i = 1; i <= m; i++) { ll d; scanf("%lld", &d); queries.push_back(d); } vector<ll> sorted_queries = queries; sort(sorted_queries.begin(), sorted_queries.end()); sorted_queries.resize( unique(sorted_queries.begin(), sorted_queries.end()) - sorted_queries.begin()); debug() << imie(sorted_queries); for (ll d : sorted_queries) { debug() << "Next d:" << imie(d); while (true) { assert(!events.empty()); Event event = *events.begin(); if (event.d > d) { break; } events.erase(events.begin()); Process(event); //Wypisz(); //debug() << imie(events); } Wypisz(); debug() << imie(events); query_answers[d] = sum_cost_a * d + sum_cost_b; } for (ll q : queries) { printf("%lld\n", query_answers[q]); } return 0; } |