#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 1e6 + 5;
int maxn[MAXN * 4 + 10], lazy[MAXN * 4 + 10];
void pushup(int rt) { maxn[rt] = max(maxn[rt << 1], maxn[rt << 1 | 1]); }
void pushdown(int rt) {
if (lazy[rt]) {
maxn[rt << 1] += lazy[rt], maxn[rt << 1 | 1] += lazy[rt];
lazy[rt << 1] += lazy[rt], lazy[rt << 1 | 1] += lazy[rt];
lazy[rt] = 0;
}
}
void update(int L, int R, int I, int l = 1, int r = MAXN, int rt = 1) {
if (L <= l && r <= R) {
maxn[rt] += I, lazy[rt] += I;
return;
}
int m = (l + r) / 2;
pushdown(rt);
if (L <= m)
update(L, R, I, l, m, rt << 1);
if (R > m)
update(L, R, I, m + 1, r, rt << 1 | 1);
pushup(rt);
}
int query(int L, int R, int l = 1, int r = MAXN, int rt = 1) {
if (L <= l && r <= R)
return maxn[rt];
int m = (l + r) / 2, ans = -1e9, placed = 0;
pushdown(rt);
if (L <= m) {
ans = query(L, R, l, m, rt << 1);
placed = 1;
}
if (R > m) {
if(placed) ans = max(ans, query(L, R, m + 1, r, rt << 1 | 1));
else ans = query(L, R, m + 1, r, rt << 1 | 1);
}
return ans;
}
void p_update(int x, int I, int len = MAXN) {
int v = query(x, x);
update(x, x, I - v);
}
typedef pair<int, int> pr;
#define mp make_pair
pr pool[MAXN];
int tot;
struct BIT {
int t[MAXN + 20];
void update(int x, int I) {
while (x <= MAXN)
t[x] += I, x += x & -x;
}
int query(int x) {
int ans = 0;
while (x)
ans += t[x], x -= x & -x;
return ans;
}
} T;
void compress(int &x, int id) {
x = lower_bound(pool + 1, pool + tot + 1, mp(x, id)) - pool;
}
std::vector<int> countScans(std::vector<int> A, std::vector<int> X,
std::vector<int> V) {
int Q = X.size(), N = A.size();
std::vector<int> answer(Q);
for (int i = 0; i < N; i++)
pool[++tot] = mp(A[i], i);
for (int i = 0; i < Q; i++)
pool[++tot] = mp(V[i], X[i]);
sort(pool + 1, pool + tot + 1);
tot = unique(pool + 1, pool + tot + 1) - pool - 1;
// for (i = 1; i <= tot; i++) {
// printf("%d %d\n",pool[i].first,pool[i].second);
// }
for (int i = 0; i < N; i++)
compress(A[i], i), T.update(A[i], 1);
for (int i = 0; i < Q; i++)
compress(V[i], X[i]);
for (int i = 0; i < 4 * MAXN + 10; i++)
maxn[i] = -1e7;
for (int i = 0; i < N; i++) {
p_update(A[i], i - T.query(A[i] - 1));
// cerr<<"ID"<<A[i]<<"EXPECTED"<<i - T.query(A[i] - 1)<<endl;
}
// cerr << "Compressed Results:" << endl;
// for (int i = 0; i < N; i++)
// cerr << A[i] << " \n"[i == N - 1];
// for (int i = 0; i < Q; i++)
// cerr << X[i] << " " << V[i] << endl;
for (int i = 0; i < Q; i++) {
int src = A[X[i]];
// cerr << "SGT - before:" << endl;
// for (int i = 1; i <= 7; i++)
// cerr << query(i, i) << " \n"[i == 7];
if (src == V[i]) {
answer[i] = query(1, MAXN);
continue;
}
T.update(src, -1);
T.update(V[i], 1);
p_update(src, -1e7);
p_update(V[i], X[i] - T.query(V[i] - 1));
if (src < V[i]) {
if (src + 1 <= V[i] - 1)
update(src + 1, V[i] - 1, 1);
} else {
if (V[i] + 1 <= src - 1)
update(V[i] + 1, src - 1, -1);
}
A[X[i]] = V[i];
answer[i] = query(1, MAXN);
}
return answer;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16084 KB |
Output is correct |
2 |
Correct |
13 ms |
16104 KB |
Output is correct |
3 |
Correct |
13 ms |
16188 KB |
Output is correct |
4 |
Correct |
14 ms |
16192 KB |
Output is correct |
5 |
Correct |
14 ms |
16240 KB |
Output is correct |
6 |
Correct |
13 ms |
16200 KB |
Output is correct |
7 |
Correct |
13 ms |
16212 KB |
Output is correct |
8 |
Correct |
12 ms |
16156 KB |
Output is correct |
9 |
Correct |
13 ms |
16128 KB |
Output is correct |
10 |
Correct |
12 ms |
16212 KB |
Output is correct |
11 |
Correct |
14 ms |
16092 KB |
Output is correct |
12 |
Correct |
28 ms |
16212 KB |
Output is correct |
13 |
Correct |
16 ms |
16112 KB |
Output is correct |
14 |
Correct |
16 ms |
16092 KB |
Output is correct |
15 |
Correct |
13 ms |
16212 KB |
Output is correct |
16 |
Correct |
12 ms |
16212 KB |
Output is correct |
17 |
Correct |
12 ms |
16120 KB |
Output is correct |
18 |
Correct |
12 ms |
16212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16084 KB |
Output is correct |
2 |
Correct |
13 ms |
16104 KB |
Output is correct |
3 |
Correct |
13 ms |
16188 KB |
Output is correct |
4 |
Correct |
14 ms |
16192 KB |
Output is correct |
5 |
Correct |
14 ms |
16240 KB |
Output is correct |
6 |
Correct |
13 ms |
16200 KB |
Output is correct |
7 |
Correct |
13 ms |
16212 KB |
Output is correct |
8 |
Correct |
12 ms |
16156 KB |
Output is correct |
9 |
Correct |
13 ms |
16128 KB |
Output is correct |
10 |
Correct |
12 ms |
16212 KB |
Output is correct |
11 |
Correct |
14 ms |
16092 KB |
Output is correct |
12 |
Correct |
28 ms |
16212 KB |
Output is correct |
13 |
Correct |
16 ms |
16112 KB |
Output is correct |
14 |
Correct |
16 ms |
16092 KB |
Output is correct |
15 |
Correct |
13 ms |
16212 KB |
Output is correct |
16 |
Correct |
12 ms |
16212 KB |
Output is correct |
17 |
Correct |
12 ms |
16120 KB |
Output is correct |
18 |
Correct |
12 ms |
16212 KB |
Output is correct |
19 |
Correct |
24 ms |
16680 KB |
Output is correct |
20 |
Correct |
26 ms |
16768 KB |
Output is correct |
21 |
Correct |
32 ms |
16724 KB |
Output is correct |
22 |
Correct |
27 ms |
16724 KB |
Output is correct |
23 |
Correct |
27 ms |
16672 KB |
Output is correct |
24 |
Correct |
31 ms |
16820 KB |
Output is correct |
25 |
Correct |
26 ms |
16712 KB |
Output is correct |
26 |
Correct |
25 ms |
16692 KB |
Output is correct |
27 |
Correct |
26 ms |
16668 KB |
Output is correct |
28 |
Correct |
26 ms |
16620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
16808 KB |
Output is correct |
2 |
Correct |
83 ms |
18380 KB |
Output is correct |
3 |
Correct |
130 ms |
19968 KB |
Output is correct |
4 |
Correct |
141 ms |
19940 KB |
Output is correct |
5 |
Correct |
150 ms |
19980 KB |
Output is correct |
6 |
Correct |
150 ms |
19864 KB |
Output is correct |
7 |
Correct |
134 ms |
19916 KB |
Output is correct |
8 |
Correct |
125 ms |
19824 KB |
Output is correct |
9 |
Correct |
129 ms |
19816 KB |
Output is correct |
10 |
Correct |
92 ms |
19304 KB |
Output is correct |
11 |
Correct |
89 ms |
19344 KB |
Output is correct |
12 |
Correct |
84 ms |
19388 KB |
Output is correct |
13 |
Correct |
98 ms |
19312 KB |
Output is correct |
14 |
Correct |
109 ms |
19288 KB |
Output is correct |
15 |
Correct |
102 ms |
19532 KB |
Output is correct |
16 |
Correct |
110 ms |
19328 KB |
Output is correct |
17 |
Correct |
111 ms |
19404 KB |
Output is correct |
18 |
Correct |
110 ms |
19308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16084 KB |
Output is correct |
2 |
Correct |
13 ms |
16104 KB |
Output is correct |
3 |
Correct |
13 ms |
16188 KB |
Output is correct |
4 |
Correct |
14 ms |
16192 KB |
Output is correct |
5 |
Correct |
14 ms |
16240 KB |
Output is correct |
6 |
Correct |
13 ms |
16200 KB |
Output is correct |
7 |
Correct |
13 ms |
16212 KB |
Output is correct |
8 |
Correct |
12 ms |
16156 KB |
Output is correct |
9 |
Correct |
13 ms |
16128 KB |
Output is correct |
10 |
Correct |
12 ms |
16212 KB |
Output is correct |
11 |
Correct |
14 ms |
16092 KB |
Output is correct |
12 |
Correct |
28 ms |
16212 KB |
Output is correct |
13 |
Correct |
16 ms |
16112 KB |
Output is correct |
14 |
Correct |
16 ms |
16092 KB |
Output is correct |
15 |
Correct |
13 ms |
16212 KB |
Output is correct |
16 |
Correct |
12 ms |
16212 KB |
Output is correct |
17 |
Correct |
12 ms |
16120 KB |
Output is correct |
18 |
Correct |
12 ms |
16212 KB |
Output is correct |
19 |
Correct |
24 ms |
16680 KB |
Output is correct |
20 |
Correct |
26 ms |
16768 KB |
Output is correct |
21 |
Correct |
32 ms |
16724 KB |
Output is correct |
22 |
Correct |
27 ms |
16724 KB |
Output is correct |
23 |
Correct |
27 ms |
16672 KB |
Output is correct |
24 |
Correct |
31 ms |
16820 KB |
Output is correct |
25 |
Correct |
26 ms |
16712 KB |
Output is correct |
26 |
Correct |
25 ms |
16692 KB |
Output is correct |
27 |
Correct |
26 ms |
16668 KB |
Output is correct |
28 |
Correct |
26 ms |
16620 KB |
Output is correct |
29 |
Correct |
36 ms |
16808 KB |
Output is correct |
30 |
Correct |
83 ms |
18380 KB |
Output is correct |
31 |
Correct |
130 ms |
19968 KB |
Output is correct |
32 |
Correct |
141 ms |
19940 KB |
Output is correct |
33 |
Correct |
150 ms |
19980 KB |
Output is correct |
34 |
Correct |
150 ms |
19864 KB |
Output is correct |
35 |
Correct |
134 ms |
19916 KB |
Output is correct |
36 |
Correct |
125 ms |
19824 KB |
Output is correct |
37 |
Correct |
129 ms |
19816 KB |
Output is correct |
38 |
Correct |
92 ms |
19304 KB |
Output is correct |
39 |
Correct |
89 ms |
19344 KB |
Output is correct |
40 |
Correct |
84 ms |
19388 KB |
Output is correct |
41 |
Correct |
98 ms |
19312 KB |
Output is correct |
42 |
Correct |
109 ms |
19288 KB |
Output is correct |
43 |
Correct |
102 ms |
19532 KB |
Output is correct |
44 |
Correct |
110 ms |
19328 KB |
Output is correct |
45 |
Correct |
111 ms |
19404 KB |
Output is correct |
46 |
Correct |
110 ms |
19308 KB |
Output is correct |
47 |
Correct |
447 ms |
29628 KB |
Output is correct |
48 |
Correct |
1685 ms |
53680 KB |
Output is correct |
49 |
Correct |
1866 ms |
62460 KB |
Output is correct |
50 |
Correct |
1825 ms |
62536 KB |
Output is correct |
51 |
Correct |
1821 ms |
62632 KB |
Output is correct |
52 |
Correct |
1747 ms |
62508 KB |
Output is correct |
53 |
Correct |
1749 ms |
62508 KB |
Output is correct |
54 |
Correct |
1796 ms |
62664 KB |
Output is correct |
55 |
Correct |
1863 ms |
62792 KB |
Output is correct |
56 |
Correct |
1579 ms |
62680 KB |
Output is correct |
57 |
Correct |
1706 ms |
62680 KB |
Output is correct |
58 |
Correct |
1591 ms |
62716 KB |
Output is correct |
59 |
Correct |
1535 ms |
59960 KB |
Output is correct |
60 |
Correct |
1509 ms |
59836 KB |
Output is correct |
61 |
Correct |
1563 ms |
59848 KB |
Output is correct |
62 |
Correct |
1491 ms |
58964 KB |
Output is correct |
63 |
Correct |
1464 ms |
59028 KB |
Output is correct |
64 |
Correct |
1463 ms |
59016 KB |
Output is correct |
65 |
Correct |
1448 ms |
58072 KB |
Output is correct |
66 |
Correct |
1407 ms |
58052 KB |
Output is correct |
67 |
Correct |
1436 ms |
58140 KB |
Output is correct |