#include <bits/stdc++.h>
using namespace std;
const int N_MAX = 500000;
const int Q_MAX = 500000;
const int V_MAX = 1000000;
const int INF = INT_MAX / 2;
int N, Q;
int arr[N_MAX];
struct Query {
int pos;
int val;
};
Query queries[Q_MAX];
int V;
void compress () {
vector <pair <int, int>> aux;
for (int i = 0; i < N; i++) {
aux.push_back(make_pair(arr[i], i));
}
for (int i = 0; i < Q; i++) {
aux.push_back(make_pair(queries[i].val, queries[i].pos));
}
sort(aux.begin(), aux.end());
aux.resize(unique(aux.begin(), aux.end()) - aux.begin());
map <pair <int, int>, int> mp;
for (pair <int, int> p : aux) {
mp[p] = V++;
}
for (int i = 0; i < N; i++) {
arr[i] = mp[make_pair(arr[i], i)];
}
for (int i = 0; i < Q; i++) {
queries[i].val = mp[make_pair(queries[i].val, queries[i].pos)];
}
}
struct Info {
int cnt;
int maxVal;
int lazy;
void setVal (const int &y) {
maxVal = y;
cnt = (y != -INF);
}
};
Info operator + (const Info &x, const Info &y) {
return Info{x.cnt + y.cnt, max(x.maxVal, y.maxVal), 0};
}
void operator += (Info &x, const int &y) {
x.maxVal += y;
x.lazy += y;
}
Info SGT[V_MAX * 4 + 2];
void build (int node, int l, int r) {
if (l == r) {
SGT[node].setVal(-INF);
} else {
int mid = (l + r) / 2;
int lSon = node * 2, rSon = node * 2 + 1;
build(lSon, l, mid);
build(rSon, mid + 1, r);
SGT[node] = SGT[lSon] + SGT[rSon];
}
}
void build () {
build(1, 0, V - 1);
}
void updateAdd (int node, int l, int r, int ul, int ur, int uadd) {
if (ul <= l && r <= ur) {
SGT[node] += uadd;
} else {
int mid = (l + r) / 2;
int lSon = node * 2, rSon = node * 2 + 1;
SGT[lSon] += SGT[node].lazy;
SGT[rSon] += SGT[node].lazy;
if (ul <= mid) {
updateAdd(lSon, l, mid, ul, ur, uadd);
}
if (mid + 1 <= ur) {
updateAdd(rSon, mid + 1, r, ul, ur, uadd);
}
SGT[node] = SGT[lSon] + SGT[rSon];
}
}
void updateAdd (int ul, int ur, int uadd) {
updateAdd(1, 0, V - 1, ul, ur, uadd);
}
void updateSet (int node, int l, int r, int upos, int uset) {
if (l == r) {
SGT[node].setVal(uset);
} else {
int mid = (l + r) / 2;
int lSon = node * 2, rSon = node * 2 + 1;
SGT[lSon] += SGT[node].lazy;
SGT[rSon] += SGT[node].lazy;
if (upos <= mid) {
updateSet(lSon, l, mid, upos, uset);
} else {
updateSet(rSon, mid + 1, r, upos, uset);
}
SGT[node] = SGT[lSon] + SGT[rSon];
}
}
void updateSet (int upos, int uset) {
updateSet(1, 0, V - 1, upos, uset);
}
int queryCnt (int node, int l, int r, int qr) {
if (r <= qr) {
return SGT[node].cnt;
} else {
int mid = (l + r) / 2;
int lSon = node * 2, rSon = node * 2 + 1;
SGT[lSon] += SGT[node].lazy;
SGT[rSon] += SGT[node].lazy;
SGT[node].lazy = 0;
int cnt = queryCnt(lSon, l, mid, qr);
if (mid + 1 <= qr) {
cnt += queryCnt(rSon, mid + 1, r, qr);
}
return cnt;
}
}
int queryCnt (int qr) {
return queryCnt(1, 0, V - 1, qr);
}
vector <int> countScans (vector <int> _A, vector <int> _X, vector <int> _V) {
N = (int) _A.size(); Q = _X.size();
for (int i = 0; i < N; i++) {
arr[i] = _A[i];
}
for (int i = 0; i < Q; i++) {
queries[i] = Query{_X[i], _V[i]};
}
compress();
build();
for (int i = 0; i < N; i++) {
int smaller = queryCnt(arr[i]);
updateAdd(arr[i], V - 1, -1);
updateSet(arr[i], i - smaller);
}
vector <int> answer(Q);
for (int i = 0; i < Q; i++) {
updateSet(arr[queries[i].pos], -INF);
if (queries[i].val <= arr[queries[i].pos]) {
updateAdd(queries[i].val, arr[queries[i].pos], -1);
} else {
updateAdd(arr[queries[i].pos], queries[i].val, +1);
}
int smaller = queryCnt(queries[i].val);
updateSet(queries[i].val, queries[i].pos - smaller);
arr[queries[i].pos] = queries[i].val;
answer[i] = SGT[1].maxVal;
}
return answer;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
452 KB |
Output is correct |
3 |
Correct |
5 ms |
724 KB |
Output is correct |
4 |
Correct |
5 ms |
752 KB |
Output is correct |
5 |
Correct |
5 ms |
708 KB |
Output is correct |
6 |
Correct |
6 ms |
724 KB |
Output is correct |
7 |
Correct |
6 ms |
824 KB |
Output is correct |
8 |
Correct |
5 ms |
724 KB |
Output is correct |
9 |
Correct |
5 ms |
828 KB |
Output is correct |
10 |
Correct |
5 ms |
724 KB |
Output is correct |
11 |
Correct |
5 ms |
724 KB |
Output is correct |
12 |
Correct |
5 ms |
724 KB |
Output is correct |
13 |
Correct |
5 ms |
740 KB |
Output is correct |
14 |
Correct |
4 ms |
724 KB |
Output is correct |
15 |
Correct |
5 ms |
724 KB |
Output is correct |
16 |
Correct |
5 ms |
704 KB |
Output is correct |
17 |
Correct |
4 ms |
724 KB |
Output is correct |
18 |
Correct |
4 ms |
712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
452 KB |
Output is correct |
3 |
Correct |
5 ms |
724 KB |
Output is correct |
4 |
Correct |
5 ms |
752 KB |
Output is correct |
5 |
Correct |
5 ms |
708 KB |
Output is correct |
6 |
Correct |
6 ms |
724 KB |
Output is correct |
7 |
Correct |
6 ms |
824 KB |
Output is correct |
8 |
Correct |
5 ms |
724 KB |
Output is correct |
9 |
Correct |
5 ms |
828 KB |
Output is correct |
10 |
Correct |
5 ms |
724 KB |
Output is correct |
11 |
Correct |
5 ms |
724 KB |
Output is correct |
12 |
Correct |
5 ms |
724 KB |
Output is correct |
13 |
Correct |
5 ms |
740 KB |
Output is correct |
14 |
Correct |
4 ms |
724 KB |
Output is correct |
15 |
Correct |
5 ms |
724 KB |
Output is correct |
16 |
Correct |
5 ms |
704 KB |
Output is correct |
17 |
Correct |
4 ms |
724 KB |
Output is correct |
18 |
Correct |
4 ms |
712 KB |
Output is correct |
19 |
Correct |
21 ms |
2008 KB |
Output is correct |
20 |
Correct |
25 ms |
2184 KB |
Output is correct |
21 |
Correct |
30 ms |
2188 KB |
Output is correct |
22 |
Correct |
27 ms |
2184 KB |
Output is correct |
23 |
Correct |
21 ms |
2004 KB |
Output is correct |
24 |
Correct |
21 ms |
2044 KB |
Output is correct |
25 |
Correct |
20 ms |
1980 KB |
Output is correct |
26 |
Correct |
21 ms |
1980 KB |
Output is correct |
27 |
Correct |
20 ms |
1904 KB |
Output is correct |
28 |
Correct |
22 ms |
1908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
3304 KB |
Output is correct |
2 |
Correct |
93 ms |
6900 KB |
Output is correct |
3 |
Correct |
173 ms |
11892 KB |
Output is correct |
4 |
Correct |
210 ms |
11864 KB |
Output is correct |
5 |
Correct |
167 ms |
11896 KB |
Output is correct |
6 |
Correct |
172 ms |
11832 KB |
Output is correct |
7 |
Correct |
183 ms |
11788 KB |
Output is correct |
8 |
Correct |
161 ms |
11928 KB |
Output is correct |
9 |
Correct |
169 ms |
11900 KB |
Output is correct |
10 |
Correct |
132 ms |
7676 KB |
Output is correct |
11 |
Correct |
117 ms |
7676 KB |
Output is correct |
12 |
Correct |
119 ms |
7644 KB |
Output is correct |
13 |
Correct |
158 ms |
7600 KB |
Output is correct |
14 |
Correct |
116 ms |
7828 KB |
Output is correct |
15 |
Correct |
123 ms |
7680 KB |
Output is correct |
16 |
Correct |
109 ms |
7684 KB |
Output is correct |
17 |
Correct |
137 ms |
7560 KB |
Output is correct |
18 |
Correct |
109 ms |
7596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
452 KB |
Output is correct |
3 |
Correct |
5 ms |
724 KB |
Output is correct |
4 |
Correct |
5 ms |
752 KB |
Output is correct |
5 |
Correct |
5 ms |
708 KB |
Output is correct |
6 |
Correct |
6 ms |
724 KB |
Output is correct |
7 |
Correct |
6 ms |
824 KB |
Output is correct |
8 |
Correct |
5 ms |
724 KB |
Output is correct |
9 |
Correct |
5 ms |
828 KB |
Output is correct |
10 |
Correct |
5 ms |
724 KB |
Output is correct |
11 |
Correct |
5 ms |
724 KB |
Output is correct |
12 |
Correct |
5 ms |
724 KB |
Output is correct |
13 |
Correct |
5 ms |
740 KB |
Output is correct |
14 |
Correct |
4 ms |
724 KB |
Output is correct |
15 |
Correct |
5 ms |
724 KB |
Output is correct |
16 |
Correct |
5 ms |
704 KB |
Output is correct |
17 |
Correct |
4 ms |
724 KB |
Output is correct |
18 |
Correct |
4 ms |
712 KB |
Output is correct |
19 |
Correct |
21 ms |
2008 KB |
Output is correct |
20 |
Correct |
25 ms |
2184 KB |
Output is correct |
21 |
Correct |
30 ms |
2188 KB |
Output is correct |
22 |
Correct |
27 ms |
2184 KB |
Output is correct |
23 |
Correct |
21 ms |
2004 KB |
Output is correct |
24 |
Correct |
21 ms |
2044 KB |
Output is correct |
25 |
Correct |
20 ms |
1980 KB |
Output is correct |
26 |
Correct |
21 ms |
1980 KB |
Output is correct |
27 |
Correct |
20 ms |
1904 KB |
Output is correct |
28 |
Correct |
22 ms |
1908 KB |
Output is correct |
29 |
Correct |
38 ms |
3304 KB |
Output is correct |
30 |
Correct |
93 ms |
6900 KB |
Output is correct |
31 |
Correct |
173 ms |
11892 KB |
Output is correct |
32 |
Correct |
210 ms |
11864 KB |
Output is correct |
33 |
Correct |
167 ms |
11896 KB |
Output is correct |
34 |
Correct |
172 ms |
11832 KB |
Output is correct |
35 |
Correct |
183 ms |
11788 KB |
Output is correct |
36 |
Correct |
161 ms |
11928 KB |
Output is correct |
37 |
Correct |
169 ms |
11900 KB |
Output is correct |
38 |
Correct |
132 ms |
7676 KB |
Output is correct |
39 |
Correct |
117 ms |
7676 KB |
Output is correct |
40 |
Correct |
119 ms |
7644 KB |
Output is correct |
41 |
Correct |
158 ms |
7600 KB |
Output is correct |
42 |
Correct |
116 ms |
7828 KB |
Output is correct |
43 |
Correct |
123 ms |
7680 KB |
Output is correct |
44 |
Correct |
109 ms |
7684 KB |
Output is correct |
45 |
Correct |
137 ms |
7560 KB |
Output is correct |
46 |
Correct |
109 ms |
7596 KB |
Output is correct |
47 |
Correct |
796 ms |
40088 KB |
Output is correct |
48 |
Correct |
3137 ms |
110404 KB |
Output is correct |
49 |
Correct |
3302 ms |
118008 KB |
Output is correct |
50 |
Correct |
3314 ms |
118056 KB |
Output is correct |
51 |
Correct |
3315 ms |
118172 KB |
Output is correct |
52 |
Correct |
3318 ms |
118064 KB |
Output is correct |
53 |
Correct |
3283 ms |
118016 KB |
Output is correct |
54 |
Correct |
2803 ms |
118280 KB |
Output is correct |
55 |
Correct |
3193 ms |
118252 KB |
Output is correct |
56 |
Correct |
2899 ms |
118208 KB |
Output is correct |
57 |
Correct |
3133 ms |
118212 KB |
Output is correct |
58 |
Correct |
3035 ms |
118256 KB |
Output is correct |
59 |
Correct |
2835 ms |
109140 KB |
Output is correct |
60 |
Correct |
2958 ms |
109128 KB |
Output is correct |
61 |
Correct |
2834 ms |
108992 KB |
Output is correct |
62 |
Correct |
2645 ms |
104988 KB |
Output is correct |
63 |
Correct |
2792 ms |
105000 KB |
Output is correct |
64 |
Correct |
2985 ms |
105004 KB |
Output is correct |
65 |
Correct |
2532 ms |
100940 KB |
Output is correct |
66 |
Correct |
2496 ms |
100900 KB |
Output is correct |
67 |
Correct |
2472 ms |
100940 KB |
Output is correct |