-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaxrope.cpp
360 lines (317 loc) · 8.55 KB
/
maxrope.cpp
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// iran
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;
//#define endl '\n'
class xorshift {
uint64_t x;
public:
xorshift() {
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
x = rnd();
for (int i = 0; i < 100; i++) {
random();
}
}
uint64_t random() {
x = x ^ (x << 7);
return x = x ^ (x >> 9);
}
};
using T = i64;
struct SumMonoid {
static constexpr T id() {
return 0;
}
static T op(T a, T b) {
return a + b;
}
};
struct MinMonoid {
static constexpr T id() {
return 2e18;
}
static T op(T a, T b) {
return min(a, b);
}
};
// 本来はSumMonoid用
struct UpdateMonoid {
static constexpr T id() {
return 2e18;
}
static T op(T a, T b) {
return b;
}
};
struct Modifier {
// lazyの結果によってaccがどう変わるか。szは部分木のサイズ
static T op(T a, T b, int sz) {
return b == UpdateMonoid::id() ? a : b * sz;
}
};
template<class Monoid, class OperatorMonoid>
class ImplicitTreap {
xorshift rnd;
struct Node {
T value, acc, lazy;
int priority, cnt;
bool rev;
Node *l, *r;
Node(T value, int priority) : value(value), acc(Monoid::id()), lazy(OperatorMonoid::id()), priority(priority), cnt(1), rev(false), l(nullptr), r(nullptr) {}
} *root = nullptr;
using Tree = Node *;
int cnt(Tree t) {
return t ? t->cnt : 0;
}
T acc(Tree t) {
return t ? t->acc : Monoid::id();
}
void update_cnt(Tree t) {
if (t) {
t->cnt = 1 + cnt(t->l) + cnt(t->r);
}
}
void update_acc(Tree t) {
if (t) {
t->acc = Monoid::op(acc(t->l), Monoid::op(t->value, acc(t->r)));
}
}
void pushup(Tree t) {
update_cnt(t), update_acc(t);
}
void pushdown(Tree t) {
if (t && t->rev) {
t->rev = false;
swap(t->l, t->r);
if (t->l) t->l->rev ^= 1;
if (t->r) t->r->rev ^= 1;
}
if (t && t->lazy != OperatorMonoid::id()) {
if (t->l) {
t->l->lazy = OperatorMonoid::op(t->l->lazy, t->lazy);
t->l->acc = Modifier::op(t->l->acc, t->lazy, cnt(t->l));
}
if (t->r) {
t->r->lazy = OperatorMonoid::op(t->r->lazy, t->lazy);
t->r->acc = Modifier::op(t->r->acc, t->lazy, cnt(t->r));
}
t->value = Modifier::op(t->value, t->lazy, 1);
t->lazy = OperatorMonoid::id();
}
pushup(t);
}
void split(Tree t, int key, Tree& l, Tree& r) {
if (!t) {
l = r = nullptr;
return;
}
pushdown(t);
int implicit_key = cnt(t->l) + 1;
if (key < implicit_key) {
split(t->l, key, l, t->l), r = t;
} else {
split(t->r, key - implicit_key, t->r, r), l = t;
}
pushup(t);
}
void insert(Tree& t, int key, Tree item) {
Tree t1, t2;
split(t, key, t1, t2);
merge(t1, t1, item);
merge(t, t1, t2);
}
void merge(Tree& t, Tree l, Tree r) {
pushdown(l);
pushdown(r);
if (!l || !r) {
t = l ? l : r;
} else if (l->priority > r->priority) {
merge(l->r, l->r, r), t = l;
} else {
merge(r->l, l, r->l), t = r;
}
pushup(t);
}
void erase(Tree& t, int key) {
Tree t1, t2, t3;
split(t, key + 1, t1, t2);
split(t1, key, t1, t3);
merge(t, t1, t2);
}
void update(Tree t, int l, int r, T x) {
if (l >= r) return;
Tree t1, t2, t3;
split(t, l, t1, t2);
split(t2, r - l, t2 , t3);
t2->lazy = OperatorMonoid::op(t2->lazy, x);
t2->acc = Modifier::op(t2->acc, x, cnt(t2));
merge(t2, t2, t3);
merge(t, t1, t2);
}
T query(Tree t, int l, int r) {
if (l == r) return Monoid::id();
Tree t1, t2, t3;
split(t, l, t1, t2);
split(t2, r - l, t2, t3);
T ret = t2->acc;
merge(t2, t2, t3);
merge(t, t1, t2);
return ret;
}
// [l, r)の中で左から何番目か
int find(Tree t, T x, int offset, bool left = true) {
if (Monoid::op(t->acc, x) == x) {
return -1;
} else {
if (left) {
if (t->l && Monoid::op(t->l->acc, x) != x) {
return find(t->l, x, offset, left);
} else {
return (Monoid::op(t->value, x) != x) ? offset + cnt(t->l) : find(t->r, x, offset + cnt(t->l) + 1, left);
}
} else {
if (t->r && Monoid::op(t->r->acc, x) != x) {
return find(t->r, x, offset + cnt(t->l) + 1, left);
} else {
return (Monoid::op(t->value, x) != x) ? offset + cnt(t->l) : find(t->l, x, offset, left);
}
}
}
}
void reverse(Tree t, int l, int r) {
if (l > r) return;
Tree t1, t2, t3;
split(t, l, t1, t2);
split(t2, r - l, t2, t3);
t2->rev ^= 1;
merge(t2, t2, t3);
merge(t, t1, t2);
}
// [l, r)の先頭がmになるようにシフトさせる。std::rotateと同じ仕様
void rotate(Tree t, int l, int m, int r) {
reverse(t, l, r);
reverse(t, l, l + r - m);
reverse(t, l + r - m, r);
}
void dump(Tree t) {
if (!t) return;
pushdown(t);
dump(t->l);
cout << t->value << " ";
dump(t->r);
}
public:
ImplicitTreap() {}
ImplicitTreap(vector<T> as) {
::reverse(as.begin(), as.end());
for (T a : as) {
insert(0, a);
}
}
int size() {
return cnt(root);
}
void insert(int pos, T x) {
insert(root, pos, new Node(x, rnd.random()));
}
void update(int l, int r, T x) {
update(root, l, r, x);
}
T query(int l, int r) {
return query(root, l, r);
}
// 二分探索。[l, r)内のkでMonoid::op(tr[k], x) != xとなる最左/最右のもの。存在しない場合は-1
// たとえばMinMonoidの場合、x未満の最左/最右の要素の位置を返す
int binary_search(int l, int r, T x, bool left = true) {
if (l >= r) return -1;
Tree t1, t2, t3;
split(root, l, t1, t2);
split(t2, r - l, t2, t3);
int ret = find(t2, x, l, left);
merge(t2, t2, t3);
merge(root, t1, t2);
return ret;
}
void erase(int pos) {
erase(root, pos);
}
void reverse(int l, int r) {
reverse(root, l, r);
}
void rotate(int l, int m, int r) {
rotate(root, l, m, r);
}
void dump() {
dump(root);
cout << endl;
}
T operator[](int pos) {
return query(pos, pos + 1);
}
};
struct MaxRope {
ImplicitTreap<SumMonoid, UpdateMonoid> tr;
ImplicitTreap<MinMonoid, UpdateMonoid> tr2;
int cnt = 0;
void add(T a) {
int p = tr2.binary_search(0, tr2.size(), a);
if (p == -1) {
tr.insert(tr.size(), a);
tr2.insert(tr2.size(), a);
} else {
tr.insert(p, a);
tr2.insert(p, a);
}
cnt++;
}
int size() const {
return cnt;
}
T sum(int k) {
return tr.query(0, k);
}
T operator[](int k) {
return tr[k];
}
void dump() {
tr.dump();
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int q;
cin >> q;
MaxRope rope;
long long sum = 0;
long long B = 0;
while (q--) {
int t;
cin >> t;
if (t == 1) {
int a, b;
cin >> a >> b;
rope.add(a);
sum += a;
B += b;
} else {
int n = rope.size();
if (n & 1) {
int mid = n / 2;
long long M = rope[mid];
long long large = rope.sum(mid);
long long small = sum - large - M;
cout << M << ' ' << M * mid - small + large - mid * M + B << '\n';
} else {
int mid = n / 2;
long long M = rope[mid];
long long large = rope.sum(mid);
long long small = sum - large - M;
cout << M << ' ' << M * (mid - 1) - small + large - mid * M + B << '\n';
}
}
}
}