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
#include <algorithm>
#include <cstdio>
#include <map>
#include <set>
#include <vector>

constexpr int kMaxNumGroups = 200005;
constexpr int kMaxNumOvens = 200005;

constexpr bool kEnableLogging = false;

struct Group {
  int index;
  int number;
  long long time; // time <= 10^12

  Group(int index, int number, long long time) :
    index(index), number(number), time(time) {}

  Group() {}
};

bool operator<(const Group& a, const Group& b) {
  return a.index < b.index;
}

struct Oven {
  int index;
  int time; // time <= 10^6

  Oven(int index, long long time) : index(index), time(time) {}

  Oven() {}
};

bool operator<(const Oven& a, const Oven& b) {
  return a.time < b.time;
}

// At oven_time, group_index needs to be merged with group_index + 1.
struct GroupMergeEvent {
  int group_index;
  long long oven_time;

  GroupMergeEvent(int group_index, long long oven_time) :
    group_index(group_index), oven_time(oven_time) {}

  GroupMergeEvent() {}
};

bool operator<(const GroupMergeEvent& a, const GroupMergeEvent& b) {
  return a.oven_time < b.oven_time;
}

int num_customers, num_ovens;
std::map<int, Group> groups;
std::vector<Oven> ovens;
std::multiset<GroupMergeEvent> group_merge_events;

long long result[kMaxNumOvens];

int main() {
  scanf("%d%d", &num_customers, &num_ovens);
  // This never changes.
  long long sum_of_arrival_times = 0;
  for (int i = 0; i < num_customers; ++i) {
    Group group;
    group.index = i + 1;
    group.number = 1;
    scanf("%lld", &group.time);
    groups[group.index] = group;
    sum_of_arrival_times += group.time;
  }
  // These two values are modified during each merge.
  long long sum_of_time_times_number = sum_of_arrival_times;
  long long sum_of_number_triangles = 0;

  // Insert guard. The guard does not change the result.
  groups[0] = Group(0, 1, 0);

  for (int i = 0; i < num_ovens; ++i) {
    Oven oven;
    oven.index = i;
    scanf("%d", &oven.time);
    ovens.push_back(oven);
  }
  std::sort(ovens.begin(), ovens.end());

  for (int i = 0; i < num_customers; ++i) {
    const auto& this_group = groups[i];
    const auto& next_group = groups[i + 1];
    group_merge_events.insert(
        GroupMergeEvent(this_group.index,
                        next_group.time - this_group.time));
    if (kEnableLogging) {
      printf("Inserted merge event: %d %lld\n", this_group.index, next_group.time - this_group.time);
    }
  }

  for (const auto& oven : ovens) {
    if (kEnableLogging) {
      printf("=======\nOven: %lld\n", oven.time);
    }
    // Perform all merges.
    while (!group_merge_events.empty()) {
      const auto it = group_merge_events.begin();
      if (groups.find(it->group_index) == groups.end()) {
        // This group has already been merged.
        group_merge_events.erase(it);
        continue;
      }
      if (kEnableLogging) {
        printf("GroupMergeEvent: %d %lld\n", it->group_index, it->oven_time);
      }
      if (it->oven_time <= oven.time) {
        // Merge this group with the next group.
        // Step 1. Find this group.
        const auto this_group = groups[it->group_index];
        // Step 2. Find the next group.
        const auto next_group = groups.upper_bound(it->group_index)->second;
        if (kEnableLogging) {
          printf("next_group: %d %d %lld\n", next_group.index, next_group.number, next_group.time); 
        }
        // Merge this group and the next group.
        Group merged_group;
        merged_group.index = this_group.index;
        merged_group.number = this_group.number + next_group.number;
        merged_group.time = this_group.time;
        // Remove both groups.
        groups.erase(this_group.index);
        groups.erase(next_group.index);
        // Insert the merged group.
        groups[merged_group.index] = merged_group;
        // Erase the event.
        group_merge_events.erase(it);
        // Create the next merge event;
        const auto next_next_group_it = groups.upper_bound(merged_group.index);
        if (next_next_group_it != groups.end()) {
          const auto& next_next_group = next_next_group_it->second;
          GroupMergeEvent next_merge_event;
          next_merge_event.group_index = merged_group.index;
          next_merge_event.oven_time =
              (next_next_group.time - merged_group.time + merged_group.number - 1) / merged_group.number;
          group_merge_events.insert(next_merge_event);
        }
        // Update values.
        sum_of_number_triangles -=
            ((long long) this_group.number * (this_group.number - 1)) / 2;
        if (kEnableLogging) {
          printf("-= %lld\n", ((long long) this_group.number * (this_group.number - 1)) / 2);
        }
        sum_of_number_triangles -=
            ((long long) next_group.number * (next_group.number - 1)) / 2;
        if (kEnableLogging) {
          printf("-= %lld\n", ((long long) next_group.number * (next_group.number - 1)) / 2);
        }
        sum_of_number_triangles +=
            ((long long) merged_group.number * (merged_group.number - 1)) / 2;
        if (kEnableLogging) {
          printf("+= %lld\n", ((long long) merged_group.number * (merged_group.number - 1)) / 2);
        }

        sum_of_time_times_number -=
            (long long) this_group.time * this_group.number;
        sum_of_time_times_number -=
            (long long) next_group.time * next_group.number;
        sum_of_time_times_number +=
            (long long) merged_group.time * merged_group.number;
      } else {
        break;
      }
    }
    if (kEnableLogging) {
      printf("Groups:\n");
      for (const auto& group : groups) {
        printf("index: %d number: %d time: %lld\n", group.second.index, group.second.number, group.second.time);
      }
      printf("sum_of_time_times_number = %lld\n", sum_of_time_times_number);
      printf("sum_of_number_triangles = %lld\n", sum_of_number_triangles);
      printf("sum_of_arrival_times = %lld\n", sum_of_arrival_times);
    }
    result[oven.index] =
        sum_of_time_times_number + oven.time*sum_of_number_triangles - sum_of_arrival_times;
  }

  for (int i = 0; i < num_ovens; ++i) {
    printf("%lld\n", result[i]);
  }
  return 0;
}