#define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <queue> #include <map> using namespace std; #define ll long long #define mp make_pair ll SumFromOneToN(ll n) { return n * (n + 1) / 2; } class Grouping { public: ll discount; ll penaltyCount; ll leftBound; Grouping(ll leftBound) : penaltyCount(0), discount(0) { this->leftBound = leftBound; } void Combine(Grouping g, ll& discountDelta, ll& penaltyMultiplierDetla) { ll newPenaltyCount = this->penaltyCount + g.penaltyCount + 1; discountDelta = (g.penaltyCount + 1) * (g.leftBound - this->leftBound); penaltyMultiplierDetla = SumFromOneToN(newPenaltyCount) - SumFromOneToN(this->penaltyCount) - SumFromOneToN(g.penaltyCount); this->discount += g.discount + discountDelta; this->penaltyCount = newPenaltyCount; } }; class ListItem { public: Grouping g; ListItem* next; ListItem(Grouping g) : g(g) { next = nullptr; } }; class Gap { public: ll round; ListItem* left; ListItem* right; Gap(ll round, ListItem* left, ListItem* right) { this->round = round; this->left = left; this->right = right; } bool Valid() { return left->next == right; } }; class GapCompare { public: bool operator() (Gap* a, Gap* b) { return a->round > b->round; } }; int main() { int n, m; scanf("%d %d", &n, &m); ListItem* li = new ListItem(Grouping(0)); ListItem* groupsHead(li); ListItem* groupsTail(li); priority_queue<Gap*, vector<Gap*>, GapCompare> gapQueue; ll prevT = 0; for (int i = 0; i < n; i++) { ll t; scanf("%lld", &t); ListItem* newTail = new ListItem(Grouping(t)); gapQueue.push(new Gap(t - prevT, groupsTail, newTail)); groupsTail->next = newTail; groupsTail = newTail; prevT = t; } map<ll, pair<ll, ll>> resultMap; resultMap[1] = mp(0, 0); ll penaltyCount = 0; ll discount = 0; while (!gapQueue.empty()) { Gap* currentGap = gapQueue.top(); gapQueue.pop(); if (currentGap->Valid()) { ll discountDelta; ll penaltyCountDelta; currentGap->left->g.Combine(currentGap->right->g, discountDelta, penaltyCountDelta); currentGap->left->next = currentGap->right->next; currentGap->right->next = nullptr; discount += discountDelta; penaltyCount += penaltyCountDelta; resultMap[-currentGap->round] = mp(penaltyCount, discount); if (currentGap->left->next) { ll speed = 1 + currentGap->left->g.penaltyCount; ll distance = currentGap->left->next->g.leftBound - currentGap->left->g.leftBound; ll round = (distance + speed - 1) / speed; gapQueue.push(new Gap(round, currentGap->left, currentGap->left->next)); } } } for (int i = 0; i < m; i++) { ll d; scanf("%lld", &d); auto iter = resultMap.lower_bound(-d); ll multiplier = iter->second.first; ll discount = iter->second.second; printf("%lld\n", multiplier*d - discount); } }
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 | #define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <queue> #include <map> using namespace std; #define ll long long #define mp make_pair ll SumFromOneToN(ll n) { return n * (n + 1) / 2; } class Grouping { public: ll discount; ll penaltyCount; ll leftBound; Grouping(ll leftBound) : penaltyCount(0), discount(0) { this->leftBound = leftBound; } void Combine(Grouping g, ll& discountDelta, ll& penaltyMultiplierDetla) { ll newPenaltyCount = this->penaltyCount + g.penaltyCount + 1; discountDelta = (g.penaltyCount + 1) * (g.leftBound - this->leftBound); penaltyMultiplierDetla = SumFromOneToN(newPenaltyCount) - SumFromOneToN(this->penaltyCount) - SumFromOneToN(g.penaltyCount); this->discount += g.discount + discountDelta; this->penaltyCount = newPenaltyCount; } }; class ListItem { public: Grouping g; ListItem* next; ListItem(Grouping g) : g(g) { next = nullptr; } }; class Gap { public: ll round; ListItem* left; ListItem* right; Gap(ll round, ListItem* left, ListItem* right) { this->round = round; this->left = left; this->right = right; } bool Valid() { return left->next == right; } }; class GapCompare { public: bool operator() (Gap* a, Gap* b) { return a->round > b->round; } }; int main() { int n, m; scanf("%d %d", &n, &m); ListItem* li = new ListItem(Grouping(0)); ListItem* groupsHead(li); ListItem* groupsTail(li); priority_queue<Gap*, vector<Gap*>, GapCompare> gapQueue; ll prevT = 0; for (int i = 0; i < n; i++) { ll t; scanf("%lld", &t); ListItem* newTail = new ListItem(Grouping(t)); gapQueue.push(new Gap(t - prevT, groupsTail, newTail)); groupsTail->next = newTail; groupsTail = newTail; prevT = t; } map<ll, pair<ll, ll>> resultMap; resultMap[1] = mp(0, 0); ll penaltyCount = 0; ll discount = 0; while (!gapQueue.empty()) { Gap* currentGap = gapQueue.top(); gapQueue.pop(); if (currentGap->Valid()) { ll discountDelta; ll penaltyCountDelta; currentGap->left->g.Combine(currentGap->right->g, discountDelta, penaltyCountDelta); currentGap->left->next = currentGap->right->next; currentGap->right->next = nullptr; discount += discountDelta; penaltyCount += penaltyCountDelta; resultMap[-currentGap->round] = mp(penaltyCount, discount); if (currentGap->left->next) { ll speed = 1 + currentGap->left->g.penaltyCount; ll distance = currentGap->left->next->g.leftBound - currentGap->left->g.leftBound; ll round = (distance + speed - 1) / speed; gapQueue.push(new Gap(round, currentGap->left, currentGap->left->next)); } } } for (int i = 0; i < m; i++) { ll d; scanf("%lld", &d); auto iter = resultMap.lower_bound(-d); ll multiplier = iter->second.first; ll discount = iter->second.second; printf("%lld\n", multiplier*d - discount); } } |