#include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; int min (int a, int b) { return a > b ? b : a; } int max (int a, int b) { return a > b ? a : b; } typedef struct interval { int mi, ma; int analized; long long tmp_multiply; long long tmp_minus; long long sum_multiply; long long sum_minus; } interval; typedef struct stove { long long time; int id; } stove; bool compare_by_time(const stove &a, const stove &b) { return a.time < b.time; } int find_bigger_mm(const vector<stove> stoves, int mi, int ma, long long y, long long multiply, long long minus); int main() { std::ios_base::sync_with_stdio(false); long long n, m, tmp; cin >> n; cin >> m; vector<long long> times(n+1, -1); times[0] = 0; for (int i = 1; i <= n; i++) { cin >> tmp; times[i] = tmp; } vector<long long> answers(m, -1); vector<stove> stoves; stoves.resize(m); for (int i = 0; i < m; i++) { cin >> tmp; stoves[i].time = tmp; stoves[i].id = i; } sort(stoves.begin(), stoves.end(), compare_by_time); vector<interval> tasks; interval zero_task; zero_task.mi = 0; zero_task.ma = m-1; zero_task.analized = 0; zero_task.tmp_multiply = 0; zero_task.tmp_minus = 0; zero_task.sum_multiply = 0; zero_task.sum_minus = 0; tasks.push_back(zero_task); while(!tasks.empty()) { interval t(tasks.back()); tasks.pop_back(); if (t.analized == n) { t.sum_multiply += t.tmp_multiply; t.sum_minus += t.tmp_minus; t.tmp_multiply = 0; t.tmp_minus = 0; for(int i = t.mi; i <= t.ma; i++) { answers[stoves[i].id] = stoves[i].time*t.sum_multiply - t.sum_minus; } } else { long long y = times[t.analized+1] - times[t.analized]; long long space_max = stoves[t.ma].time*(t.tmp_multiply+1) - t.tmp_minus; long long space_min = stoves[t.mi].time*(t.tmp_multiply+1) - t.tmp_minus; if (y >= space_max) { t.analized++; t.sum_multiply += t.tmp_multiply; t.sum_minus += t.tmp_minus; t.tmp_multiply = 0; t.tmp_minus = 0; tasks.push_back(t); } else if (y < space_min) { t.analized++; t.tmp_multiply++; t.tmp_minus += y; tasks.push_back(t); } else { int bigger = find_bigger_mm(stoves, t.mi, t.ma, y, t.tmp_multiply, t.tmp_minus); interval t1(t), t2(t); t1.ma = bigger-1; t2.mi = bigger; tasks.push_back(t1); tasks.push_back(t2); } } } for (int i = 0; i < m; i++) { cout << answers[i] << endl; } return 0; } int find_bigger_mm(const vector<stove> stoves, int mi, int ma, long long y, long long multiply, long long minus) { int mid = (mi+ma)/2; int space_mid = stoves[mid].time*(multiply+1)-minus; if (ma-mi <= 1) { return ma; } if (y < space_mid) { return find_bigger_mm(stoves, mi, mid, y, multiply, minus); } else { return find_bigger_mm(stoves, mid, ma, y, multiply, minus); } }
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 | #include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; int min (int a, int b) { return a > b ? b : a; } int max (int a, int b) { return a > b ? a : b; } typedef struct interval { int mi, ma; int analized; long long tmp_multiply; long long tmp_minus; long long sum_multiply; long long sum_minus; } interval; typedef struct stove { long long time; int id; } stove; bool compare_by_time(const stove &a, const stove &b) { return a.time < b.time; } int find_bigger_mm(const vector<stove> stoves, int mi, int ma, long long y, long long multiply, long long minus); int main() { std::ios_base::sync_with_stdio(false); long long n, m, tmp; cin >> n; cin >> m; vector<long long> times(n+1, -1); times[0] = 0; for (int i = 1; i <= n; i++) { cin >> tmp; times[i] = tmp; } vector<long long> answers(m, -1); vector<stove> stoves; stoves.resize(m); for (int i = 0; i < m; i++) { cin >> tmp; stoves[i].time = tmp; stoves[i].id = i; } sort(stoves.begin(), stoves.end(), compare_by_time); vector<interval> tasks; interval zero_task; zero_task.mi = 0; zero_task.ma = m-1; zero_task.analized = 0; zero_task.tmp_multiply = 0; zero_task.tmp_minus = 0; zero_task.sum_multiply = 0; zero_task.sum_minus = 0; tasks.push_back(zero_task); while(!tasks.empty()) { interval t(tasks.back()); tasks.pop_back(); if (t.analized == n) { t.sum_multiply += t.tmp_multiply; t.sum_minus += t.tmp_minus; t.tmp_multiply = 0; t.tmp_minus = 0; for(int i = t.mi; i <= t.ma; i++) { answers[stoves[i].id] = stoves[i].time*t.sum_multiply - t.sum_minus; } } else { long long y = times[t.analized+1] - times[t.analized]; long long space_max = stoves[t.ma].time*(t.tmp_multiply+1) - t.tmp_minus; long long space_min = stoves[t.mi].time*(t.tmp_multiply+1) - t.tmp_minus; if (y >= space_max) { t.analized++; t.sum_multiply += t.tmp_multiply; t.sum_minus += t.tmp_minus; t.tmp_multiply = 0; t.tmp_minus = 0; tasks.push_back(t); } else if (y < space_min) { t.analized++; t.tmp_multiply++; t.tmp_minus += y; tasks.push_back(t); } else { int bigger = find_bigger_mm(stoves, t.mi, t.ma, y, t.tmp_multiply, t.tmp_minus); interval t1(t), t2(t); t1.ma = bigger-1; t2.mi = bigger; tasks.push_back(t1); tasks.push_back(t2); } } } for (int i = 0; i < m; i++) { cout << answers[i] << endl; } return 0; } int find_bigger_mm(const vector<stove> stoves, int mi, int ma, long long y, long long multiply, long long minus) { int mid = (mi+ma)/2; int space_mid = stoves[mid].time*(multiply+1)-minus; if (ma-mi <= 1) { return ma; } if (y < space_mid) { return find_bigger_mm(stoves, mi, mid, y, multiply, minus); } else { return find_bigger_mm(stoves, mid, ma, y, multiply, minus); } } |