#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <ctime> #include <cassert> using namespace std; int n; vector <pair <long long, long long> > grz; long long inf = 1.6e18; int gpd; long long wynik = 0; class Splay { private: struct node { node *left; node *right; node *par; long long war; long long dd; int roz; node(long long W) : left(0), right(0), par(0), war(W), dd(0), roz(1) {} ~node() { if(left) delete left; if(right) delete right; } } *root; void left_rotate(node *x) { node *y = x -> par; y -> war += y -> dd; if(y -> left) y -> left -> dd += y -> dd; x -> dd += y -> dd; y -> dd = 0; x -> war += x -> dd; if(x -> left) x -> left -> dd += x -> dd; if(x -> right) x -> right -> dd += x -> dd; x -> dd = 0; y -> right = 0; if(x -> left) { y -> right = x -> left; x -> left -> par = y; } x -> par = y -> par; if(x -> par) { if(y == x -> par -> left) x -> par -> left = x; else x -> par -> right = x; } y -> par = x; x -> left = y; y -> roz = x -> roz = 1; if(y -> left) y -> roz += y -> left -> roz; if(y -> right) y -> roz += y -> right -> roz; x -> roz += y -> roz; if(x -> right) x -> roz += x -> right -> roz; } void right_rotate(node *x) { node *y = x -> par; y -> war += y -> dd; if(y -> right) y -> right -> dd += y -> dd; x -> dd += y -> dd; y -> dd = 0; x -> war += x -> dd; if(x -> right) x -> right -> dd += x -> dd; if(x -> left) x -> left -> dd += x -> dd; x -> dd = 0; y -> left = 0; if(x -> right) { y -> left = x -> right; x -> right -> par = y; } x -> par = y -> par; if(x -> par) { if(y == x -> par -> left) x -> par -> left = x; else x -> par -> right = x; } y -> par = x; x -> right = y; y -> roz = x -> roz = 1; if(y -> left) y -> roz += y -> left -> roz; if(y -> right) y -> roz += y -> right -> roz; x -> roz += y -> roz; if(x -> left) x -> roz += x -> left -> roz; } void splay(node *x) { while(x -> par) { node *y = x -> par; if(!(y -> par)) { if(x == y -> left) right_rotate(x); else left_rotate(x); } else if(x == y -> left && y == y -> par -> left) { right_rotate(x -> par); right_rotate(x); } else if(x == y -> right && y == y -> par -> right) { left_rotate(x -> par); left_rotate(x); } else if(x == y -> left && y == y -> par -> right) { right_rotate(x); left_rotate(x); } else { left_rotate(x); right_rotate(x); } } root = x; } node *insert(node *x, long long war) { node *y = x -> left; if(y) { x = y; while(x) { y = x; x = x -> right; } x = new node (war); x -> par = y; y -> right = x; splay(x); return x; } else { y = new node (war); y -> par = x; x -> left = y; splay(y); return y; } } node *Find(long long a, long long b) { node *x = root; node *y = 0; long long dod = 0; int ile = 0; node *r = 0; while(x) { y = x; dod += x -> dd; int pp = 1 + ile; if(x -> left) pp += x -> left -> roz; if(x -> war + dod - a * (pp - 1) <= b - a) { r = x; x = x -> left; } else { ile = pp; x = x -> right; } } if(y) splay(y); return r; } void dfs(node *x, long long add) { add += x -> dd; if(x -> left) dfs(x -> left, add); if(gpd > 0 && gpd <= n) { wynik += add + x -> war; cout << wynik << '\n'; } gpd++; if(x -> right) dfs(x -> right, add); } public: Splay() { root = new node (inf); node *x = new node (-inf); x -> par = root; root -> right = x; } ~Splay() { if(root) delete root; } void wrzucaj(long long a, long long b) { node *p = Find(a, b); node *x = insert(p, p -> war + p -> dd); x -> war = p -> war + p -> dd; int il = 0; if(x -> left) il += x -> left -> roz; if(x -> war - a * il < b - a) x -> war = b + a * (il - 1); if(x -> right) { x -> right -> dd += a; splay(x -> right); } } void wyswietl() { gpd = 0; dfs(root, 0); } }; int main() { srand(time(0)); ios_base::sync_with_stdio(0); cin >> n; grz.resize(n); for(int i = 0; i < n; ++i) cin >> grz[i].first >> grz[i].second; sort(grz.begin(), grz.end()); Splay X; for(int j = 0; j < n; ++j) X.wrzucaj(grz[j].first, grz[j].second); X.wyswietl(); 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 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 | #include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <ctime> #include <cassert> using namespace std; int n; vector <pair <long long, long long> > grz; long long inf = 1.6e18; int gpd; long long wynik = 0; class Splay { private: struct node { node *left; node *right; node *par; long long war; long long dd; int roz; node(long long W) : left(0), right(0), par(0), war(W), dd(0), roz(1) {} ~node() { if(left) delete left; if(right) delete right; } } *root; void left_rotate(node *x) { node *y = x -> par; y -> war += y -> dd; if(y -> left) y -> left -> dd += y -> dd; x -> dd += y -> dd; y -> dd = 0; x -> war += x -> dd; if(x -> left) x -> left -> dd += x -> dd; if(x -> right) x -> right -> dd += x -> dd; x -> dd = 0; y -> right = 0; if(x -> left) { y -> right = x -> left; x -> left -> par = y; } x -> par = y -> par; if(x -> par) { if(y == x -> par -> left) x -> par -> left = x; else x -> par -> right = x; } y -> par = x; x -> left = y; y -> roz = x -> roz = 1; if(y -> left) y -> roz += y -> left -> roz; if(y -> right) y -> roz += y -> right -> roz; x -> roz += y -> roz; if(x -> right) x -> roz += x -> right -> roz; } void right_rotate(node *x) { node *y = x -> par; y -> war += y -> dd; if(y -> right) y -> right -> dd += y -> dd; x -> dd += y -> dd; y -> dd = 0; x -> war += x -> dd; if(x -> right) x -> right -> dd += x -> dd; if(x -> left) x -> left -> dd += x -> dd; x -> dd = 0; y -> left = 0; if(x -> right) { y -> left = x -> right; x -> right -> par = y; } x -> par = y -> par; if(x -> par) { if(y == x -> par -> left) x -> par -> left = x; else x -> par -> right = x; } y -> par = x; x -> right = y; y -> roz = x -> roz = 1; if(y -> left) y -> roz += y -> left -> roz; if(y -> right) y -> roz += y -> right -> roz; x -> roz += y -> roz; if(x -> left) x -> roz += x -> left -> roz; } void splay(node *x) { while(x -> par) { node *y = x -> par; if(!(y -> par)) { if(x == y -> left) right_rotate(x); else left_rotate(x); } else if(x == y -> left && y == y -> par -> left) { right_rotate(x -> par); right_rotate(x); } else if(x == y -> right && y == y -> par -> right) { left_rotate(x -> par); left_rotate(x); } else if(x == y -> left && y == y -> par -> right) { right_rotate(x); left_rotate(x); } else { left_rotate(x); right_rotate(x); } } root = x; } node *insert(node *x, long long war) { node *y = x -> left; if(y) { x = y; while(x) { y = x; x = x -> right; } x = new node (war); x -> par = y; y -> right = x; splay(x); return x; } else { y = new node (war); y -> par = x; x -> left = y; splay(y); return y; } } node *Find(long long a, long long b) { node *x = root; node *y = 0; long long dod = 0; int ile = 0; node *r = 0; while(x) { y = x; dod += x -> dd; int pp = 1 + ile; if(x -> left) pp += x -> left -> roz; if(x -> war + dod - a * (pp - 1) <= b - a) { r = x; x = x -> left; } else { ile = pp; x = x -> right; } } if(y) splay(y); return r; } void dfs(node *x, long long add) { add += x -> dd; if(x -> left) dfs(x -> left, add); if(gpd > 0 && gpd <= n) { wynik += add + x -> war; cout << wynik << '\n'; } gpd++; if(x -> right) dfs(x -> right, add); } public: Splay() { root = new node (inf); node *x = new node (-inf); x -> par = root; root -> right = x; } ~Splay() { if(root) delete root; } void wrzucaj(long long a, long long b) { node *p = Find(a, b); node *x = insert(p, p -> war + p -> dd); x -> war = p -> war + p -> dd; int il = 0; if(x -> left) il += x -> left -> roz; if(x -> war - a * il < b - a) x -> war = b + a * (il - 1); if(x -> right) { x -> right -> dd += a; splay(x -> right); } } void wyswietl() { gpd = 0; dfs(root, 0); } }; int main() { srand(time(0)); ios_base::sync_with_stdio(0); cin >> n; grz.resize(n); for(int i = 0; i < n; ++i) cin >> grz[i].first >> grz[i].second; sort(grz.begin(), grz.end()); Splay X; for(int j = 0; j < n; ++j) X.wrzucaj(grz[j].first, grz[j].second); X.wyswietl(); return 0; } |