#include "bubblesort2.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
struct node {
node *left, *right;
ii key;
int val, mx, sz;
int prior, lazy;
node(ii k, int v) {
left = right = NULL;
key = k;
val = mx = v;
sz = 1;
prior = rand();
lazy = 0;
}
};
typedef node* pnode;
void push(pnode t) {
if (!t || t->lazy == 0)
return;
t->val += t->lazy;
t->mx += t->lazy;
if (t->left)
t->left->lazy += t->lazy;
if (t->right)
t->right->lazy += t->lazy;
t->lazy = 0;
}
void update(pnode t) {
if (!t) return;
push(t->left);
push(t->right);
t->mx = t->val;
t->sz = 1;
if (t->left) {
t->mx = max(t->mx, t->left->mx);
t->sz += t->left->sz;
}
if (t->right) {
t->mx = max(t->mx, t->right->mx);
t->sz += t->right->sz;
}
}
void split(pnode t, pnode &L, pnode &R, ii k) {
if (!t) {
L = R = NULL;
return;
}
push(t);
if (k < t->key) {
split(t->left, L, t->left, k);
R = t;
} else {
split(t->right, t->right, R, k);
L = t;
}
update(t);
}
void merge(pnode &t, pnode L, pnode R) {
push(L); push(R);
if (!L || !R) {
t = (L ? L : R);
return;
}
if (L->prior > R->prior) {
merge(L->right, L->right, R);
t = L;
} else {
merge(R->left, L, R->left);
t = R;
}
update(t);
}
void inorder(pnode t) {
if (!t) return;
push(t);
update(t);
inorder(t->left);
printf("(%d, %d): val = %d, mx = %d\n", t->key.fi, t->key.se, t->val, t->mx);
inorder(t->right);
}
vector<int> countScans(vector<int> A, vector<int> X, vector<int> V) {
srand(time(NULL));
vector<ii> sor;
int n = (int)A.size();
for (int i = 0; i < n; i++) {
sor.pb(mp(A[i], i));
}
sort(sor.begin(), sor.end());
pnode root = NULL;
for (int i = 0; i < n; i++) {
merge(root, root, new node(sor[i], sor[i].se - i));
}
vector<int> ans;
int q = (int)X.size();
for (int i = 0; i < q; i++) {
ii bef = mp(A[X[i]], X[i]);
A[X[i]] = V[i];
ii aft = mp(A[X[i]], X[i]);
pnode other = NULL, dummy = NULL;
split(root, root, other, bef);
if (other) {
other->lazy++;
push(other);
}
split(root, root, dummy, mp(bef.fi, bef.se - 1));
delete dummy;
merge(root, root, other);
split(root, root, other, aft);
merge(root, root, new node(aft, X[i] - (root ? root->sz : 0)));
if (other) {
other->lazy--;
push(other);
}
merge(root, root, other);
//puts("NOW INORDER");inorder(root);
ans.pb(root->mx);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
6 ms |
504 KB |
Output is correct |
3 |
Correct |
10 ms |
708 KB |
Output is correct |
4 |
Correct |
12 ms |
808 KB |
Output is correct |
5 |
Correct |
9 ms |
896 KB |
Output is correct |
6 |
Correct |
12 ms |
960 KB |
Output is correct |
7 |
Correct |
10 ms |
1076 KB |
Output is correct |
8 |
Correct |
11 ms |
1088 KB |
Output is correct |
9 |
Correct |
8 ms |
1136 KB |
Output is correct |
10 |
Correct |
11 ms |
1312 KB |
Output is correct |
11 |
Correct |
11 ms |
1356 KB |
Output is correct |
12 |
Correct |
10 ms |
1356 KB |
Output is correct |
13 |
Correct |
9 ms |
1356 KB |
Output is correct |
14 |
Correct |
10 ms |
1356 KB |
Output is correct |
15 |
Correct |
10 ms |
1396 KB |
Output is correct |
16 |
Correct |
7 ms |
1424 KB |
Output is correct |
17 |
Correct |
8 ms |
1464 KB |
Output is correct |
18 |
Correct |
9 ms |
1504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
6 ms |
504 KB |
Output is correct |
3 |
Correct |
10 ms |
708 KB |
Output is correct |
4 |
Correct |
12 ms |
808 KB |
Output is correct |
5 |
Correct |
9 ms |
896 KB |
Output is correct |
6 |
Correct |
12 ms |
960 KB |
Output is correct |
7 |
Correct |
10 ms |
1076 KB |
Output is correct |
8 |
Correct |
11 ms |
1088 KB |
Output is correct |
9 |
Correct |
8 ms |
1136 KB |
Output is correct |
10 |
Correct |
11 ms |
1312 KB |
Output is correct |
11 |
Correct |
11 ms |
1356 KB |
Output is correct |
12 |
Correct |
10 ms |
1356 KB |
Output is correct |
13 |
Correct |
9 ms |
1356 KB |
Output is correct |
14 |
Correct |
10 ms |
1356 KB |
Output is correct |
15 |
Correct |
10 ms |
1396 KB |
Output is correct |
16 |
Correct |
7 ms |
1424 KB |
Output is correct |
17 |
Correct |
8 ms |
1464 KB |
Output is correct |
18 |
Correct |
9 ms |
1504 KB |
Output is correct |
19 |
Correct |
36 ms |
2312 KB |
Output is correct |
20 |
Correct |
42 ms |
2608 KB |
Output is correct |
21 |
Correct |
31 ms |
2800 KB |
Output is correct |
22 |
Correct |
35 ms |
2984 KB |
Output is correct |
23 |
Correct |
38 ms |
3132 KB |
Output is correct |
24 |
Correct |
32 ms |
3132 KB |
Output is correct |
25 |
Correct |
34 ms |
3132 KB |
Output is correct |
26 |
Correct |
27 ms |
3132 KB |
Output is correct |
27 |
Correct |
36 ms |
3252 KB |
Output is correct |
28 |
Correct |
35 ms |
3252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
4524 KB |
Output is correct |
2 |
Correct |
167 ms |
5952 KB |
Output is correct |
3 |
Correct |
235 ms |
7468 KB |
Output is correct |
4 |
Correct |
263 ms |
8008 KB |
Output is correct |
5 |
Correct |
228 ms |
8008 KB |
Output is correct |
6 |
Correct |
247 ms |
8008 KB |
Output is correct |
7 |
Correct |
228 ms |
8008 KB |
Output is correct |
8 |
Correct |
243 ms |
8008 KB |
Output is correct |
9 |
Correct |
342 ms |
8008 KB |
Output is correct |
10 |
Correct |
196 ms |
8008 KB |
Output is correct |
11 |
Correct |
177 ms |
8536 KB |
Output is correct |
12 |
Correct |
214 ms |
8936 KB |
Output is correct |
13 |
Correct |
155 ms |
8960 KB |
Output is correct |
14 |
Correct |
168 ms |
8960 KB |
Output is correct |
15 |
Correct |
165 ms |
9056 KB |
Output is correct |
16 |
Correct |
108 ms |
9056 KB |
Output is correct |
17 |
Correct |
125 ms |
9056 KB |
Output is correct |
18 |
Correct |
134 ms |
9056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
6 ms |
504 KB |
Output is correct |
3 |
Correct |
10 ms |
708 KB |
Output is correct |
4 |
Correct |
12 ms |
808 KB |
Output is correct |
5 |
Correct |
9 ms |
896 KB |
Output is correct |
6 |
Correct |
12 ms |
960 KB |
Output is correct |
7 |
Correct |
10 ms |
1076 KB |
Output is correct |
8 |
Correct |
11 ms |
1088 KB |
Output is correct |
9 |
Correct |
8 ms |
1136 KB |
Output is correct |
10 |
Correct |
11 ms |
1312 KB |
Output is correct |
11 |
Correct |
11 ms |
1356 KB |
Output is correct |
12 |
Correct |
10 ms |
1356 KB |
Output is correct |
13 |
Correct |
9 ms |
1356 KB |
Output is correct |
14 |
Correct |
10 ms |
1356 KB |
Output is correct |
15 |
Correct |
10 ms |
1396 KB |
Output is correct |
16 |
Correct |
7 ms |
1424 KB |
Output is correct |
17 |
Correct |
8 ms |
1464 KB |
Output is correct |
18 |
Correct |
9 ms |
1504 KB |
Output is correct |
19 |
Correct |
36 ms |
2312 KB |
Output is correct |
20 |
Correct |
42 ms |
2608 KB |
Output is correct |
21 |
Correct |
31 ms |
2800 KB |
Output is correct |
22 |
Correct |
35 ms |
2984 KB |
Output is correct |
23 |
Correct |
38 ms |
3132 KB |
Output is correct |
24 |
Correct |
32 ms |
3132 KB |
Output is correct |
25 |
Correct |
34 ms |
3132 KB |
Output is correct |
26 |
Correct |
27 ms |
3132 KB |
Output is correct |
27 |
Correct |
36 ms |
3252 KB |
Output is correct |
28 |
Correct |
35 ms |
3252 KB |
Output is correct |
29 |
Correct |
32 ms |
4524 KB |
Output is correct |
30 |
Correct |
167 ms |
5952 KB |
Output is correct |
31 |
Correct |
235 ms |
7468 KB |
Output is correct |
32 |
Correct |
263 ms |
8008 KB |
Output is correct |
33 |
Correct |
228 ms |
8008 KB |
Output is correct |
34 |
Correct |
247 ms |
8008 KB |
Output is correct |
35 |
Correct |
228 ms |
8008 KB |
Output is correct |
36 |
Correct |
243 ms |
8008 KB |
Output is correct |
37 |
Correct |
342 ms |
8008 KB |
Output is correct |
38 |
Correct |
196 ms |
8008 KB |
Output is correct |
39 |
Correct |
177 ms |
8536 KB |
Output is correct |
40 |
Correct |
214 ms |
8936 KB |
Output is correct |
41 |
Correct |
155 ms |
8960 KB |
Output is correct |
42 |
Correct |
168 ms |
8960 KB |
Output is correct |
43 |
Correct |
165 ms |
9056 KB |
Output is correct |
44 |
Correct |
108 ms |
9056 KB |
Output is correct |
45 |
Correct |
125 ms |
9056 KB |
Output is correct |
46 |
Correct |
134 ms |
9056 KB |
Output is correct |
47 |
Correct |
962 ms |
19976 KB |
Output is correct |
48 |
Correct |
4151 ms |
49320 KB |
Output is correct |
49 |
Correct |
4213 ms |
55024 KB |
Output is correct |
50 |
Correct |
4320 ms |
56128 KB |
Output is correct |
51 |
Correct |
4375 ms |
56900 KB |
Output is correct |
52 |
Correct |
4140 ms |
56900 KB |
Output is correct |
53 |
Correct |
4470 ms |
57012 KB |
Output is correct |
54 |
Correct |
4212 ms |
57012 KB |
Output is correct |
55 |
Correct |
4555 ms |
57012 KB |
Output is correct |
56 |
Correct |
3956 ms |
58388 KB |
Output is correct |
57 |
Correct |
4414 ms |
58452 KB |
Output is correct |
58 |
Correct |
4019 ms |
58452 KB |
Output is correct |
59 |
Correct |
3760 ms |
58456 KB |
Output is correct |
60 |
Correct |
3693 ms |
58456 KB |
Output is correct |
61 |
Correct |
3544 ms |
58456 KB |
Output is correct |
62 |
Correct |
3010 ms |
58876 KB |
Output is correct |
63 |
Correct |
3157 ms |
58876 KB |
Output is correct |
64 |
Correct |
3177 ms |
58876 KB |
Output is correct |
65 |
Correct |
2991 ms |
58876 KB |
Output is correct |
66 |
Correct |
3076 ms |
64748 KB |
Output is correct |
67 |
Correct |
2794 ms |
76080 KB |
Output is correct |