#include "bubblesort2.h"
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "
const int MAX_N = 1e6 + 10;
struct Node {
int val, lazy, cnt;
Node() {val = lazy = cnt = 0;}
Node operator +(const Node &other) const {
Node ret;
ret.val = max(val, other.val);
ret.cnt = cnt + other.cnt;
if(ret.cnt == 0) {
ret.val = 0;
}
return ret;
}
};
struct SegTree {
Node tree[4 * MAX_N];
void push(int curr, int l, int r) {
tree[curr].val += tree[curr].lazy;
if(tree[curr].cnt == 0) {
tree[curr].val = 0;
}
if(l != r) {
tree[curr * 2].lazy += tree[curr].lazy;
tree[curr * 2 + 1].lazy += tree[curr].lazy;
}
tree[curr].lazy = 0;
}
void updPos(int curr, int l, int r, int ind, int val) {
push(curr, l, r);
if(l == r && l == ind) {
tree[curr].cnt += val;
if(tree[curr].cnt == 0) {
tree[curr].val = 0;
}
return;
} else if(l > ind || r < ind) {
return;
}
int m = (l + r) / 2ll;
updPos(curr * 2, l, m, ind, val);
updPos(curr * 2 + 1, m + 1, r, ind, val);
tree[curr] = tree[curr * 2] + tree[curr * 2 + 1];
}
void upd(int curr, int l, int r, int ql, int qr, int val) {
push(curr, l, r);
if(ql <= l && r <= qr) {
tree[curr].lazy += val;
push(curr, l, r);
return;
} else if(l > qr || r < ql) {
return;
}
int m = (l + r) / 2ll;
upd(curr * 2, l, m, ql, qr, val);
upd(curr * 2 + 1, m + 1, r, ql, qr, val);
tree[curr] = tree[curr * 2] + tree[curr * 2 + 1];
}
int ansPos(int curr, int l, int r, int ql, int qr) {
push(curr, l, r);
if(ql <= l && r <= qr) {
return tree[curr].cnt;
} else if(l > qr || r < ql) {
return 0;
}
int m = (l + r) / 2ll;
return ansPos(curr * 2, l, m, ql, qr) + ansPos(curr * 2 + 1, m + 1, r, ql, qr);
}
int ans(int curr, int l, int r, int ql, int qr) {
push(curr, l, r);
if(ql <= l && r <= qr) {
return tree[curr].val;
} else if(l > qr || r < ql) {
return 0;
}
int m = (l + r) / 2ll;
return max(ans(curr * 2, l, m, ql, qr), ans(curr * 2 + 1, m + 1, r, ql, qr));
}
};
SegTree ans;
vector<int> countScans(vector<int> A, vector<int> X, vector<int> V){
int Q = X.size(), N = A.size();
vector<int> answer(Q);
vector<pair<int, int> > cord;
for(int i = 0; i < N; i ++) {
cord.push_back({A[i], i});
}
for(int i = 0; i < Q; i ++) {
cord.push_back({V[i], X[i]});
}
sort(cord.begin(), cord.end());
cord.resize(unique(cord.begin(), cord.end()) - cord.begin());
for(int i = 0; i < N; i ++) {
A[i] = lower_bound(cord.begin(), cord.end(), make_pair(A[i], i)) - cord.begin() + 1;
}
for(int i = 0; i < Q; i ++) {
V[i] = lower_bound(cord.begin(), cord.end(), make_pair(V[i], X[i])) - cord.begin() + 1;
}
for(int i = 0; i < N; i ++) {
ans.updPos(1, 0, MAX_N - 1, A[i], 1);
}
for(int i = 0; i < N; i ++) {
int smaller = ans.ansPos(1, 0, MAX_N - 1, 0, A[i] - 1);
ans.upd(1, 0, MAX_N - 1, A[i], A[i], i - smaller);
}
for(int i = 0; i < Q; i ++) {
ans.updPos(1, 0, MAX_N - 1, A[X[i]], -1);
ans.upd(1, 0, MAX_N - 1, A[X[i]] + 1, MAX_N - 1, 1);
A[X[i]] = V[i];
ans.updPos(1, 0, MAX_N - 1, A[X[i]], 1);
int smaller = ans.ansPos(1, 0, MAX_N - 1, 0, V[i] - 1);
ans.upd(1, 0, MAX_N - 1, V[i], V[i], X[i] - smaller);
ans.upd(1, 0, MAX_N - 1, V[i] + 1, MAX_N - 1, -1);
answer[i] = ans.ans(1, 0, MAX_N - 1, 0, MAX_N - 1);
}
return answer;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
47332 KB |
Output is correct |
2 |
Correct |
35 ms |
47340 KB |
Output is correct |
3 |
Correct |
42 ms |
47460 KB |
Output is correct |
4 |
Correct |
43 ms |
47584 KB |
Output is correct |
5 |
Correct |
41 ms |
47460 KB |
Output is correct |
6 |
Correct |
40 ms |
47468 KB |
Output is correct |
7 |
Correct |
40 ms |
47460 KB |
Output is correct |
8 |
Correct |
40 ms |
47460 KB |
Output is correct |
9 |
Correct |
41 ms |
47460 KB |
Output is correct |
10 |
Correct |
40 ms |
47460 KB |
Output is correct |
11 |
Correct |
45 ms |
47468 KB |
Output is correct |
12 |
Correct |
40 ms |
47460 KB |
Output is correct |
13 |
Correct |
41 ms |
47468 KB |
Output is correct |
14 |
Correct |
41 ms |
47512 KB |
Output is correct |
15 |
Correct |
40 ms |
47460 KB |
Output is correct |
16 |
Correct |
40 ms |
47460 KB |
Output is correct |
17 |
Correct |
40 ms |
47460 KB |
Output is correct |
18 |
Correct |
40 ms |
47460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
47332 KB |
Output is correct |
2 |
Correct |
35 ms |
47340 KB |
Output is correct |
3 |
Correct |
42 ms |
47460 KB |
Output is correct |
4 |
Correct |
43 ms |
47584 KB |
Output is correct |
5 |
Correct |
41 ms |
47460 KB |
Output is correct |
6 |
Correct |
40 ms |
47468 KB |
Output is correct |
7 |
Correct |
40 ms |
47460 KB |
Output is correct |
8 |
Correct |
40 ms |
47460 KB |
Output is correct |
9 |
Correct |
41 ms |
47460 KB |
Output is correct |
10 |
Correct |
40 ms |
47460 KB |
Output is correct |
11 |
Correct |
45 ms |
47468 KB |
Output is correct |
12 |
Correct |
40 ms |
47460 KB |
Output is correct |
13 |
Correct |
41 ms |
47468 KB |
Output is correct |
14 |
Correct |
41 ms |
47512 KB |
Output is correct |
15 |
Correct |
40 ms |
47460 KB |
Output is correct |
16 |
Correct |
40 ms |
47460 KB |
Output is correct |
17 |
Correct |
40 ms |
47460 KB |
Output is correct |
18 |
Correct |
40 ms |
47460 KB |
Output is correct |
19 |
Correct |
68 ms |
47972 KB |
Output is correct |
20 |
Correct |
73 ms |
47972 KB |
Output is correct |
21 |
Correct |
70 ms |
47980 KB |
Output is correct |
22 |
Correct |
71 ms |
47980 KB |
Output is correct |
23 |
Correct |
72 ms |
48024 KB |
Output is correct |
24 |
Correct |
71 ms |
48096 KB |
Output is correct |
25 |
Correct |
75 ms |
48020 KB |
Output is correct |
26 |
Correct |
71 ms |
47972 KB |
Output is correct |
27 |
Correct |
68 ms |
47972 KB |
Output is correct |
28 |
Correct |
68 ms |
47844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
47972 KB |
Output is correct |
2 |
Correct |
184 ms |
48992 KB |
Output is correct |
3 |
Correct |
299 ms |
50396 KB |
Output is correct |
4 |
Correct |
302 ms |
50396 KB |
Output is correct |
5 |
Correct |
301 ms |
50392 KB |
Output is correct |
6 |
Correct |
292 ms |
50396 KB |
Output is correct |
7 |
Correct |
304 ms |
50396 KB |
Output is correct |
8 |
Correct |
315 ms |
50396 KB |
Output is correct |
9 |
Correct |
294 ms |
50396 KB |
Output is correct |
10 |
Correct |
263 ms |
50528 KB |
Output is correct |
11 |
Correct |
280 ms |
50652 KB |
Output is correct |
12 |
Correct |
264 ms |
50524 KB |
Output is correct |
13 |
Correct |
262 ms |
50524 KB |
Output is correct |
14 |
Correct |
283 ms |
50524 KB |
Output is correct |
15 |
Correct |
263 ms |
50524 KB |
Output is correct |
16 |
Correct |
263 ms |
50524 KB |
Output is correct |
17 |
Correct |
258 ms |
50524 KB |
Output is correct |
18 |
Correct |
258 ms |
50524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
47332 KB |
Output is correct |
2 |
Correct |
35 ms |
47340 KB |
Output is correct |
3 |
Correct |
42 ms |
47460 KB |
Output is correct |
4 |
Correct |
43 ms |
47584 KB |
Output is correct |
5 |
Correct |
41 ms |
47460 KB |
Output is correct |
6 |
Correct |
40 ms |
47468 KB |
Output is correct |
7 |
Correct |
40 ms |
47460 KB |
Output is correct |
8 |
Correct |
40 ms |
47460 KB |
Output is correct |
9 |
Correct |
41 ms |
47460 KB |
Output is correct |
10 |
Correct |
40 ms |
47460 KB |
Output is correct |
11 |
Correct |
45 ms |
47468 KB |
Output is correct |
12 |
Correct |
40 ms |
47460 KB |
Output is correct |
13 |
Correct |
41 ms |
47468 KB |
Output is correct |
14 |
Correct |
41 ms |
47512 KB |
Output is correct |
15 |
Correct |
40 ms |
47460 KB |
Output is correct |
16 |
Correct |
40 ms |
47460 KB |
Output is correct |
17 |
Correct |
40 ms |
47460 KB |
Output is correct |
18 |
Correct |
40 ms |
47460 KB |
Output is correct |
19 |
Correct |
68 ms |
47972 KB |
Output is correct |
20 |
Correct |
73 ms |
47972 KB |
Output is correct |
21 |
Correct |
70 ms |
47980 KB |
Output is correct |
22 |
Correct |
71 ms |
47980 KB |
Output is correct |
23 |
Correct |
72 ms |
48024 KB |
Output is correct |
24 |
Correct |
71 ms |
48096 KB |
Output is correct |
25 |
Correct |
75 ms |
48020 KB |
Output is correct |
26 |
Correct |
71 ms |
47972 KB |
Output is correct |
27 |
Correct |
68 ms |
47972 KB |
Output is correct |
28 |
Correct |
68 ms |
47844 KB |
Output is correct |
29 |
Correct |
81 ms |
47972 KB |
Output is correct |
30 |
Correct |
184 ms |
48992 KB |
Output is correct |
31 |
Correct |
299 ms |
50396 KB |
Output is correct |
32 |
Correct |
302 ms |
50396 KB |
Output is correct |
33 |
Correct |
301 ms |
50392 KB |
Output is correct |
34 |
Correct |
292 ms |
50396 KB |
Output is correct |
35 |
Correct |
304 ms |
50396 KB |
Output is correct |
36 |
Correct |
315 ms |
50396 KB |
Output is correct |
37 |
Correct |
294 ms |
50396 KB |
Output is correct |
38 |
Correct |
263 ms |
50528 KB |
Output is correct |
39 |
Correct |
280 ms |
50652 KB |
Output is correct |
40 |
Correct |
264 ms |
50524 KB |
Output is correct |
41 |
Correct |
262 ms |
50524 KB |
Output is correct |
42 |
Correct |
283 ms |
50524 KB |
Output is correct |
43 |
Correct |
263 ms |
50524 KB |
Output is correct |
44 |
Correct |
263 ms |
50524 KB |
Output is correct |
45 |
Correct |
258 ms |
50524 KB |
Output is correct |
46 |
Correct |
258 ms |
50524 KB |
Output is correct |
47 |
Correct |
947 ms |
59220 KB |
Output is correct |
48 |
Correct |
3548 ms |
80428 KB |
Output is correct |
49 |
Correct |
3815 ms |
82416 KB |
Output is correct |
50 |
Correct |
3762 ms |
82384 KB |
Output is correct |
51 |
Correct |
3775 ms |
82384 KB |
Output is correct |
52 |
Correct |
3725 ms |
82256 KB |
Output is correct |
53 |
Correct |
3702 ms |
82256 KB |
Output is correct |
54 |
Correct |
3510 ms |
82448 KB |
Output is correct |
55 |
Correct |
3690 ms |
82496 KB |
Output is correct |
56 |
Correct |
3321 ms |
82376 KB |
Output is correct |
57 |
Correct |
3469 ms |
82384 KB |
Output is correct |
58 |
Correct |
3266 ms |
82384 KB |
Output is correct |
59 |
Correct |
3223 ms |
81088 KB |
Output is correct |
60 |
Correct |
3354 ms |
81232 KB |
Output is correct |
61 |
Correct |
3257 ms |
81196 KB |
Output is correct |
62 |
Correct |
3115 ms |
80980 KB |
Output is correct |
63 |
Correct |
3100 ms |
80896 KB |
Output is correct |
64 |
Correct |
3121 ms |
80972 KB |
Output is correct |
65 |
Correct |
2974 ms |
80848 KB |
Output is correct |
66 |
Correct |
2900 ms |
80900 KB |
Output is correct |
67 |
Correct |
2889 ms |
81024 KB |
Output is correct |