#include <algorithm> #include <cstdio> #include <cstdlib> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> #include <cassert> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second inline list<int>::iterator inc(list<int>::iterator it) { return ++it; } inline list<int>::iterator dcr(list<int>::iterator it) { return --it; } const int MAXN = 1000005; int N; pair<LL, LL> T[MAXN]; list<int> hull; struct Node { int first, last; LL first_value, last_value, first_by, last_by; LL add, mul; } _data[1<<21]; int M; void update(int i) { const Node& copy_first_from = (_data[2*i].first < _data[2*i+1].first) ? _data[2*i] : _data[2*i+1]; _data[i].first = copy_first_from.first; _data[i].first_value = copy_first_from.first_value; _data[i].first_by = copy_first_from.first_by; const Node& copy_last_from = (_data[2*i].last > _data[2*i+1].last) ? _data[2*i] : _data[2*i+1]; _data[i].last = copy_last_from.last; _data[i].last_value = copy_last_from.last_value; _data[i].last_by = copy_last_from.last_by; } void push(int v) { if (!_data[v].add && !_data[v].mul) return; _data[2*v].add += _data[v].add; _data[2*v+1].add += _data[v].add; _data[2*v].mul += _data[v].mul; _data[2*v+1].mul += _data[v].mul; REP(j,2) { _data[2*v+j].first_value += _data[v].add + _data[v].mul * _data[2*v+j].first_by; _data[2*v+j].last_value += _data[v].add + _data[v].mul * _data[2*v+j].last_by; } _data[v].add = _data[v].mul = 0; } void init() { M = 1; while (M < N) M <<= 1; REP(i,2*M) _data[i].first = M, _data[i].last = -1; REP(i,N) { _data[M+i].first_value = _data[M+i].last_value = T[i].nd; _data[M+i].first_by = _data[M+i].last_by = T[i].st; } } int CACHE_BREAKER = 1; void add(int A, int B, LL val, int v = 1, int L = 0, int R = M - 1) { if (B < L || R < A) return; if (A <= L && R <= B) { _data[v].add += val; _data[v].first_value += val; _data[v].last_value += val; return; } add(A, B, val, 2*v, L, (L+R)/2); add(A, B, val, 2*v+1, (L+R)/2+1, R); update(v); } void mul(int A, int B, LL val, int v = 1, int L = 0, int R = M - 1) { if (B < L || R < A) return; if (A <= L && R <= B) { _data[v].mul += val; _data[v].first_value += val * _data[v].first_by; _data[v].last_value += val * _data[v].last_by; return; } mul(A, B, val, 2*v, L, (L+R)/2); mul(A, B, val, 2*v+1, (L+R)/2+1, R); update(v); } void set_flag(int which, bool val, int v = 1, int L = 0, int R = M - 1) { if (L == R) { if (val) _data[v].first = _data[v].last = which; else _data[v].first = M, _data[v].last = -1; return; } push(v); if (which <= (L+R)/2) set_flag(which, val, 2*v, L, (L+R)/2); else set_flag(which, val, 2*v+1, (L+R)/2+1, R); update(v); } void remove_from_hull(int v) { set_flag(v, false); } void add_to_hull(int v) { set_flag(v, true); } LL _cache[MAXN]; int _cachever[MAXN]; LL get_value(int which) { if (_cachever[which] == CACHE_BREAKER) return _cache[which]; _cachever[which] = CACHE_BREAKER; int v = which + M; LL res = _data[v].first_value, mul = 0; while (v > 1) { v /= 2; res += _data[v].add; mul += _data[v].mul; } return _cache[which] = res + mul * T[which].st; } int find_best(int v = 1, int L = 0, int R = M - 1) { if (L == R) return L; push(v); bool use_left; if (_data[2*v].last == -1) { use_left = false; } else if (_data[2*v+1].first == M) { use_left = true; } else { use_left = _data[2*v].last_value > _data[2*v+1].first_value; } return use_left ? find_best(2*v, L, (L+R)/2) : find_best(2*v+1, (L+R)/2+1, R); } inline LL updated_value(int i) { return get_value(i); } bool ccw(int aa, int bb, int cc) { LL det = (T[aa].st-T[bb].st)*(get_value(bb)-get_value(cc))-(get_value(aa)-get_value(bb))*(T[bb].st-T[cc].st); //Dla T=int -> uwzględnić rozmiar (LL)! return (det > 0); }; vector<int> candidates[MAXN]; bool used[MAXN]; list<int>::iterator ptr[MAXN]; #define SECOND_LAST(l) (*dcr(dcr(l.end()))) void compute_hull() { REP(i,N) { while (hull.size() > 1 && !ccw(i, *dcr(hull.end()), SECOND_LAST(hull))) { candidates[i].pb(*dcr(hull.end())); candidates[SECOND_LAST(hull)].pb(*dcr(hull.end())); hull.erase(dcr(hull.end())); } hull.push_back(i); ptr[i] = dcr(hull.end()); } } LL pop() { int k = find_best(); used[k] = true; LL result = updated_value(k); remove_from_hull(k); add(0,k-1,T[k].st); mul(k,N-1,1); ++CACHE_BREAKER; list<int>::iterator best = ptr[k]; list<int>::iterator pos = inc(best); hull.erase(best); if (hull.empty()) { return result; } int a, b; if (pos == hull.end()) { a = *dcr(pos) + 1; b = N; } else if (pos == hull.begin()) { a = 0; b = *pos; } else { a = *dcr(pos) + 1; b = *pos; } sort(candidates[k].begin(), candidates[k].end()); candidates[k].erase(unique(candidates[k].begin(), candidates[k].end()), candidates[k].end()); set<int> to_add, to_remove; for (vector<int>::iterator it = candidates[k].begin(); it != candidates[k].end(); ++it) { int i = *it; if (i < a || i >= b || used[i]) continue; pos = ptr[i] = hull.insert(pos, i); to_add.insert(i); if (pos == hull.begin()) { ++pos; continue; } while (true) { list<int>::iterator prev = dcr(pos); if (prev == hull.begin() || ccw(i, *prev, *dcr(prev))) break; candidates[i].pb(*prev); candidates[*dcr(prev)].pb(*prev); if (to_add.find(*prev) == to_add.end()) to_remove.insert(*prev); else to_add.erase(*prev); hull.erase(prev); } ++pos; } int old_visited = 0; while (pos != hull.end()) { ++old_visited; bool removed_any = false; while (pos != hull.begin() && dcr(pos) != hull.begin() && !ccw(*pos, *dcr(pos), *dcr(dcr(pos)))) { candidates[*pos].pb(*dcr(pos)); candidates[*dcr(dcr(pos))].pb(*dcr(pos)); if (to_add.find(*dcr(pos)) == to_add.end()) to_remove.insert(*dcr(pos)); else to_add.erase(*dcr(pos)); hull.erase(dcr(pos)); removed_any = true; } if (old_visited >= 2 && !removed_any) break; } for (auto& a: to_remove) remove_from_hull(a); for (auto& a: to_add) add_to_hull(a); return result; } LL DP[1000000]; int main() { ios_base::sync_with_stdio(0); scanf("%d", &N); REP(i,N) scanf("%lld%lld", &T[i].st, &T[i].nd); sort(T, T+N); if (N <= 8000) { REP(i,N) FORD(j,N,0) DP[j] = max(DP[j], (j == 0 ? 0 : DP[j-1]) + T[i].nd + j * T[i].st); REP(i,N) printf("%lld\n", DP[i]); return 0; } init(); compute_hull(); for (auto& v: hull) _data[M+v].first = _data[M+v].last = v; FORD(i,M,1) update(i); LL sum = 0; REP(i,N) { sum += pop(); printf("%lld\n", sum); } }
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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | #include <algorithm> #include <cstdio> #include <cstdlib> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> #include <cassert> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second inline list<int>::iterator inc(list<int>::iterator it) { return ++it; } inline list<int>::iterator dcr(list<int>::iterator it) { return --it; } const int MAXN = 1000005; int N; pair<LL, LL> T[MAXN]; list<int> hull; struct Node { int first, last; LL first_value, last_value, first_by, last_by; LL add, mul; } _data[1<<21]; int M; void update(int i) { const Node& copy_first_from = (_data[2*i].first < _data[2*i+1].first) ? _data[2*i] : _data[2*i+1]; _data[i].first = copy_first_from.first; _data[i].first_value = copy_first_from.first_value; _data[i].first_by = copy_first_from.first_by; const Node& copy_last_from = (_data[2*i].last > _data[2*i+1].last) ? _data[2*i] : _data[2*i+1]; _data[i].last = copy_last_from.last; _data[i].last_value = copy_last_from.last_value; _data[i].last_by = copy_last_from.last_by; } void push(int v) { if (!_data[v].add && !_data[v].mul) return; _data[2*v].add += _data[v].add; _data[2*v+1].add += _data[v].add; _data[2*v].mul += _data[v].mul; _data[2*v+1].mul += _data[v].mul; REP(j,2) { _data[2*v+j].first_value += _data[v].add + _data[v].mul * _data[2*v+j].first_by; _data[2*v+j].last_value += _data[v].add + _data[v].mul * _data[2*v+j].last_by; } _data[v].add = _data[v].mul = 0; } void init() { M = 1; while (M < N) M <<= 1; REP(i,2*M) _data[i].first = M, _data[i].last = -1; REP(i,N) { _data[M+i].first_value = _data[M+i].last_value = T[i].nd; _data[M+i].first_by = _data[M+i].last_by = T[i].st; } } int CACHE_BREAKER = 1; void add(int A, int B, LL val, int v = 1, int L = 0, int R = M - 1) { if (B < L || R < A) return; if (A <= L && R <= B) { _data[v].add += val; _data[v].first_value += val; _data[v].last_value += val; return; } add(A, B, val, 2*v, L, (L+R)/2); add(A, B, val, 2*v+1, (L+R)/2+1, R); update(v); } void mul(int A, int B, LL val, int v = 1, int L = 0, int R = M - 1) { if (B < L || R < A) return; if (A <= L && R <= B) { _data[v].mul += val; _data[v].first_value += val * _data[v].first_by; _data[v].last_value += val * _data[v].last_by; return; } mul(A, B, val, 2*v, L, (L+R)/2); mul(A, B, val, 2*v+1, (L+R)/2+1, R); update(v); } void set_flag(int which, bool val, int v = 1, int L = 0, int R = M - 1) { if (L == R) { if (val) _data[v].first = _data[v].last = which; else _data[v].first = M, _data[v].last = -1; return; } push(v); if (which <= (L+R)/2) set_flag(which, val, 2*v, L, (L+R)/2); else set_flag(which, val, 2*v+1, (L+R)/2+1, R); update(v); } void remove_from_hull(int v) { set_flag(v, false); } void add_to_hull(int v) { set_flag(v, true); } LL _cache[MAXN]; int _cachever[MAXN]; LL get_value(int which) { if (_cachever[which] == CACHE_BREAKER) return _cache[which]; _cachever[which] = CACHE_BREAKER; int v = which + M; LL res = _data[v].first_value, mul = 0; while (v > 1) { v /= 2; res += _data[v].add; mul += _data[v].mul; } return _cache[which] = res + mul * T[which].st; } int find_best(int v = 1, int L = 0, int R = M - 1) { if (L == R) return L; push(v); bool use_left; if (_data[2*v].last == -1) { use_left = false; } else if (_data[2*v+1].first == M) { use_left = true; } else { use_left = _data[2*v].last_value > _data[2*v+1].first_value; } return use_left ? find_best(2*v, L, (L+R)/2) : find_best(2*v+1, (L+R)/2+1, R); } inline LL updated_value(int i) { return get_value(i); } bool ccw(int aa, int bb, int cc) { LL det = (T[aa].st-T[bb].st)*(get_value(bb)-get_value(cc))-(get_value(aa)-get_value(bb))*(T[bb].st-T[cc].st); //Dla T=int -> uwzględnić rozmiar (LL)! return (det > 0); }; vector<int> candidates[MAXN]; bool used[MAXN]; list<int>::iterator ptr[MAXN]; #define SECOND_LAST(l) (*dcr(dcr(l.end()))) void compute_hull() { REP(i,N) { while (hull.size() > 1 && !ccw(i, *dcr(hull.end()), SECOND_LAST(hull))) { candidates[i].pb(*dcr(hull.end())); candidates[SECOND_LAST(hull)].pb(*dcr(hull.end())); hull.erase(dcr(hull.end())); } hull.push_back(i); ptr[i] = dcr(hull.end()); } } LL pop() { int k = find_best(); used[k] = true; LL result = updated_value(k); remove_from_hull(k); add(0,k-1,T[k].st); mul(k,N-1,1); ++CACHE_BREAKER; list<int>::iterator best = ptr[k]; list<int>::iterator pos = inc(best); hull.erase(best); if (hull.empty()) { return result; } int a, b; if (pos == hull.end()) { a = *dcr(pos) + 1; b = N; } else if (pos == hull.begin()) { a = 0; b = *pos; } else { a = *dcr(pos) + 1; b = *pos; } sort(candidates[k].begin(), candidates[k].end()); candidates[k].erase(unique(candidates[k].begin(), candidates[k].end()), candidates[k].end()); set<int> to_add, to_remove; for (vector<int>::iterator it = candidates[k].begin(); it != candidates[k].end(); ++it) { int i = *it; if (i < a || i >= b || used[i]) continue; pos = ptr[i] = hull.insert(pos, i); to_add.insert(i); if (pos == hull.begin()) { ++pos; continue; } while (true) { list<int>::iterator prev = dcr(pos); if (prev == hull.begin() || ccw(i, *prev, *dcr(prev))) break; candidates[i].pb(*prev); candidates[*dcr(prev)].pb(*prev); if (to_add.find(*prev) == to_add.end()) to_remove.insert(*prev); else to_add.erase(*prev); hull.erase(prev); } ++pos; } int old_visited = 0; while (pos != hull.end()) { ++old_visited; bool removed_any = false; while (pos != hull.begin() && dcr(pos) != hull.begin() && !ccw(*pos, *dcr(pos), *dcr(dcr(pos)))) { candidates[*pos].pb(*dcr(pos)); candidates[*dcr(dcr(pos))].pb(*dcr(pos)); if (to_add.find(*dcr(pos)) == to_add.end()) to_remove.insert(*dcr(pos)); else to_add.erase(*dcr(pos)); hull.erase(dcr(pos)); removed_any = true; } if (old_visited >= 2 && !removed_any) break; } for (auto& a: to_remove) remove_from_hull(a); for (auto& a: to_add) add_to_hull(a); return result; } LL DP[1000000]; int main() { ios_base::sync_with_stdio(0); scanf("%d", &N); REP(i,N) scanf("%lld%lld", &T[i].st, &T[i].nd); sort(T, T+N); if (N <= 8000) { REP(i,N) FORD(j,N,0) DP[j] = max(DP[j], (j == 0 ? 0 : DP[j-1]) + T[i].nd + j * T[i].st); REP(i,N) printf("%lld\n", DP[i]); return 0; } init(); compute_hull(); for (auto& v: hull) _data[M+v].first = _data[M+v].last = v; FORD(i,M,1) update(i); LL sum = 0; REP(i,N) { sum += pop(); printf("%lld\n", sum); } } |