#include "bits/stdc++.h" using namespace std; using LL = long long; const int N = 1e6; const int BUCKET_SIZE = 2000; const int N_BUCKETS = 2000; struct line { LL a; LL b; LL operator()(int x){ return (LL) a * x + b; } }; line raw_lines[N]; struct bucket { pair<LL, line*> envelope[BUCKET_SIZE]; int envelope_size; int j; int j_position; line* lines; int bucket_size; LL addend; void increment_j(); void rebuild_envelope(); pair<LL, line*> score(); } buckets[N_BUCKETS]; void bucket::increment_j(){ ++j; if(j_position + 1 < envelope_size && envelope[j_position + 1].first <= j) { ++j_position; } } LL div_ceil(LL x, LL y){ if(y < 0){x = -x; y = -y;} if(x >= 0) return (x+y-1)/y; return -((-x)/y); } void bucket::rebuild_envelope(){ envelope_size = 0; j_position = 0; for(int i = 0; i < bucket_size; ++i){ line l1 = lines[i]; if(l1.b == -1) continue; //cerr << "we can use line: " << i << "\n"; while(envelope_size > 0 && (*envelope[envelope_size - 1].second)(envelope[envelope_size - 1].first) <= l1(envelope[envelope_size - 1].first)) { envelope_size--; } if(envelope_size == 0 || envelope[envelope_size - 1].second->a == l1.a){ envelope[envelope_size++] = {j, lines + i}; continue; } line l0 = *envelope[envelope_size - 1].second; LL x = div_ceil(l0.b-l1.b, l1.a-l0.a); if(x > N) continue; envelope[envelope_size++] = {x, lines + i}; } } pair<LL, line*> bucket::score(){ if(envelope_size == 0) return {-2, lines + bucket_size}; return {(*envelope[j_position].second)(j) + addend, envelope[j_position].second}; } void test_bucket(){ { bucket b; line lines[3]; lines[0] = {0, 3}; lines[1] = {1, 0}; lines[2] = {2, 0}; b.lines = lines; b.bucket_size = 3; b.j = 0; b.rebuild_envelope(); assert(b.envelope_size == 2); assert(b.envelope[0] == make_pair(0LL, lines + 0)); assert(b.envelope[1] == make_pair(2LL, lines + 2)); assert(b.j == 0); assert(b.j_position == 0); b.increment_j(); assert(b.j == 1); assert(b.j_position == 0); b.increment_j(); assert(b.j == 2); assert(b.j_position == 1); } { bucket b; line lines[3]; lines[0] = {0, 5}; lines[1] = {2, 2}; lines[2] = {3, 0}; b.lines = lines; b.bucket_size = 3; b.j = 0; b.rebuild_envelope(); assert(b.envelope_size == 2); assert(b.envelope[0] == make_pair(0LL, lines + 0)); assert(b.envelope[1] == make_pair(2LL, lines + 2)); assert(b.j == 0); assert(b.j_position == 0); b.increment_j(); assert(b.j == 1); assert(b.j_position == 0); b.increment_j(); assert(b.j == 2); assert(b.j_position == 1); b.increment_j(); assert(b.j == 3); assert(b.j_position == 1); } } int main(){ // test_bucket(); int n; cin >> n; for(int i = 0; i < n; ++i){ cin >> raw_lines[i].a >> raw_lines[i].b; } sort(raw_lines, raw_lines + n, [](const line &l0, const line &l1){ if(l0.a == l1.a) return l0.b < l1.b; return l0.a < l1.a; }); //for(int i = 0; i < n; i++) cerr << raw_lines[i].a << " " << raw_lines[i].b << "\n"; int n_buckets = min(N_BUCKETS, max(1, (int) (sqrt(n) * 0.70))); for(int bucket_i = 0; bucket_i < n_buckets; ++bucket_i) { buckets[bucket_i].j = 0; int i0 = (LL) bucket_i * n / n_buckets; int i1 = (LL) (bucket_i + 1) * n / n_buckets; //cerr << "i0 = " << i0 << "\n"; //cerr << "i1 = " << i1 << "\n"; buckets[bucket_i].lines = raw_lines + i0; buckets[bucket_i].bucket_size = i1 - i0; buckets[bucket_i].addend = 0; buckets[bucket_i].rebuild_envelope(); } LL total = 0; for(int i = 0; i < n; i++){ //for(int i = 0; i < n; i++) cout << raw_lines[i].b << " "; cout << "\n"; pair<pair<LL, line*>, int> best({-1, nullptr}, n_buckets); for(int bucket_i = 0; bucket_i < n_buckets; ++bucket_i) { //cerr << "bucket_i = " << bucket_i << "\n"; //for(int j = 0; j < buckets[bucket_i].envelope_size; ++j){ //cerr << "x = " << buckets[bucket_i].envelope[j].first; //cerr << " line = " << buckets[bucket_i].envelope[j].second - raw_lines << "\n"; //} auto cand = buckets[bucket_i].score(); //cerr << "cand score = " << cand.first << "\n"; //cerr << "cand line = " << cand.second - raw_lines << "\n"; best = max(best, make_pair(cand, bucket_i)); } //cerr << "best score = " << best.first.first << "\n"; //cerr << "best line = " << best.first.second - raw_lines << "\n"; int best_bucket = best.second; for(int bucket_j = 0; bucket_j < best_bucket; ++bucket_j){ buckets[bucket_j].addend += best.first.second->a; } for(line *l = buckets[best_bucket].lines; l != best.first.second; ++l) { if(l->b != -1){ l->b += best.first.second->a; } } best.first.second->b = -1; for(line *l = best.first.second + 1; l != buckets[best_bucket].lines + buckets[best_bucket].bucket_size; ++l) { if(l->b != -1){ l->b += l->a; } } buckets[best_bucket].rebuild_envelope(); for(int bucket_j = best_bucket + 1; bucket_j < n_buckets; ++bucket_j){ buckets[bucket_j].increment_j(); } total += best.first.first; cout << total << "\n"; } }
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 | #include "bits/stdc++.h" using namespace std; using LL = long long; const int N = 1e6; const int BUCKET_SIZE = 2000; const int N_BUCKETS = 2000; struct line { LL a; LL b; LL operator()(int x){ return (LL) a * x + b; } }; line raw_lines[N]; struct bucket { pair<LL, line*> envelope[BUCKET_SIZE]; int envelope_size; int j; int j_position; line* lines; int bucket_size; LL addend; void increment_j(); void rebuild_envelope(); pair<LL, line*> score(); } buckets[N_BUCKETS]; void bucket::increment_j(){ ++j; if(j_position + 1 < envelope_size && envelope[j_position + 1].first <= j) { ++j_position; } } LL div_ceil(LL x, LL y){ if(y < 0){x = -x; y = -y;} if(x >= 0) return (x+y-1)/y; return -((-x)/y); } void bucket::rebuild_envelope(){ envelope_size = 0; j_position = 0; for(int i = 0; i < bucket_size; ++i){ line l1 = lines[i]; if(l1.b == -1) continue; //cerr << "we can use line: " << i << "\n"; while(envelope_size > 0 && (*envelope[envelope_size - 1].second)(envelope[envelope_size - 1].first) <= l1(envelope[envelope_size - 1].first)) { envelope_size--; } if(envelope_size == 0 || envelope[envelope_size - 1].second->a == l1.a){ envelope[envelope_size++] = {j, lines + i}; continue; } line l0 = *envelope[envelope_size - 1].second; LL x = div_ceil(l0.b-l1.b, l1.a-l0.a); if(x > N) continue; envelope[envelope_size++] = {x, lines + i}; } } pair<LL, line*> bucket::score(){ if(envelope_size == 0) return {-2, lines + bucket_size}; return {(*envelope[j_position].second)(j) + addend, envelope[j_position].second}; } void test_bucket(){ { bucket b; line lines[3]; lines[0] = {0, 3}; lines[1] = {1, 0}; lines[2] = {2, 0}; b.lines = lines; b.bucket_size = 3; b.j = 0; b.rebuild_envelope(); assert(b.envelope_size == 2); assert(b.envelope[0] == make_pair(0LL, lines + 0)); assert(b.envelope[1] == make_pair(2LL, lines + 2)); assert(b.j == 0); assert(b.j_position == 0); b.increment_j(); assert(b.j == 1); assert(b.j_position == 0); b.increment_j(); assert(b.j == 2); assert(b.j_position == 1); } { bucket b; line lines[3]; lines[0] = {0, 5}; lines[1] = {2, 2}; lines[2] = {3, 0}; b.lines = lines; b.bucket_size = 3; b.j = 0; b.rebuild_envelope(); assert(b.envelope_size == 2); assert(b.envelope[0] == make_pair(0LL, lines + 0)); assert(b.envelope[1] == make_pair(2LL, lines + 2)); assert(b.j == 0); assert(b.j_position == 0); b.increment_j(); assert(b.j == 1); assert(b.j_position == 0); b.increment_j(); assert(b.j == 2); assert(b.j_position == 1); b.increment_j(); assert(b.j == 3); assert(b.j_position == 1); } } int main(){ // test_bucket(); int n; cin >> n; for(int i = 0; i < n; ++i){ cin >> raw_lines[i].a >> raw_lines[i].b; } sort(raw_lines, raw_lines + n, [](const line &l0, const line &l1){ if(l0.a == l1.a) return l0.b < l1.b; return l0.a < l1.a; }); //for(int i = 0; i < n; i++) cerr << raw_lines[i].a << " " << raw_lines[i].b << "\n"; int n_buckets = min(N_BUCKETS, max(1, (int) (sqrt(n) * 0.70))); for(int bucket_i = 0; bucket_i < n_buckets; ++bucket_i) { buckets[bucket_i].j = 0; int i0 = (LL) bucket_i * n / n_buckets; int i1 = (LL) (bucket_i + 1) * n / n_buckets; //cerr << "i0 = " << i0 << "\n"; //cerr << "i1 = " << i1 << "\n"; buckets[bucket_i].lines = raw_lines + i0; buckets[bucket_i].bucket_size = i1 - i0; buckets[bucket_i].addend = 0; buckets[bucket_i].rebuild_envelope(); } LL total = 0; for(int i = 0; i < n; i++){ //for(int i = 0; i < n; i++) cout << raw_lines[i].b << " "; cout << "\n"; pair<pair<LL, line*>, int> best({-1, nullptr}, n_buckets); for(int bucket_i = 0; bucket_i < n_buckets; ++bucket_i) { //cerr << "bucket_i = " << bucket_i << "\n"; //for(int j = 0; j < buckets[bucket_i].envelope_size; ++j){ //cerr << "x = " << buckets[bucket_i].envelope[j].first; //cerr << " line = " << buckets[bucket_i].envelope[j].second - raw_lines << "\n"; //} auto cand = buckets[bucket_i].score(); //cerr << "cand score = " << cand.first << "\n"; //cerr << "cand line = " << cand.second - raw_lines << "\n"; best = max(best, make_pair(cand, bucket_i)); } //cerr << "best score = " << best.first.first << "\n"; //cerr << "best line = " << best.first.second - raw_lines << "\n"; int best_bucket = best.second; for(int bucket_j = 0; bucket_j < best_bucket; ++bucket_j){ buckets[bucket_j].addend += best.first.second->a; } for(line *l = buckets[best_bucket].lines; l != best.first.second; ++l) { if(l->b != -1){ l->b += best.first.second->a; } } best.first.second->b = -1; for(line *l = best.first.second + 1; l != buckets[best_bucket].lines + buckets[best_bucket].bucket_size; ++l) { if(l->b != -1){ l->b += l->a; } } buckets[best_bucket].rebuild_envelope(); for(int bucket_j = best_bucket + 1; bucket_j < n_buckets; ++bucket_j){ buckets[bucket_j].increment_j(); } total += best.first.first; cout << total << "\n"; } } |