#include<bits/stdc++.h>
using namespace std;
#define app push_back
#define ii pair <int, int>
#define mp make_pair
const int N = 1e6 + 7;
int mx[N << 2], add[N << 2];
void push(int v) {
if (add[v]) {
for (int i = v * 2 + 1; i <= v * 2 + 2; ++i) {
mx[i] += add[v]; add[i] += add[v];
}
add[v] = 0;
}
}
void relax(int v) {
mx[v] = max(mx[v * 2 + 1], mx[v * 2 + 2]);
}
void upd(int v, int tl, int tr, int p, int x) {
if (tl == tr) {
mx[v] = x;
return;
}
int tm = (tl + tr) >> 1;
push(v);
if (p <= tm) upd(v * 2 + 1, tl, tm, p, x);
else upd(v * 2 + 2, tm + 1, tr, p, x);
relax(v);
}
void add_(int v, int tl, int tr, int l, int r, int x) {
if (tr < l || r < tl) return;
if (l <= tl && tr <= r) {
mx[v] += x; add[v] += x;
return;
}
int tm = (tl + tr) >> 1;
push(v);
add_(v * 2 + 1, tl, tm, l, r, x); add_(v * 2 + 2, tm + 1, tr, l, r, x);
relax(v);
}
int f[N];
void addf(int i, int x) {
for (; i < N; i |= i + 1) f[i] += x;
}
int get(int i) {
int ans = 0;
for (; i >= 0; i &= i + 1, --i) ans += f[i];
return ans;
}
vector <int> countScans(vector <int> a, vector <int> p, vector <int> x) {
int n = a.size(), q = p.size();
vector <ii> v;
for (int i = 0; i < n; ++i) {
v.app({a[i], i});
}
for (int i = 0; i < q; ++i) {
v.app({x[i], p[i]});
}
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
for (int i = 0; i < n; ++i) {
a[i] = lower_bound(v.begin(), v.end(), mp(a[i], i)) - v.begin();
}
for (int i = 0; i < q; ++i) {
x[i] = lower_bound(v.begin(), v.end(), mp(x[i], p[i])) - v.begin();
}
for (int i = 0; i < (N << 2); ++i) mx[i] = -N;
vector <int> t;
for (int i = 0; i < n; ++i) {
t.app(a[i]);
}
sort(t.begin(), t.end()); t.resize(unique(t.begin(), t.end()) - t.begin());
for (int i = 0; i < n; ++i) {
int p = lower_bound(t.begin(), t.end(), a[i]) - t.begin();
upd(0, 0, N, a[i], i - p);
addf(a[i], 1);
}
vector <int> ans;
for (int qq = 0; qq < q; ++qq) {
int i = p[qq], v = x[qq];
upd(0, 0, N, a[i], -N);
addf(a[i], -1);
int pp = get(v);
upd(0, 0, N, v, i - pp);
addf(v, 1);
if (a[i] < v) {
add_(0, 0, N, min(a[i], v) + 1, max(a[i], v) - 1, 1);
}
else {
add_(0, 0, N, min(a[i], v) + 1, max(a[i], v) - 1, -1);
}
a[i] = v;
ans.app(mx[0]);
}
return ans;
}
#ifdef HOME
signed main() {
//freopen("input.txt", "r", stdin);
int n, q;
cin >> n >> q;
vector <int> a(n), p(q), x(q);
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < q; ++i) cin >> p[i] >> x[i];
vector <int> ans = countScans(a, p, x);
for (int e : ans) cout << e << '\n';
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
16120 KB |
Output is correct |
2 |
Correct |
18 ms |
16120 KB |
Output is correct |
3 |
Correct |
21 ms |
16252 KB |
Output is correct |
4 |
Correct |
20 ms |
16248 KB |
Output is correct |
5 |
Correct |
20 ms |
16248 KB |
Output is correct |
6 |
Correct |
20 ms |
16248 KB |
Output is correct |
7 |
Correct |
20 ms |
16248 KB |
Output is correct |
8 |
Correct |
21 ms |
16236 KB |
Output is correct |
9 |
Correct |
20 ms |
16220 KB |
Output is correct |
10 |
Correct |
20 ms |
16248 KB |
Output is correct |
11 |
Correct |
20 ms |
16248 KB |
Output is correct |
12 |
Correct |
20 ms |
16248 KB |
Output is correct |
13 |
Correct |
20 ms |
16248 KB |
Output is correct |
14 |
Correct |
20 ms |
16248 KB |
Output is correct |
15 |
Correct |
20 ms |
16248 KB |
Output is correct |
16 |
Correct |
20 ms |
16292 KB |
Output is correct |
17 |
Correct |
20 ms |
16248 KB |
Output is correct |
18 |
Correct |
20 ms |
16252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
16120 KB |
Output is correct |
2 |
Correct |
18 ms |
16120 KB |
Output is correct |
3 |
Correct |
21 ms |
16252 KB |
Output is correct |
4 |
Correct |
20 ms |
16248 KB |
Output is correct |
5 |
Correct |
20 ms |
16248 KB |
Output is correct |
6 |
Correct |
20 ms |
16248 KB |
Output is correct |
7 |
Correct |
20 ms |
16248 KB |
Output is correct |
8 |
Correct |
21 ms |
16236 KB |
Output is correct |
9 |
Correct |
20 ms |
16220 KB |
Output is correct |
10 |
Correct |
20 ms |
16248 KB |
Output is correct |
11 |
Correct |
20 ms |
16248 KB |
Output is correct |
12 |
Correct |
20 ms |
16248 KB |
Output is correct |
13 |
Correct |
20 ms |
16248 KB |
Output is correct |
14 |
Correct |
20 ms |
16248 KB |
Output is correct |
15 |
Correct |
20 ms |
16248 KB |
Output is correct |
16 |
Correct |
20 ms |
16292 KB |
Output is correct |
17 |
Correct |
20 ms |
16248 KB |
Output is correct |
18 |
Correct |
20 ms |
16252 KB |
Output is correct |
19 |
Correct |
34 ms |
16888 KB |
Output is correct |
20 |
Correct |
37 ms |
16932 KB |
Output is correct |
21 |
Correct |
35 ms |
16888 KB |
Output is correct |
22 |
Correct |
36 ms |
16888 KB |
Output is correct |
23 |
Correct |
35 ms |
16888 KB |
Output is correct |
24 |
Correct |
35 ms |
16888 KB |
Output is correct |
25 |
Correct |
34 ms |
16888 KB |
Output is correct |
26 |
Correct |
34 ms |
16888 KB |
Output is correct |
27 |
Correct |
34 ms |
16760 KB |
Output is correct |
28 |
Correct |
34 ms |
17016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
17140 KB |
Output is correct |
2 |
Correct |
95 ms |
18768 KB |
Output is correct |
3 |
Correct |
158 ms |
20516 KB |
Output is correct |
4 |
Correct |
158 ms |
20432 KB |
Output is correct |
5 |
Correct |
155 ms |
20588 KB |
Output is correct |
6 |
Correct |
158 ms |
20460 KB |
Output is correct |
7 |
Correct |
155 ms |
20460 KB |
Output is correct |
8 |
Correct |
154 ms |
20440 KB |
Output is correct |
9 |
Correct |
153 ms |
20436 KB |
Output is correct |
10 |
Correct |
124 ms |
20048 KB |
Output is correct |
11 |
Correct |
124 ms |
20044 KB |
Output is correct |
12 |
Correct |
122 ms |
19948 KB |
Output is correct |
13 |
Correct |
121 ms |
19952 KB |
Output is correct |
14 |
Correct |
121 ms |
19948 KB |
Output is correct |
15 |
Correct |
121 ms |
19948 KB |
Output is correct |
16 |
Correct |
120 ms |
19568 KB |
Output is correct |
17 |
Correct |
119 ms |
19636 KB |
Output is correct |
18 |
Correct |
119 ms |
19564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
16120 KB |
Output is correct |
2 |
Correct |
18 ms |
16120 KB |
Output is correct |
3 |
Correct |
21 ms |
16252 KB |
Output is correct |
4 |
Correct |
20 ms |
16248 KB |
Output is correct |
5 |
Correct |
20 ms |
16248 KB |
Output is correct |
6 |
Correct |
20 ms |
16248 KB |
Output is correct |
7 |
Correct |
20 ms |
16248 KB |
Output is correct |
8 |
Correct |
21 ms |
16236 KB |
Output is correct |
9 |
Correct |
20 ms |
16220 KB |
Output is correct |
10 |
Correct |
20 ms |
16248 KB |
Output is correct |
11 |
Correct |
20 ms |
16248 KB |
Output is correct |
12 |
Correct |
20 ms |
16248 KB |
Output is correct |
13 |
Correct |
20 ms |
16248 KB |
Output is correct |
14 |
Correct |
20 ms |
16248 KB |
Output is correct |
15 |
Correct |
20 ms |
16248 KB |
Output is correct |
16 |
Correct |
20 ms |
16292 KB |
Output is correct |
17 |
Correct |
20 ms |
16248 KB |
Output is correct |
18 |
Correct |
20 ms |
16252 KB |
Output is correct |
19 |
Correct |
34 ms |
16888 KB |
Output is correct |
20 |
Correct |
37 ms |
16932 KB |
Output is correct |
21 |
Correct |
35 ms |
16888 KB |
Output is correct |
22 |
Correct |
36 ms |
16888 KB |
Output is correct |
23 |
Correct |
35 ms |
16888 KB |
Output is correct |
24 |
Correct |
35 ms |
16888 KB |
Output is correct |
25 |
Correct |
34 ms |
16888 KB |
Output is correct |
26 |
Correct |
34 ms |
16888 KB |
Output is correct |
27 |
Correct |
34 ms |
16760 KB |
Output is correct |
28 |
Correct |
34 ms |
17016 KB |
Output is correct |
29 |
Correct |
41 ms |
17140 KB |
Output is correct |
30 |
Correct |
95 ms |
18768 KB |
Output is correct |
31 |
Correct |
158 ms |
20516 KB |
Output is correct |
32 |
Correct |
158 ms |
20432 KB |
Output is correct |
33 |
Correct |
155 ms |
20588 KB |
Output is correct |
34 |
Correct |
158 ms |
20460 KB |
Output is correct |
35 |
Correct |
155 ms |
20460 KB |
Output is correct |
36 |
Correct |
154 ms |
20440 KB |
Output is correct |
37 |
Correct |
153 ms |
20436 KB |
Output is correct |
38 |
Correct |
124 ms |
20048 KB |
Output is correct |
39 |
Correct |
124 ms |
20044 KB |
Output is correct |
40 |
Correct |
122 ms |
19948 KB |
Output is correct |
41 |
Correct |
121 ms |
19952 KB |
Output is correct |
42 |
Correct |
121 ms |
19948 KB |
Output is correct |
43 |
Correct |
121 ms |
19948 KB |
Output is correct |
44 |
Correct |
120 ms |
19568 KB |
Output is correct |
45 |
Correct |
119 ms |
19636 KB |
Output is correct |
46 |
Correct |
119 ms |
19564 KB |
Output is correct |
47 |
Correct |
521 ms |
31456 KB |
Output is correct |
48 |
Correct |
1936 ms |
62808 KB |
Output is correct |
49 |
Correct |
2145 ms |
66604 KB |
Output is correct |
50 |
Correct |
2169 ms |
66516 KB |
Output is correct |
51 |
Correct |
2143 ms |
66652 KB |
Output is correct |
52 |
Correct |
2157 ms |
66692 KB |
Output is correct |
53 |
Correct |
2117 ms |
66796 KB |
Output is correct |
54 |
Correct |
1956 ms |
66724 KB |
Output is correct |
55 |
Correct |
2074 ms |
66820 KB |
Output is correct |
56 |
Correct |
1909 ms |
66804 KB |
Output is correct |
57 |
Correct |
2001 ms |
66684 KB |
Output is correct |
58 |
Correct |
1933 ms |
66760 KB |
Output is correct |
59 |
Correct |
1757 ms |
64028 KB |
Output is correct |
60 |
Correct |
1772 ms |
64036 KB |
Output is correct |
61 |
Correct |
1757 ms |
64084 KB |
Output is correct |
62 |
Correct |
1680 ms |
63184 KB |
Output is correct |
63 |
Correct |
1708 ms |
63128 KB |
Output is correct |
64 |
Correct |
1706 ms |
63028 KB |
Output is correct |
65 |
Correct |
1617 ms |
62244 KB |
Output is correct |
66 |
Correct |
1596 ms |
62264 KB |
Output is correct |
67 |
Correct |
1602 ms |
62328 KB |
Output is correct |