#include <cctype> #include <cstdio> #include <vector> #include <algorithm> typedef long long i64; const int N = 1000000 + 10; i64 nextInt() { char ch; while (!isdigit(ch = getchar())) {} i64 res = ch - '0'; while (isdigit(ch = getchar())) res = 10 * res + ch - '0'; return res; } struct Node { Node *ch[2], *p; int size; i64 tag, val; inline bool dir() const { return this == p->ch[1]; } inline void apply(i64); inline void release(); inline void update(); void rotate(); void splay(Node*); void dfs(); } pool[N]; inline void Node::apply(i64 t) { tag += t; val += t; } inline void Node::release() { if (tag) { if (ch[0]) ch[0]->apply(tag); if (ch[1]) ch[1]->apply(tag); tag = 0; } } inline void Node::update() { size = 1; if (ch[0]) size += ch[0]->size; if (ch[1]) size += ch[1]->size; } void Node::rotate() { Node *v = p; v->release(); release(); int d = dir(); if (v->p) v->p->ch[v->dir()] = this; p = v->p; if (ch[!d]) ch[!d]->p = v; v->ch[d] = ch[!d]; ch[!d] = v; v->p = this; v->update(); update(); } void Node::splay(Node *t = NULL) { release(); while (p != t) { if (p->p != t) (dir() == p->dir() ? p : this)->rotate(); rotate(); } update(); } Node *select(int a, i64 b) { int k = 0; Node *p = &pool[1]; p->splay(); Node *res = NULL; while (p) { p->release(); int s = p->ch[0] ? p->ch[0]->size : 0; if ((i64)a * (k + s) + b < p->val) { res = p; k += s + 1; p = p->ch[1]; } else { p = p->ch[0]; } } return res; } i64 ans[N]; int tot; void Node::dfs() { release(); if (ch[0]) ch[0]->dfs(); ans[++tot] = val; if (ch[1]) ch[1]->dfs(); } int n; std::pair<int, i64> a[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) a[i].first = nextInt(), a[i].second = nextInt(); std::sort(a + 1, a + n + 1); pool[1].val = a[1].second; pool[1].update(); for (int i = 2; i <= n; ++i) { int x = a[i].first; i64 y = a[i].second; Node *p = select(x, y); Node *q = &pool[i]; if (p) { p->splay(); int s = p->ch[0] ? p->ch[0]->size : 0; q->val = x * (s + 1LL) + y; if (p->ch[1]) p->ch[1]->apply(x), p->ch[1]->p = q, q->ch[1] = p->ch[1], q->update(); q->p = p; p->ch[1] = q; p->update(); q->splay(); } else { p = &pool[1]; p->splay(); q->val = y; p->apply(x); q->ch[1] = p; p->p = q; q->update(); } } tot = 0; pool[1].splay(); pool[1].dfs(); for (int i = 2; i <= n; ++i) ans[i] += ans[i - 1]; for (int i = 1; i <= n; ++i) printf("%lld\n", ans[i]); return 0; }
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 | #include <cctype> #include <cstdio> #include <vector> #include <algorithm> typedef long long i64; const int N = 1000000 + 10; i64 nextInt() { char ch; while (!isdigit(ch = getchar())) {} i64 res = ch - '0'; while (isdigit(ch = getchar())) res = 10 * res + ch - '0'; return res; } struct Node { Node *ch[2], *p; int size; i64 tag, val; inline bool dir() const { return this == p->ch[1]; } inline void apply(i64); inline void release(); inline void update(); void rotate(); void splay(Node*); void dfs(); } pool[N]; inline void Node::apply(i64 t) { tag += t; val += t; } inline void Node::release() { if (tag) { if (ch[0]) ch[0]->apply(tag); if (ch[1]) ch[1]->apply(tag); tag = 0; } } inline void Node::update() { size = 1; if (ch[0]) size += ch[0]->size; if (ch[1]) size += ch[1]->size; } void Node::rotate() { Node *v = p; v->release(); release(); int d = dir(); if (v->p) v->p->ch[v->dir()] = this; p = v->p; if (ch[!d]) ch[!d]->p = v; v->ch[d] = ch[!d]; ch[!d] = v; v->p = this; v->update(); update(); } void Node::splay(Node *t = NULL) { release(); while (p != t) { if (p->p != t) (dir() == p->dir() ? p : this)->rotate(); rotate(); } update(); } Node *select(int a, i64 b) { int k = 0; Node *p = &pool[1]; p->splay(); Node *res = NULL; while (p) { p->release(); int s = p->ch[0] ? p->ch[0]->size : 0; if ((i64)a * (k + s) + b < p->val) { res = p; k += s + 1; p = p->ch[1]; } else { p = p->ch[0]; } } return res; } i64 ans[N]; int tot; void Node::dfs() { release(); if (ch[0]) ch[0]->dfs(); ans[++tot] = val; if (ch[1]) ch[1]->dfs(); } int n; std::pair<int, i64> a[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) a[i].first = nextInt(), a[i].second = nextInt(); std::sort(a + 1, a + n + 1); pool[1].val = a[1].second; pool[1].update(); for (int i = 2; i <= n; ++i) { int x = a[i].first; i64 y = a[i].second; Node *p = select(x, y); Node *q = &pool[i]; if (p) { p->splay(); int s = p->ch[0] ? p->ch[0]->size : 0; q->val = x * (s + 1LL) + y; if (p->ch[1]) p->ch[1]->apply(x), p->ch[1]->p = q, q->ch[1] = p->ch[1], q->update(); q->p = p; p->ch[1] = q; p->update(); q->splay(); } else { p = &pool[1]; p->splay(); q->val = y; p->apply(x); q->ch[1] = p; p->p = q; q->update(); } } tot = 0; pool[1].splay(); pool[1].dfs(); for (int i = 2; i <= n; ++i) ans[i] += ans[i - 1]; for (int i = 1; i <= n; ++i) printf("%lld\n", ans[i]); return 0; } |