/* Copyright 2016 AcrossTheSky */ #include <iostream> #include <cstdio> #include <utility> #include <cassert> #include <map> #include <vector> #include <deque> #include <queue> #include <stack> #include <set> #include <cstring> #include <cstdlib> #include <cctype> #include <sstream> #include <fstream> #include <string> #include <cmath> #include <algorithm> #define REP(i, a, b) for (int i = (a); i <= (b); ++i) #define PER(i, a, b) for (int i = (a); i >= (b); --i) #define RVC(i, c) fot (int i = 0; i < (c).size(); ++i) #define RED(k, u) for (int k = head[(u)]; k; k = edge[k].next) #define lowbit(x) ((x) & (-(x))) #define CL(x, v) memset(x, v, sizeof x) #define MP std::make_pair #define PB push_back #define FR first #define SC second #define rank rankk #define next nextt #define link linkk #define index indexx #define abs(x) ((x) > 0 ? (x) : (-(x))) using namespace std; typedef long long LL; typedef pair<int, int> PII; template<class T> inline bool getmin(T *a, const T &b) { if (b < *a) { *a = b; return true; } return false; } template<class T> inline bool getmax(T *a, const T &b) { if (b > *a) { *a = b; return true; } return false; } template<class T> inline void read(T *a) { char c; while (isspace(c = getchar())) {} bool flag = 0; if (c == '-') flag = 1, *a = 0; else *a = c - 48; while (isdigit(c = getchar())) *a = *a * 10 + c - 48; if (flag) *a = -*a; } const int mo = 1000000007; template<class T> T pow(T a, T b, int c = mo) { T res = 1; for (T i = 1; i <= b; i <<= 1, a = 1LL * a * a % c) if (b & i) res = 1LL * res * a % c; return res; } /*======================= TEMPLATE =======================*/ const int N = 1100000; struct node { node *l, *r; unsigned fix; int size; LL f, t; void down() { if (!t) return; if (l) l -> f += t, l -> t += t; if (r) r -> f += t, r -> t += t; t = 0; } void up() { size = 1; if (l) size += l -> size; if (r) size += r -> size; } void add(LL x) { f += x; t += x; } }pool[N], *tree; typedef pair<node*, node*> PN; int tot; node *merge(node *a, node *b) { if (!a || !b) return a ? a : b; a -> down(); b -> down(); if (a -> fix > b -> fix) { a -> r = merge(a -> r, b); a -> up(); return a; } else { b -> l = merge(a, b -> l); b -> up(); return b; } } PN split(node *a, int x) { if (!a) return MP((node*)0, (node*)0); int sz = 1; if (a -> l) sz += a -> l -> size; a -> down(); if (x >= sz) { PN tmp = split(a -> r, x - sz); a -> r = tmp.FR; a -> up(); return MP(a, tmp.SC); } else { PN tmp = split(a -> l, x); a -> l = tmp.SC; a -> up(); return MP(tmp.FR, a); } } node* insert(node *a, node *b, int x) { if (!a || !b) return a ? a : b; if (a -> fix > b -> fix) { int sz = 1; if (a -> l) sz += a -> l -> size; if (x >= sz) a -> r = insert(a -> r, b, x - sz); else a -> l = insert(a -> l, b, x); a -> up(); return a; } else { PN tmp = split(a, x); b -> l = tmp.FR; b -> r = tmp.SC; b -> up(); return b; } } int find(node *u, const LL &a, const LL &b, int now) { if (!u) return 0; int sz = 1; if (u -> l) sz += u -> l -> size; u -> down(); if (a * (now + sz - 1) + b > u -> f) { int v = find(u -> l, a, b, now); if (!v) return now + sz; else return v; } else return find(u -> r, a, b, now + sz); } node* newnode(LL x) { node *np = &pool[++tot]; np -> l = np -> r = NULL; np -> f = x; np -> size = 1; np -> fix = 1u * rand() * rand(); np -> t = 0; return np; } int n; pair<LL, LL> a[N]; LL ans[N]; void dfs(node *u, int s) { u -> down(); int sz = 1; if (u -> l) dfs(u -> l, s), sz += u -> l -> size; ans[s + sz] = u -> f; if (u -> r) dfs(u -> r, s + sz); } int main() { cin >> n; REP(i, 1, n) read(&a[i].FR), read(&a[i].SC); sort(a + 1, a + n + 1); REP(i, 1, n) { int j = find(tree, a[i].FR, a[i].SC, 0); if (!j) tree = merge(tree, newnode(a[i].SC + a[i].FR * (i - 1))); else { tree = insert(tree, newnode(a[i].SC + a[i].FR * (j - 1)), j - 1); PN tmp = split(tree, j); tmp.SC -> add(a[i].FR); tree = merge(tmp.FR, tmp.SC); } } dfs(tree, 0); REP(i, 1, n) { ans[i] += ans[i - 1]; 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 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 | /* Copyright 2016 AcrossTheSky */ #include <iostream> #include <cstdio> #include <utility> #include <cassert> #include <map> #include <vector> #include <deque> #include <queue> #include <stack> #include <set> #include <cstring> #include <cstdlib> #include <cctype> #include <sstream> #include <fstream> #include <string> #include <cmath> #include <algorithm> #define REP(i, a, b) for (int i = (a); i <= (b); ++i) #define PER(i, a, b) for (int i = (a); i >= (b); --i) #define RVC(i, c) fot (int i = 0; i < (c).size(); ++i) #define RED(k, u) for (int k = head[(u)]; k; k = edge[k].next) #define lowbit(x) ((x) & (-(x))) #define CL(x, v) memset(x, v, sizeof x) #define MP std::make_pair #define PB push_back #define FR first #define SC second #define rank rankk #define next nextt #define link linkk #define index indexx #define abs(x) ((x) > 0 ? (x) : (-(x))) using namespace std; typedef long long LL; typedef pair<int, int> PII; template<class T> inline bool getmin(T *a, const T &b) { if (b < *a) { *a = b; return true; } return false; } template<class T> inline bool getmax(T *a, const T &b) { if (b > *a) { *a = b; return true; } return false; } template<class T> inline void read(T *a) { char c; while (isspace(c = getchar())) {} bool flag = 0; if (c == '-') flag = 1, *a = 0; else *a = c - 48; while (isdigit(c = getchar())) *a = *a * 10 + c - 48; if (flag) *a = -*a; } const int mo = 1000000007; template<class T> T pow(T a, T b, int c = mo) { T res = 1; for (T i = 1; i <= b; i <<= 1, a = 1LL * a * a % c) if (b & i) res = 1LL * res * a % c; return res; } /*======================= TEMPLATE =======================*/ const int N = 1100000; struct node { node *l, *r; unsigned fix; int size; LL f, t; void down() { if (!t) return; if (l) l -> f += t, l -> t += t; if (r) r -> f += t, r -> t += t; t = 0; } void up() { size = 1; if (l) size += l -> size; if (r) size += r -> size; } void add(LL x) { f += x; t += x; } }pool[N], *tree; typedef pair<node*, node*> PN; int tot; node *merge(node *a, node *b) { if (!a || !b) return a ? a : b; a -> down(); b -> down(); if (a -> fix > b -> fix) { a -> r = merge(a -> r, b); a -> up(); return a; } else { b -> l = merge(a, b -> l); b -> up(); return b; } } PN split(node *a, int x) { if (!a) return MP((node*)0, (node*)0); int sz = 1; if (a -> l) sz += a -> l -> size; a -> down(); if (x >= sz) { PN tmp = split(a -> r, x - sz); a -> r = tmp.FR; a -> up(); return MP(a, tmp.SC); } else { PN tmp = split(a -> l, x); a -> l = tmp.SC; a -> up(); return MP(tmp.FR, a); } } node* insert(node *a, node *b, int x) { if (!a || !b) return a ? a : b; if (a -> fix > b -> fix) { int sz = 1; if (a -> l) sz += a -> l -> size; if (x >= sz) a -> r = insert(a -> r, b, x - sz); else a -> l = insert(a -> l, b, x); a -> up(); return a; } else { PN tmp = split(a, x); b -> l = tmp.FR; b -> r = tmp.SC; b -> up(); return b; } } int find(node *u, const LL &a, const LL &b, int now) { if (!u) return 0; int sz = 1; if (u -> l) sz += u -> l -> size; u -> down(); if (a * (now + sz - 1) + b > u -> f) { int v = find(u -> l, a, b, now); if (!v) return now + sz; else return v; } else return find(u -> r, a, b, now + sz); } node* newnode(LL x) { node *np = &pool[++tot]; np -> l = np -> r = NULL; np -> f = x; np -> size = 1; np -> fix = 1u * rand() * rand(); np -> t = 0; return np; } int n; pair<LL, LL> a[N]; LL ans[N]; void dfs(node *u, int s) { u -> down(); int sz = 1; if (u -> l) dfs(u -> l, s), sz += u -> l -> size; ans[s + sz] = u -> f; if (u -> r) dfs(u -> r, s + sz); } int main() { cin >> n; REP(i, 1, n) read(&a[i].FR), read(&a[i].SC); sort(a + 1, a + n + 1); REP(i, 1, n) { int j = find(tree, a[i].FR, a[i].SC, 0); if (!j) tree = merge(tree, newnode(a[i].SC + a[i].FR * (i - 1))); else { tree = insert(tree, newnode(a[i].SC + a[i].FR * (j - 1)), j - 1); PN tmp = split(tree, j); tmp.SC -> add(a[i].FR); tree = merge(tmp.FR, tmp.SC); } } dfs(tree, 0); REP(i, 1, n) { ans[i] += ans[i - 1]; printf("%lld\n", ans[i]); } return 0; } |