#include "bubblesort2.h"
#include <bits/stdc++.h>
using namespace std;
struct Tag {
int add;
Tag(int _add) : add(_add) {}
Tag() : add(0) {}
void apply(Tag &tag) { add += tag.add; }
};
struct Info {
int len, sum;
Info(int _len, int _sum) : len(_len), sum(_sum) {}
Info() : len(0), sum(0) {}
void apply(Tag &tag) { sum += tag.add * len; }
Info combine(Info &other) { return {len + other.len, sum + other.sum}; }
};
struct Tree {
vector<Tag> tag;
vector<Info> info;
Tree(int size) {
tag.resize(size*4);
info.resize(size*4);
}
void build(vector<Info> &base, int x, int xl, int xr) {
if (xl == xr) {
info[x] = base[xl];
} else {
int xm = (xl + xr) / 2;
build(base, x*2, xl, xm);
build(base, x*2+1, xm+1, xr);
info[x] = info[x*2].combine(info[x*2+1]);
}
}
void pushdown(int x) {
info[x].apply(tag[x]);
if (x*2 < (int) tag.size()) tag[x*2].apply(tag[x]);
if (x*2+1 < (int) tag.size()) tag[x*2+1].apply(tag[x]);
tag[x] = {};
}
void update(int l, int r, int x, int xl, int xr, Tag delta) {
if (l > r) return;
pushdown(x);
if (l == xl && r == xr) {
tag[x].apply(delta);
} else {
int xm = (xl + xr) / 2;
update(l, min(r, xm), x*2, xl, xm, delta);
update(max(l, xm+1), r, x*2+1, xm+1, xr, delta);
pushdown(x); pushdown(x*2); pushdown(x*2+1);
info[x] = info[x*2].combine(info[x*2+1]);
}
}
Info query(int l, int r, int x, int xl, int xr) {
if (l > r) return {};
pushdown(x);
if (l == xl && r == xr) {
return info[x];
} else {
int xm = (xl + xr) / 2;
Info left = query(l, min(r, xm), x*2, xl, xm);
Info right = query(max(l, xm+1), r, x*2+1, xm+1, xr);
return left.combine(right);
}
}
};
// need to only query from a number if it is smaller than everything
// to the right of it
// i.e. pick the rightmost item, then the next smallest and next and so on
// if it is smaller than everything to the right, then i can
// count the number of items above it and subtract it from how much stuff is
// on the right, to get it ans
// in other words, this can be done if we use a segment tree to count number below x
// we can update one of these important values
// now we just need to be able to identify and track how these values change
// for now let's just see if i can get 60 points
vector<int> countScans(vector<int> A, vector<int> X, vector<int> V) {
int N = A.size();
int Q = X.size();
set<int> AV;
for (int a : A) AV.insert(a);
for (int v : V) AV.insert(v);
map<int,int> idxmap; int T = 0;
for (int av : AV) idxmap[av] = T++;
Tree tree(T);
vector<Info> base(T, {1, 0});
tree.build(base, 1, 0, T-1);
vector<set<int>> idx(T);
for (int i=0; i<N; i++) {
A[i] = idxmap[A[i]];
idx[A[i]].insert(i);
tree.update(0, A[i], 1, 0, T-1, {+1});
}
vector<int> ans(Q);
for (int i=0; i<Q; i++) {
idx[A[X[i]]].erase(X[i]);
tree.update(0, A[X[i]], 1, 0, T-1, {-1});
A[X[i]] = idxmap[V[i]];
tree.update(0, A[X[i]], 1, 0, T-1, {+1});
idx[A[X[i]]].insert(X[i]);
int prev = 0;
for (int j=0; j<T-1; j++) {
if (idx[j].empty()) continue;
int k = *idx[j].rbegin();
if (k < prev) continue;
// cout << tree.query(j+1, j+1, 1, 0, T-1).sum << "," << (N-1 - k) << "\n";
ans[i] = max(ans[i], tree.query(j+1, j+1, 1, 0, T-1).sum - (N-1 - k));
prev = k;
}
// cout << "--\n";
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
2 |
Correct |
6 ms |
604 KB |
Output is correct |
3 |
Correct |
30 ms |
1368 KB |
Output is correct |
4 |
Correct |
30 ms |
1408 KB |
Output is correct |
5 |
Correct |
63 ms |
1372 KB |
Output is correct |
6 |
Correct |
347 ms |
1368 KB |
Output is correct |
7 |
Correct |
292 ms |
1368 KB |
Output is correct |
8 |
Correct |
218 ms |
1368 KB |
Output is correct |
9 |
Correct |
50 ms |
1368 KB |
Output is correct |
10 |
Correct |
173 ms |
1128 KB |
Output is correct |
11 |
Correct |
173 ms |
1112 KB |
Output is correct |
12 |
Correct |
172 ms |
1116 KB |
Output is correct |
13 |
Correct |
168 ms |
1112 KB |
Output is correct |
14 |
Correct |
195 ms |
1220 KB |
Output is correct |
15 |
Correct |
169 ms |
1244 KB |
Output is correct |
16 |
Correct |
166 ms |
1116 KB |
Output is correct |
17 |
Correct |
165 ms |
1116 KB |
Output is correct |
18 |
Correct |
164 ms |
1212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
2 |
Correct |
6 ms |
604 KB |
Output is correct |
3 |
Correct |
30 ms |
1368 KB |
Output is correct |
4 |
Correct |
30 ms |
1408 KB |
Output is correct |
5 |
Correct |
63 ms |
1372 KB |
Output is correct |
6 |
Correct |
347 ms |
1368 KB |
Output is correct |
7 |
Correct |
292 ms |
1368 KB |
Output is correct |
8 |
Correct |
218 ms |
1368 KB |
Output is correct |
9 |
Correct |
50 ms |
1368 KB |
Output is correct |
10 |
Correct |
173 ms |
1128 KB |
Output is correct |
11 |
Correct |
173 ms |
1112 KB |
Output is correct |
12 |
Correct |
172 ms |
1116 KB |
Output is correct |
13 |
Correct |
168 ms |
1112 KB |
Output is correct |
14 |
Correct |
195 ms |
1220 KB |
Output is correct |
15 |
Correct |
169 ms |
1244 KB |
Output is correct |
16 |
Correct |
166 ms |
1116 KB |
Output is correct |
17 |
Correct |
165 ms |
1116 KB |
Output is correct |
18 |
Correct |
164 ms |
1212 KB |
Output is correct |
19 |
Correct |
510 ms |
3920 KB |
Output is correct |
20 |
Correct |
663 ms |
4348 KB |
Output is correct |
21 |
Correct |
5115 ms |
4344 KB |
Output is correct |
22 |
Correct |
1879 ms |
4348 KB |
Output is correct |
23 |
Correct |
3658 ms |
3932 KB |
Output is correct |
24 |
Correct |
3636 ms |
3932 KB |
Output is correct |
25 |
Correct |
3613 ms |
3724 KB |
Output is correct |
26 |
Correct |
3504 ms |
3724 KB |
Output is correct |
27 |
Correct |
3404 ms |
3528 KB |
Output is correct |
28 |
Correct |
3416 ms |
3516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
1880 KB |
Output is correct |
2 |
Correct |
44 ms |
3416 KB |
Output is correct |
3 |
Correct |
82 ms |
4924 KB |
Output is correct |
4 |
Correct |
80 ms |
4696 KB |
Output is correct |
5 |
Correct |
121 ms |
4444 KB |
Output is correct |
6 |
Correct |
106 ms |
4696 KB |
Output is correct |
7 |
Correct |
228 ms |
4680 KB |
Output is correct |
8 |
Correct |
179 ms |
4676 KB |
Output is correct |
9 |
Correct |
165 ms |
4428 KB |
Output is correct |
10 |
Correct |
206 ms |
4444 KB |
Output is correct |
11 |
Correct |
206 ms |
4680 KB |
Output is correct |
12 |
Correct |
205 ms |
4444 KB |
Output is correct |
13 |
Correct |
206 ms |
4444 KB |
Output is correct |
14 |
Correct |
216 ms |
4676 KB |
Output is correct |
15 |
Correct |
211 ms |
4444 KB |
Output is correct |
16 |
Correct |
203 ms |
4656 KB |
Output is correct |
17 |
Correct |
205 ms |
4696 KB |
Output is correct |
18 |
Correct |
207 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
2 |
Correct |
6 ms |
604 KB |
Output is correct |
3 |
Correct |
30 ms |
1368 KB |
Output is correct |
4 |
Correct |
30 ms |
1408 KB |
Output is correct |
5 |
Correct |
63 ms |
1372 KB |
Output is correct |
6 |
Correct |
347 ms |
1368 KB |
Output is correct |
7 |
Correct |
292 ms |
1368 KB |
Output is correct |
8 |
Correct |
218 ms |
1368 KB |
Output is correct |
9 |
Correct |
50 ms |
1368 KB |
Output is correct |
10 |
Correct |
173 ms |
1128 KB |
Output is correct |
11 |
Correct |
173 ms |
1112 KB |
Output is correct |
12 |
Correct |
172 ms |
1116 KB |
Output is correct |
13 |
Correct |
168 ms |
1112 KB |
Output is correct |
14 |
Correct |
195 ms |
1220 KB |
Output is correct |
15 |
Correct |
169 ms |
1244 KB |
Output is correct |
16 |
Correct |
166 ms |
1116 KB |
Output is correct |
17 |
Correct |
165 ms |
1116 KB |
Output is correct |
18 |
Correct |
164 ms |
1212 KB |
Output is correct |
19 |
Correct |
510 ms |
3920 KB |
Output is correct |
20 |
Correct |
663 ms |
4348 KB |
Output is correct |
21 |
Correct |
5115 ms |
4344 KB |
Output is correct |
22 |
Correct |
1879 ms |
4348 KB |
Output is correct |
23 |
Correct |
3658 ms |
3932 KB |
Output is correct |
24 |
Correct |
3636 ms |
3932 KB |
Output is correct |
25 |
Correct |
3613 ms |
3724 KB |
Output is correct |
26 |
Correct |
3504 ms |
3724 KB |
Output is correct |
27 |
Correct |
3404 ms |
3528 KB |
Output is correct |
28 |
Correct |
3416 ms |
3516 KB |
Output is correct |
29 |
Correct |
13 ms |
1880 KB |
Output is correct |
30 |
Correct |
44 ms |
3416 KB |
Output is correct |
31 |
Correct |
82 ms |
4924 KB |
Output is correct |
32 |
Correct |
80 ms |
4696 KB |
Output is correct |
33 |
Correct |
121 ms |
4444 KB |
Output is correct |
34 |
Correct |
106 ms |
4696 KB |
Output is correct |
35 |
Correct |
228 ms |
4680 KB |
Output is correct |
36 |
Correct |
179 ms |
4676 KB |
Output is correct |
37 |
Correct |
165 ms |
4428 KB |
Output is correct |
38 |
Correct |
206 ms |
4444 KB |
Output is correct |
39 |
Correct |
206 ms |
4680 KB |
Output is correct |
40 |
Correct |
205 ms |
4444 KB |
Output is correct |
41 |
Correct |
206 ms |
4444 KB |
Output is correct |
42 |
Correct |
216 ms |
4676 KB |
Output is correct |
43 |
Correct |
211 ms |
4444 KB |
Output is correct |
44 |
Correct |
203 ms |
4656 KB |
Output is correct |
45 |
Correct |
205 ms |
4696 KB |
Output is correct |
46 |
Correct |
207 ms |
4444 KB |
Output is correct |
47 |
Execution timed out |
9105 ms |
72168 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |