#include <iostream> #include <cmath> #include <assert.h> #include <queue> using namespace std; struct Event { int group_id; int group_size; int time; }; struct Group { long long head; int next_group; int size; }; bool operator<(const Event& a, const Event& b) { return a.time > b.time; } long long GroupDelta(const Group& group) { return (long long) group.size * (group.size - 1) / 2; } long long GroupTail(const Group& group, int bake_time) { return group.head + (long long) (group.size - 1) * bake_time; } int MeetTime(const Group& a, const Group& b) { assert(a.head <= b.head); return min((b.head - a.head + a.size - 1) / a.size, 1100100LL); } long long GroupShift(const Group& a, const Group& b, int bake_time) { assert(a.head <= b.head); const long long tail_a = GroupTail(a, bake_time); //cout << "a.head = " << a.head << " b.head = " << b.head << endl; //cout << "bake_time = " << bake_time << " tail_a = " << tail_a << endl; return max(tail_a + bake_time - b.head, 0LL); } long long ShiftDelta(const Group& a, const Group& b, int bake_time) { return b.size * GroupShift(a, b, bake_time); } long long JoinGroups(Group& a, Group& b) { assert(a.head <= b.head); //cout << "Joining " << a.head << " and " << b.head << endl; long long delta = 0; delta -= GroupDelta(a) + GroupDelta(b); a.size += b.size; a.next_group = b.next_group; b.size = 0; b.next_group = -1; delta += GroupDelta(a); //cout << " delta = " << delta << endl; return delta; } Group groups[200100]; priority_queue<Event> events; long long wasted_time_at[1100100]; void Schedule(int group_id, int bake_time) { //cout << "Schedule, group " << group_id << " for time " << bake_time << endl; Event event; event.group_id = group_id; event.group_size = groups[group_id].size; event.time = bake_time; events.push(event); } int main() { ios::sync_with_stdio(false); int n, m; long long t; long long step_delta = 0; cin >> n >> m; groups[0].head = 0; groups[0].next_group = 1; groups[0].size = 1; for (int i = 1; i <= n; ++i) { cin >> t; groups[i].head = t; groups[i].next_group = i + 1; groups[i].size = 1; int meet_time = MeetTime(groups[i - 1], groups[i]); Schedule(i - 1, meet_time); } groups[n + 1].head = 1e18; groups[n + 1].size = 1; int bake_time = 0; long long wasted_time = 0; while (bake_time <= 1000000) { //cout << "bake_time = " << bake_time << endl; if (!events.empty() && events.top().time <= bake_time) { // Some more events at this bake time. const Event event = events.top(); events.pop(); Group& a = groups[event.group_id]; //cout << "extracted G = " << event.group_id << " head = " << a.head << " size = " << a.size << endl; //cout << "next G = " << a.next_group << endl; if (a.size != event.group_size) { //cout << "dropped... " << endl; continue; } assert(a.size > 0); Group& b = groups[a.next_group]; //cout << "next G size = " << b.size << " head = " << b.head << endl; assert(b.size > 0); wasted_time += ShiftDelta(a, b, bake_time); step_delta += JoinGroups(a, b); int meet_time = MeetTime(a, groups[a.next_group]); //cout << "meet_time = " << meet_time << endl; Schedule(event.group_id, meet_time); } else { //cout << "wasted_time_at " << bake_time << " = " << wasted_time << endl; wasted_time_at[bake_time] = wasted_time; wasted_time += step_delta; bake_time++; } } int d; for (int i = 0; i < m; ++i) { cin >> d; cout << wasted_time_at[d] << endl; } 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 | #include <iostream> #include <cmath> #include <assert.h> #include <queue> using namespace std; struct Event { int group_id; int group_size; int time; }; struct Group { long long head; int next_group; int size; }; bool operator<(const Event& a, const Event& b) { return a.time > b.time; } long long GroupDelta(const Group& group) { return (long long) group.size * (group.size - 1) / 2; } long long GroupTail(const Group& group, int bake_time) { return group.head + (long long) (group.size - 1) * bake_time; } int MeetTime(const Group& a, const Group& b) { assert(a.head <= b.head); return min((b.head - a.head + a.size - 1) / a.size, 1100100LL); } long long GroupShift(const Group& a, const Group& b, int bake_time) { assert(a.head <= b.head); const long long tail_a = GroupTail(a, bake_time); //cout << "a.head = " << a.head << " b.head = " << b.head << endl; //cout << "bake_time = " << bake_time << " tail_a = " << tail_a << endl; return max(tail_a + bake_time - b.head, 0LL); } long long ShiftDelta(const Group& a, const Group& b, int bake_time) { return b.size * GroupShift(a, b, bake_time); } long long JoinGroups(Group& a, Group& b) { assert(a.head <= b.head); //cout << "Joining " << a.head << " and " << b.head << endl; long long delta = 0; delta -= GroupDelta(a) + GroupDelta(b); a.size += b.size; a.next_group = b.next_group; b.size = 0; b.next_group = -1; delta += GroupDelta(a); //cout << " delta = " << delta << endl; return delta; } Group groups[200100]; priority_queue<Event> events; long long wasted_time_at[1100100]; void Schedule(int group_id, int bake_time) { //cout << "Schedule, group " << group_id << " for time " << bake_time << endl; Event event; event.group_id = group_id; event.group_size = groups[group_id].size; event.time = bake_time; events.push(event); } int main() { ios::sync_with_stdio(false); int n, m; long long t; long long step_delta = 0; cin >> n >> m; groups[0].head = 0; groups[0].next_group = 1; groups[0].size = 1; for (int i = 1; i <= n; ++i) { cin >> t; groups[i].head = t; groups[i].next_group = i + 1; groups[i].size = 1; int meet_time = MeetTime(groups[i - 1], groups[i]); Schedule(i - 1, meet_time); } groups[n + 1].head = 1e18; groups[n + 1].size = 1; int bake_time = 0; long long wasted_time = 0; while (bake_time <= 1000000) { //cout << "bake_time = " << bake_time << endl; if (!events.empty() && events.top().time <= bake_time) { // Some more events at this bake time. const Event event = events.top(); events.pop(); Group& a = groups[event.group_id]; //cout << "extracted G = " << event.group_id << " head = " << a.head << " size = " << a.size << endl; //cout << "next G = " << a.next_group << endl; if (a.size != event.group_size) { //cout << "dropped... " << endl; continue; } assert(a.size > 0); Group& b = groups[a.next_group]; //cout << "next G size = " << b.size << " head = " << b.head << endl; assert(b.size > 0); wasted_time += ShiftDelta(a, b, bake_time); step_delta += JoinGroups(a, b); int meet_time = MeetTime(a, groups[a.next_group]); //cout << "meet_time = " << meet_time << endl; Schedule(event.group_id, meet_time); } else { //cout << "wasted_time_at " << bake_time << " = " << wasted_time << endl; wasted_time_at[bake_time] = wasted_time; wasted_time += step_delta; bake_time++; } } int d; for (int i = 0; i < m; ++i) { cin >> d; cout << wasted_time_at[d] << endl; } return 0; } |