/**
* author: wxhtzdy
* created: 10.09.2023 18:28:11
**/
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class fenwick {
public:
vector<T> fenw;
int n;
fenwick(int _n) : n(_n) {
fenw.resize(n);
}
void modify(int x, T v) {
while (x < n) {
fenw[x] += v;
x |= (x + 1);
}
}
T get(int x) {
T v{};
while (x >= 0) {
v += fenw[x];
x = (x & (x + 1)) - 1;
}
return v;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> op(q);
vector<int> x(q);
vector<int> y(q);
for (int i = 0; i < q; i++) {
cin >> op[i];
if (op[i] == 1) {
cin >> x[i];
} else {
cin >> x[i] >> y[i];
--x[i];
}
}
vector<int> xs;
for (int i = 0; i < n; i++) {
xs.push_back(a[i]);
}
for (int i = 0; i < q; i++) {
if (op[i] == 1) {
xs.push_back(x[i]);
} else {
xs.push_back(y[i]);
}
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
int sz = (int) xs.size();
for (int i = 0; i < n; i++) {
a[i] = (int) (lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin());
}
for (int i = 0; i < q; i++) {
if (op[i] == 1) {
x[i] = (int) (lower_bound(xs.begin(), xs.end(), x[i]) - xs.begin());
} else {
y[i] = (int) (lower_bound(xs.begin(), xs.end(), y[i]) - xs.begin());
}
}
fenwick<int> f(sz);
function<void(int, int)> Add = [&](int i, int x) {
if (i < 0 || i >= n) {
return;
}
f.modify(a[i], x);
if (i > 0) {
f.modify(min(a[i - 1], a[i]), -x);
}
};
for (int i = 0; i < n; i++) {
Add(i, +1);
}
for (int i = 0; i < q; i++) {
if (op[i] == 1) {
cout << f.get(sz - 1) - f.get(x[i] - 1) << '\n';
} else {
Add(x[i], -1);
Add(x[i] + 1, -1);
a[x[i]] = y[i];
Add(x[i], +1);
Add(x[i] + 1, +1);
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
508 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
600 KB |
Output is correct |
3 |
Correct |
2 ms |
600 KB |
Output is correct |
4 |
Correct |
12 ms |
1372 KB |
Output is correct |
5 |
Correct |
22 ms |
2240 KB |
Output is correct |
6 |
Correct |
33 ms |
3288 KB |
Output is correct |
7 |
Correct |
50 ms |
4812 KB |
Output is correct |
8 |
Correct |
66 ms |
6048 KB |
Output is correct |
9 |
Correct |
119 ms |
10048 KB |
Output is correct |
10 |
Correct |
100 ms |
8392 KB |
Output is correct |
11 |
Correct |
142 ms |
10956 KB |
Output is correct |
12 |
Correct |
143 ms |
11976 KB |
Output is correct |
13 |
Correct |
141 ms |
11988 KB |
Output is correct |
14 |
Correct |
139 ms |
11724 KB |
Output is correct |
15 |
Correct |
141 ms |
11816 KB |
Output is correct |
16 |
Correct |
146 ms |
12232 KB |
Output is correct |
17 |
Correct |
145 ms |
12276 KB |
Output is correct |
18 |
Correct |
152 ms |
12236 KB |
Output is correct |
19 |
Correct |
145 ms |
12232 KB |
Output is correct |
20 |
Correct |
148 ms |
12236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
508 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
600 KB |
Output is correct |
18 |
Correct |
2 ms |
600 KB |
Output is correct |
19 |
Correct |
12 ms |
1372 KB |
Output is correct |
20 |
Correct |
22 ms |
2240 KB |
Output is correct |
21 |
Correct |
33 ms |
3288 KB |
Output is correct |
22 |
Correct |
50 ms |
4812 KB |
Output is correct |
23 |
Correct |
66 ms |
6048 KB |
Output is correct |
24 |
Correct |
119 ms |
10048 KB |
Output is correct |
25 |
Correct |
100 ms |
8392 KB |
Output is correct |
26 |
Correct |
142 ms |
10956 KB |
Output is correct |
27 |
Correct |
143 ms |
11976 KB |
Output is correct |
28 |
Correct |
141 ms |
11988 KB |
Output is correct |
29 |
Correct |
139 ms |
11724 KB |
Output is correct |
30 |
Correct |
141 ms |
11816 KB |
Output is correct |
31 |
Correct |
146 ms |
12232 KB |
Output is correct |
32 |
Correct |
145 ms |
12276 KB |
Output is correct |
33 |
Correct |
152 ms |
12236 KB |
Output is correct |
34 |
Correct |
145 ms |
12232 KB |
Output is correct |
35 |
Correct |
148 ms |
12236 KB |
Output is correct |
36 |
Correct |
2 ms |
344 KB |
Output is correct |
37 |
Correct |
2 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
39 |
Correct |
11 ms |
1372 KB |
Output is correct |
40 |
Correct |
21 ms |
2216 KB |
Output is correct |
41 |
Correct |
36 ms |
3284 KB |
Output is correct |
42 |
Correct |
52 ms |
4388 KB |
Output is correct |
43 |
Correct |
69 ms |
5836 KB |
Output is correct |
44 |
Correct |
136 ms |
9976 KB |
Output is correct |
45 |
Correct |
107 ms |
8396 KB |
Output is correct |
46 |
Correct |
117 ms |
9160 KB |
Output is correct |
47 |
Correct |
157 ms |
11980 KB |
Output is correct |
48 |
Correct |
169 ms |
12120 KB |
Output is correct |
49 |
Correct |
162 ms |
12080 KB |
Output is correct |
50 |
Correct |
161 ms |
11824 KB |
Output is correct |
51 |
Correct |
173 ms |
12280 KB |
Output is correct |
52 |
Correct |
192 ms |
12268 KB |
Output is correct |
53 |
Correct |
186 ms |
12168 KB |
Output is correct |
54 |
Correct |
190 ms |
12240 KB |
Output is correct |
55 |
Correct |
178 ms |
12148 KB |
Output is correct |