#include "bubblesort2.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = (1 << 21) + 1;
int iDeb[MAXN], iFin[MAXN], maxVal[MAXN], lazyAdd[MAXN];
set<int> ids[MAXN];
int curDelta[MAXN];
void pull(int iNoeud) {
maxVal[iNoeud] = max(maxVal[2 * iNoeud], maxVal[2 * iNoeud + 1]);
}
void push(int iNoeud) {
if (!lazyAdd[iNoeud])
return;
maxVal[iNoeud] += lazyAdd[iNoeud];
if (iDeb[iNoeud] < iFin[iNoeud]) {
lazyAdd[2 * iNoeud] += lazyAdd[iNoeud];
lazyAdd[1 + 2 * iNoeud] += lazyAdd[iNoeud];
} else {
curDelta[iDeb[iNoeud]] += lazyAdd[iNoeud];
}
lazyAdd[iNoeud] = 0;
}
void buildTree(int iNoeud, int deb, int fin) {
iDeb[iNoeud] = deb, iFin[iNoeud] = fin;
maxVal[iNoeud] = -1e9;
if (deb == fin)
return;
buildTree(2 * iNoeud, deb, (deb + fin) / 2);
buildTree(2 * iNoeud + 1, (deb + fin) / 2 + 1, fin);
}
void change(int iNoeud, int value, int id, bool del) {
push(iNoeud);
if (iDeb[iNoeud] > value or iFin[iNoeud] < value)
return;
if (iDeb[iNoeud] == iFin[iNoeud]) {
if (del)
ids[value].erase(id);
else
ids[value].insert(id);
maxVal[iNoeud] =
(ids[value].empty() ? -1e9 : *ids[value].rbegin()) + curDelta[value];
return;
}
change(2 * iNoeud, value, id, del);
change(2 * iNoeud + 1, value, id, del);
pull(iNoeud);
}
void add(int iNoeud, int deb, int fin, int val) {
push(iNoeud);
if (iDeb[iNoeud] > fin or iFin[iNoeud] < deb)
return;
if (iDeb[iNoeud] >= deb and iFin[iNoeud] <= fin) {
lazyAdd[iNoeud] += val;
push(iNoeud);
return;
}
add(2 * iNoeud, deb, fin, val);
add(1 + 2 * iNoeud, deb, fin, val);
pull(iNoeud);
}
int query(int iNoeud, int deb, int fin) {
push(iNoeud);
if (deb > iFin[iNoeud] or fin < iDeb[iNoeud])
return -1e9;
if (deb <= iDeb[iNoeud] and fin >= iFin[iNoeud])
return maxVal[iNoeud];
return max(query(2 * iNoeud, deb, fin), query(2 * iNoeud + 1, deb, fin));
}
vector<int> countScans(vector<int> A, vector<int> X, vector<int> V) {
vector<int> values;
for (auto v : A)
values.push_back(v);
for (auto v : V)
values.push_back(v);
sort(values.begin(), values.end());
values.resize(unique(values.begin(), values.end()) - values.begin());
for (auto &v : A)
v = lower_bound(values.begin(), values.end(), v) - values.begin();
for (auto &v : V)
v = lower_bound(values.begin(), values.end(), v) - values.begin();
int nbVal = values.size();
buildTree(1, 0, nbVal - 1);
for (int i = 0; i < (int)A.size(); ++i)
change(1, A[i], i + 1, false);
for (int i = 0; i < (int)A.size(); ++i) {
add(1, A[i], nbVal - 1, -1);
}
/*cout << "-------------\n";
for (int k = 0; k < nbVal; ++k)
cout << query(1, k, k) << ' ';
cout << endl;
for (int k = 0; k < (int)A.size(); ++k)
cout << curDelta[k] << ' ';
cout << endl;
for (int k = 0; k < nbVal; ++k)
cout << (ids[k].empty() ? -1 : *ids[k].rbegin()) << ' ';
cout << endl;*/
int Q = X.size();
vector<int> answer(Q);
for (int j = 0; j < Q; j++) {
add(1, A[X[j]], nbVal - 1, 1);
change(1, A[X[j]], X[j] + 1, true);
change(1, V[j], X[j] + 1, false);
A[X[j]] = V[j];
add(1, A[X[j]], nbVal - 1, -1);
/*cout << "-------------\n";
for (int k = 0; k < nbVal; ++k)
cout << query(1, k, k) << ' ';
cout << endl;
for (int k = 0; k < (int)A.size(); ++k)
cout << curDelta[k] << ' ';
cout << endl;
for (int k = 0; k < nbVal; ++k)
cout << (ids[k].empty() ? -1 : *ids[k].rbegin()) << ' ';
cout << endl;*/
answer[j] = query(1, 0, nbVal - 1);
}
return answer;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
98812 KB |
Output is correct |
2 |
Correct |
58 ms |
99032 KB |
Output is correct |
3 |
Correct |
61 ms |
99064 KB |
Output is correct |
4 |
Correct |
61 ms |
99172 KB |
Output is correct |
5 |
Correct |
61 ms |
99140 KB |
Output is correct |
6 |
Correct |
61 ms |
99288 KB |
Output is correct |
7 |
Correct |
62 ms |
99076 KB |
Output is correct |
8 |
Correct |
62 ms |
99180 KB |
Output is correct |
9 |
Correct |
61 ms |
99048 KB |
Output is correct |
10 |
Correct |
60 ms |
99096 KB |
Output is correct |
11 |
Correct |
61 ms |
99056 KB |
Output is correct |
12 |
Correct |
60 ms |
99060 KB |
Output is correct |
13 |
Correct |
60 ms |
99140 KB |
Output is correct |
14 |
Correct |
61 ms |
99048 KB |
Output is correct |
15 |
Correct |
64 ms |
99084 KB |
Output is correct |
16 |
Correct |
60 ms |
99060 KB |
Output is correct |
17 |
Correct |
62 ms |
99140 KB |
Output is correct |
18 |
Correct |
61 ms |
99072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
98812 KB |
Output is correct |
2 |
Correct |
58 ms |
99032 KB |
Output is correct |
3 |
Correct |
61 ms |
99064 KB |
Output is correct |
4 |
Correct |
61 ms |
99172 KB |
Output is correct |
5 |
Correct |
61 ms |
99140 KB |
Output is correct |
6 |
Correct |
61 ms |
99288 KB |
Output is correct |
7 |
Correct |
62 ms |
99076 KB |
Output is correct |
8 |
Correct |
62 ms |
99180 KB |
Output is correct |
9 |
Correct |
61 ms |
99048 KB |
Output is correct |
10 |
Correct |
60 ms |
99096 KB |
Output is correct |
11 |
Correct |
61 ms |
99056 KB |
Output is correct |
12 |
Correct |
60 ms |
99060 KB |
Output is correct |
13 |
Correct |
60 ms |
99140 KB |
Output is correct |
14 |
Correct |
61 ms |
99048 KB |
Output is correct |
15 |
Correct |
64 ms |
99084 KB |
Output is correct |
16 |
Correct |
60 ms |
99060 KB |
Output is correct |
17 |
Correct |
62 ms |
99140 KB |
Output is correct |
18 |
Correct |
61 ms |
99072 KB |
Output is correct |
19 |
Correct |
84 ms |
100164 KB |
Output is correct |
20 |
Correct |
85 ms |
100160 KB |
Output is correct |
21 |
Correct |
83 ms |
100256 KB |
Output is correct |
22 |
Correct |
83 ms |
100160 KB |
Output is correct |
23 |
Correct |
78 ms |
100292 KB |
Output is correct |
24 |
Correct |
78 ms |
100136 KB |
Output is correct |
25 |
Correct |
81 ms |
100292 KB |
Output is correct |
26 |
Correct |
84 ms |
100196 KB |
Output is correct |
27 |
Correct |
76 ms |
100180 KB |
Output is correct |
28 |
Correct |
82 ms |
100104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
100468 KB |
Output is correct |
2 |
Correct |
120 ms |
101972 KB |
Output is correct |
3 |
Correct |
172 ms |
103400 KB |
Output is correct |
4 |
Correct |
169 ms |
103484 KB |
Output is correct |
5 |
Correct |
166 ms |
103448 KB |
Output is correct |
6 |
Correct |
166 ms |
103444 KB |
Output is correct |
7 |
Correct |
176 ms |
103384 KB |
Output is correct |
8 |
Correct |
164 ms |
103484 KB |
Output is correct |
9 |
Correct |
161 ms |
103492 KB |
Output is correct |
10 |
Correct |
144 ms |
103612 KB |
Output is correct |
11 |
Correct |
147 ms |
103520 KB |
Output is correct |
12 |
Correct |
141 ms |
103540 KB |
Output is correct |
13 |
Correct |
138 ms |
103552 KB |
Output is correct |
14 |
Correct |
143 ms |
103580 KB |
Output is correct |
15 |
Correct |
136 ms |
103484 KB |
Output is correct |
16 |
Correct |
133 ms |
103456 KB |
Output is correct |
17 |
Correct |
132 ms |
103544 KB |
Output is correct |
18 |
Correct |
135 ms |
103612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
98812 KB |
Output is correct |
2 |
Correct |
58 ms |
99032 KB |
Output is correct |
3 |
Correct |
61 ms |
99064 KB |
Output is correct |
4 |
Correct |
61 ms |
99172 KB |
Output is correct |
5 |
Correct |
61 ms |
99140 KB |
Output is correct |
6 |
Correct |
61 ms |
99288 KB |
Output is correct |
7 |
Correct |
62 ms |
99076 KB |
Output is correct |
8 |
Correct |
62 ms |
99180 KB |
Output is correct |
9 |
Correct |
61 ms |
99048 KB |
Output is correct |
10 |
Correct |
60 ms |
99096 KB |
Output is correct |
11 |
Correct |
61 ms |
99056 KB |
Output is correct |
12 |
Correct |
60 ms |
99060 KB |
Output is correct |
13 |
Correct |
60 ms |
99140 KB |
Output is correct |
14 |
Correct |
61 ms |
99048 KB |
Output is correct |
15 |
Correct |
64 ms |
99084 KB |
Output is correct |
16 |
Correct |
60 ms |
99060 KB |
Output is correct |
17 |
Correct |
62 ms |
99140 KB |
Output is correct |
18 |
Correct |
61 ms |
99072 KB |
Output is correct |
19 |
Correct |
84 ms |
100164 KB |
Output is correct |
20 |
Correct |
85 ms |
100160 KB |
Output is correct |
21 |
Correct |
83 ms |
100256 KB |
Output is correct |
22 |
Correct |
83 ms |
100160 KB |
Output is correct |
23 |
Correct |
78 ms |
100292 KB |
Output is correct |
24 |
Correct |
78 ms |
100136 KB |
Output is correct |
25 |
Correct |
81 ms |
100292 KB |
Output is correct |
26 |
Correct |
84 ms |
100196 KB |
Output is correct |
27 |
Correct |
76 ms |
100180 KB |
Output is correct |
28 |
Correct |
82 ms |
100104 KB |
Output is correct |
29 |
Correct |
78 ms |
100468 KB |
Output is correct |
30 |
Correct |
120 ms |
101972 KB |
Output is correct |
31 |
Correct |
172 ms |
103400 KB |
Output is correct |
32 |
Correct |
169 ms |
103484 KB |
Output is correct |
33 |
Correct |
166 ms |
103448 KB |
Output is correct |
34 |
Correct |
166 ms |
103444 KB |
Output is correct |
35 |
Correct |
176 ms |
103384 KB |
Output is correct |
36 |
Correct |
164 ms |
103484 KB |
Output is correct |
37 |
Correct |
161 ms |
103492 KB |
Output is correct |
38 |
Correct |
144 ms |
103612 KB |
Output is correct |
39 |
Correct |
147 ms |
103520 KB |
Output is correct |
40 |
Correct |
141 ms |
103540 KB |
Output is correct |
41 |
Correct |
138 ms |
103552 KB |
Output is correct |
42 |
Correct |
143 ms |
103580 KB |
Output is correct |
43 |
Correct |
136 ms |
103484 KB |
Output is correct |
44 |
Correct |
133 ms |
103456 KB |
Output is correct |
45 |
Correct |
132 ms |
103544 KB |
Output is correct |
46 |
Correct |
135 ms |
103612 KB |
Output is correct |
47 |
Correct |
951 ms |
132656 KB |
Output is correct |
48 |
Correct |
3576 ms |
183568 KB |
Output is correct |
49 |
Correct |
3955 ms |
189520 KB |
Output is correct |
50 |
Correct |
4003 ms |
189540 KB |
Output is correct |
51 |
Correct |
3942 ms |
189704 KB |
Output is correct |
52 |
Correct |
3877 ms |
189468 KB |
Output is correct |
53 |
Correct |
3898 ms |
189496 KB |
Output is correct |
54 |
Correct |
3492 ms |
189776 KB |
Output is correct |
55 |
Correct |
3716 ms |
189756 KB |
Output is correct |
56 |
Correct |
3321 ms |
189648 KB |
Output is correct |
57 |
Correct |
3551 ms |
189672 KB |
Output is correct |
58 |
Correct |
3403 ms |
189640 KB |
Output is correct |
59 |
Correct |
3022 ms |
187876 KB |
Output is correct |
60 |
Correct |
3009 ms |
187852 KB |
Output is correct |
61 |
Correct |
3017 ms |
187800 KB |
Output is correct |
62 |
Correct |
2875 ms |
187436 KB |
Output is correct |
63 |
Correct |
2860 ms |
187416 KB |
Output is correct |
64 |
Correct |
2878 ms |
187488 KB |
Output is correct |
65 |
Correct |
2670 ms |
187056 KB |
Output is correct |
66 |
Correct |
2620 ms |
187132 KB |
Output is correct |
67 |
Correct |
2642 ms |
187052 KB |
Output is correct |