#include <bits/stdc++.h> using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) template<class C> void mini(C&a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C&a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<endl;} template<class TH, class...TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<",";_dbg(sdbg+1, a...); } template<class T> ostream& operator<<(ostream &os, vector<T> V) { os<<"[";for(auto vv:V)os<<vv<<","; os<<"]"; return os; } template<class L, class R> ostream& operator<<(ostream &os, pair<L, R> P) { os<<"("<<P.first<<","<<P.second<<")"; return os; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif const int MaxBuffer = (1 << 25); char bufferIn[MaxBuffer], bufferOut[MaxBuffer]; int inPtr, outPtr; inline bool isDigit(char ch) { return ch >= '0' && ch <= '9'; } LL getLL() { while (!isDigit(bufferIn[inPtr])) { inPtr++; } LL result = 0; while (isDigit(bufferIn[inPtr])) { result = result * 10 + bufferIn[inPtr++] - '0'; } return result; } void putLL(LL value) { if (!value) { bufferOut[outPtr++] = '0'; } else { int ptr = outPtr; while (value) { bufferOut[ptr++] = value % 10 + '0'; value /= 10; } reverse(bufferOut + outPtr, bufferOut + ptr); outPtr = ptr; } bufferOut[outPtr++] = '\n'; } // zmodowany splay z biblioteczki ACM-kowej struct node{ node *l, *r, *p; int size; LL val, add; node(LL _val): l(0), r(0), p(0), size(1), val(_val), add(0){} virtual void update(){ size = 1; //sum = val; if(l){ size += l->size; //sum += l->sum; } if(r){ size += r->size; //sum += r->sum; } } void touch(){ if (add) { if (l) { l->add += add; } if (r) { r->add += add; } val += add; add = 0; } } void touch_path(){ if(p) p->touch_path(); touch(); } node*& get_child(bool right){ return right ? r : l; } static void add_child(node* x, node* q, bool right){ if(x) x->get_child(right) = q; if(q) q->p = x; } inline bool is_right(){ return p && p->r == this; } void rotate(){ if(!p) return; node *oldp = p; bool right = is_right(); add_child(p->p, this, p->is_right()); add_child(oldp, get_child(!right), right); add_child(this, oldp, !right); oldp->update(); update(); } void splay_(){ while(p){ if(is_right() ^ p->is_right()) rotate(); else p->rotate(); rotate(); } } void splay(){ //dla nieodwracalych splay'ow zastapic przez splay_ touch_path(); splay_(); } void set_val(int nval){ val = nval; update(); } void add_all(LL value){ add += value; } node* get_last(){ node* res = this; while(1){ res->touch(); if(!res->r) break; res = res->r; } res->splay_(); return res; } node* remove() { if(l) l->p = nullptr; if(r) r->p = nullptr; node* root = join(l, r); l = r = nullptr; return root; } static node* join(node* a, node* b){ if(!a) return b; while(1){ a->touch(); if(!a->r) break; a = a->r; } a->splay_(); add_child(a, b, true); a->update(); return a; } // returns {new root, result, # of smaller than result} tuple<node*, node*, int> get_leq_linear(int a, LL b) { debug("leq", a, b); int sizeLeft = 0; node *result = nullptr, *cur = this, *last = this; while (cur) { cur->touch(); int chkSize = sizeLeft; if (cur->l) { chkSize += cur->l->size; } LL pos = (LL)a * chkSize + b; last = cur; debug(pos, cur->val); if (pos >= cur->val) { result = cur; cur = cur->l; } else { cur = cur->r; sizeLeft = chkSize + 1; } } last->splay_(); if (result) { result->splay_(); } return tuple<node*, node*, int>{last, result, sizeLeft}; } void iter_all(vector<LL> &acc) { touch(); if (l) { l->iter_all(acc); } acc.push_back(val); if (r) { r->iter_all(acc); } } /*void debug_tree() { cerr << "("; if (l) { l->debug_tree(); } else { cerr << "NULL"; } cerr << ", " << val << " [" << add << "], "; if (r) { r->debug_tree(); } else { cerr << "NULL"; } cerr << ")"; }*/ virtual ~node(){ delete l; delete r; } }; const int MaxN = (1 << 20); int N; LL start[MaxN], incr[MaxN]; int order[MaxN]; bool taken[MaxN]; LL curRes; //LL bests[MaxN]; node *bests; /*void debug_tree(node *tree) { if (!tree) { cerr << "NULL"; } else { tree->debug_tree(); } cerr << "\n"; }*/ int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(12); cerr << fixed << setprecision(6); fread_unlocked(bufferIn, 1, MaxBuffer, stdin); N = getLL(); //cin >> N; for (int i = 0; i < N; i++) { incr[i] = getLL(); start[i] = getLL(); //cin >> incr[i] >> start[i]; } iota(order, order + N, 0); sort(order, order + N, [&](int lhs, int rhs) { return incr[lhs] < incr[rhs]; }); bests = new node(start[order[0]]); for (int i = 1; i < N; i++) { int pos = order[i]; node *where; int cnt; tie(bests, where, cnt) = bests->get_leq_linear(incr[pos], start[pos]); //cerr << "\n"; //debug_tree(bests); //debug_tree(where); //debug(cnt); LL putVal = start[pos] + (LL)incr[pos] * cnt; if (!where) { bests->touch(); node::add_child(bests, new node(putVal), true); } else { where->splay_(); node *left = where->l; if (left) { left->p = nullptr; } where->l = nullptr; where->add_all(incr[pos]); bests = new node(putVal); node::add_child(bests, where, true); node::add_child(bests, left, false); } //debug_tree(bests); } vector<LL> increases; bests->iter_all(increases); LL result = 0; for (int i = 0; i < N; i++) { result += increases[i]; putLL(result); //cout << result << "\n"; } fwrite_unlocked(bufferOut, 1, outPtr, stdout); }
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 <bits/stdc++.h> using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) template<class C> void mini(C&a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C&a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<endl;} template<class TH, class...TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<",";_dbg(sdbg+1, a...); } template<class T> ostream& operator<<(ostream &os, vector<T> V) { os<<"[";for(auto vv:V)os<<vv<<","; os<<"]"; return os; } template<class L, class R> ostream& operator<<(ostream &os, pair<L, R> P) { os<<"("<<P.first<<","<<P.second<<")"; return os; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif const int MaxBuffer = (1 << 25); char bufferIn[MaxBuffer], bufferOut[MaxBuffer]; int inPtr, outPtr; inline bool isDigit(char ch) { return ch >= '0' && ch <= '9'; } LL getLL() { while (!isDigit(bufferIn[inPtr])) { inPtr++; } LL result = 0; while (isDigit(bufferIn[inPtr])) { result = result * 10 + bufferIn[inPtr++] - '0'; } return result; } void putLL(LL value) { if (!value) { bufferOut[outPtr++] = '0'; } else { int ptr = outPtr; while (value) { bufferOut[ptr++] = value % 10 + '0'; value /= 10; } reverse(bufferOut + outPtr, bufferOut + ptr); outPtr = ptr; } bufferOut[outPtr++] = '\n'; } // zmodowany splay z biblioteczki ACM-kowej struct node{ node *l, *r, *p; int size; LL val, add; node(LL _val): l(0), r(0), p(0), size(1), val(_val), add(0){} virtual void update(){ size = 1; //sum = val; if(l){ size += l->size; //sum += l->sum; } if(r){ size += r->size; //sum += r->sum; } } void touch(){ if (add) { if (l) { l->add += add; } if (r) { r->add += add; } val += add; add = 0; } } void touch_path(){ if(p) p->touch_path(); touch(); } node*& get_child(bool right){ return right ? r : l; } static void add_child(node* x, node* q, bool right){ if(x) x->get_child(right) = q; if(q) q->p = x; } inline bool is_right(){ return p && p->r == this; } void rotate(){ if(!p) return; node *oldp = p; bool right = is_right(); add_child(p->p, this, p->is_right()); add_child(oldp, get_child(!right), right); add_child(this, oldp, !right); oldp->update(); update(); } void splay_(){ while(p){ if(is_right() ^ p->is_right()) rotate(); else p->rotate(); rotate(); } } void splay(){ //dla nieodwracalych splay'ow zastapic przez splay_ touch_path(); splay_(); } void set_val(int nval){ val = nval; update(); } void add_all(LL value){ add += value; } node* get_last(){ node* res = this; while(1){ res->touch(); if(!res->r) break; res = res->r; } res->splay_(); return res; } node* remove() { if(l) l->p = nullptr; if(r) r->p = nullptr; node* root = join(l, r); l = r = nullptr; return root; } static node* join(node* a, node* b){ if(!a) return b; while(1){ a->touch(); if(!a->r) break; a = a->r; } a->splay_(); add_child(a, b, true); a->update(); return a; } // returns {new root, result, # of smaller than result} tuple<node*, node*, int> get_leq_linear(int a, LL b) { debug("leq", a, b); int sizeLeft = 0; node *result = nullptr, *cur = this, *last = this; while (cur) { cur->touch(); int chkSize = sizeLeft; if (cur->l) { chkSize += cur->l->size; } LL pos = (LL)a * chkSize + b; last = cur; debug(pos, cur->val); if (pos >= cur->val) { result = cur; cur = cur->l; } else { cur = cur->r; sizeLeft = chkSize + 1; } } last->splay_(); if (result) { result->splay_(); } return tuple<node*, node*, int>{last, result, sizeLeft}; } void iter_all(vector<LL> &acc) { touch(); if (l) { l->iter_all(acc); } acc.push_back(val); if (r) { r->iter_all(acc); } } /*void debug_tree() { cerr << "("; if (l) { l->debug_tree(); } else { cerr << "NULL"; } cerr << ", " << val << " [" << add << "], "; if (r) { r->debug_tree(); } else { cerr << "NULL"; } cerr << ")"; }*/ virtual ~node(){ delete l; delete r; } }; const int MaxN = (1 << 20); int N; LL start[MaxN], incr[MaxN]; int order[MaxN]; bool taken[MaxN]; LL curRes; //LL bests[MaxN]; node *bests; /*void debug_tree(node *tree) { if (!tree) { cerr << "NULL"; } else { tree->debug_tree(); } cerr << "\n"; }*/ int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(12); cerr << fixed << setprecision(6); fread_unlocked(bufferIn, 1, MaxBuffer, stdin); N = getLL(); //cin >> N; for (int i = 0; i < N; i++) { incr[i] = getLL(); start[i] = getLL(); //cin >> incr[i] >> start[i]; } iota(order, order + N, 0); sort(order, order + N, [&](int lhs, int rhs) { return incr[lhs] < incr[rhs]; }); bests = new node(start[order[0]]); for (int i = 1; i < N; i++) { int pos = order[i]; node *where; int cnt; tie(bests, where, cnt) = bests->get_leq_linear(incr[pos], start[pos]); //cerr << "\n"; //debug_tree(bests); //debug_tree(where); //debug(cnt); LL putVal = start[pos] + (LL)incr[pos] * cnt; if (!where) { bests->touch(); node::add_child(bests, new node(putVal), true); } else { where->splay_(); node *left = where->l; if (left) { left->p = nullptr; } where->l = nullptr; where->add_all(incr[pos]); bests = new node(putVal); node::add_child(bests, where, true); node::add_child(bests, left, false); } //debug_tree(bests); } vector<LL> increases; bests->iter_all(increases); LL result = 0; for (int i = 0; i < N; i++) { result += increases[i]; putLL(result); //cout << result << "\n"; } fwrite_unlocked(bufferOut, 1, outPtr, stdout); } |