#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = array<lint, 2>;
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
const int MAXT = 270000;
pi operator+(pi a, pi b) {
if (a > b)
swap(a, b);
if (b[0] == a[0])
b[1] += a[1];
return b;
}
struct seg {
pi tree[MAXT];
int lazy[MAXT];
void lazydown(int p) {
for (int i = 2 * p; i < 2 * p + 2; i++) {
lazy[i] += lazy[p];
tree[i][0] += lazy[p];
}
lazy[p] = 0;
}
void init(int s, int e, int p) {
lazy[p] = 0;
if (s == e) {
tree[p] = {0, 1};
return;
}
int m = (s + e) / 2;
init(s, m, 2 * p);
init(m + 1, e, 2 * p + 1);
tree[p] = tree[2 * p] + tree[2 * p + 1];
}
void upd(int pos, int ps, int pe, int p, pi v) {
if (ps == pe) {
tree[p] = v;
return;
}
lazydown(p);
int pm = (ps + pe) / 2;
if (pos <= pm)
upd(pos, ps, pm, 2 * p, v);
else
upd(pos, pm + 1, pe, 2 * p + 1, v);
tree[p] = tree[2 * p] + tree[2 * p + 1];
}
void add(int s, int e, int ps, int pe, int p, int v) {
if (e < ps || pe < s)
return;
if (s <= ps && pe <= e) {
lazy[p] += v;
tree[p][0] += v;
return;
}
lazydown(p);
int pm = (ps + pe) / 2;
add(s, e, ps, pm, 2 * p, v);
add(s, e, pm + 1, pe, 2 * p + 1, v);
tree[p] = tree[2 * p] + tree[2 * p + 1];
}
pi query() { return tree[1]; }
} seg;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k, q;
cin >> n >> k >> q;
vector<int> a1(n), a2(n);
vector<int> r1(n), r2(n);
for (int i = 0; i < n; i++) {
cin >> a1[i];
a1[i]--;
r1[a1[i]] = i;
}
for (int i = 0; i < n; i++) {
cin >> a2[i];
a2[i]--;
r2[a2[i]] = i;
}
vector<vector<array<int, 3>>> swp(n + 1);
vector<vector<array<int, 4>>> temp(n + 1);
vector<pi> queries(q + 1);
for (int i = 0; i < n; i++) {
int x1 = max(k - 1, r1[i]), x2 = min(r1[i] + k, n);
int y1 = max(k - 1, r2[i]), y2 = min(r2[i] + k, n);
swp[x1].push_back({y1, y2, +1});
swp[x2].push_back({y1, y2, -1});
}
auto rangeAdd = [&](int p, int l, int r, int d, int ts) {
if (p < k - 1 || p >= n)
return;
l = max(l, k - 1);
r = min(r, n);
temp[p].push_back({ts, l, r, d});
};
for (int i = 1; i <= q; i++) {
int t;
cin >> t;
t--;
int p1 = a1[t], p2 = a1[t + 1];
swap(a1[t], a1[t + 1]);
rangeAdd(r1[p1], r2[p1], r2[p1] + k, -1, i);
rangeAdd(r1[p1] + k, r2[p1], r2[p1] + k, +1, i);
rangeAdd(r1[p2] + k - 1, r2[p2], r2[p2] + k, -1, i);
rangeAdd(r1[p2] - 1, r2[p2], r2[p2] + k, +1, i);
r1[p1]++;
r1[p2]--;
}
seg.init(k - 1, n - 1, 1);
auto rangeAdd2 = [&](int l, int r, int d) { seg.add(l, r - 1, k - 1, n - 1, 1, d); };
auto query = [&]() { return seg.tree[1]; };
vector<vector<array<lint, 3>>> haha(q + 1);
for (int i = k - 1; i < n; i++) {
for (auto &x : swp[i]) {
rangeAdd2(x[0], x[1], x[2]);
}
temp[i].push_back({q + 1, 0, 0});
int las = 0;
for (auto &[ts, l, r, d] : temp[i]) {
auto [mx, mxc] = query();
haha[las].push_back({i, mx, mxc});
las = ts;
rangeAdd2(l, r, d);
}
for (auto &[ts, l, r, d] : temp[i]) {
rangeAdd2(l, r, -d);
}
}
seg.init(k - 1, n - 1, 1);
for (int i = 0; i <= q; i++) {
for (auto [j, mx, mxc] : haha[i]) {
seg.upd(j, k - 1, n - 1, 1, pi{mx, mxc});
}
queries[i] = seg.query();
if (queries[i][0] == 0)
queries[i][1] = 1ll * (n - k + 1) * (n - k + 1);
cout << queries[i][0] << " " << queries[i][1] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
1492 KB |
Output is correct |
17 |
Correct |
6 ms |
1492 KB |
Output is correct |
18 |
Correct |
2 ms |
852 KB |
Output is correct |
19 |
Correct |
3 ms |
1168 KB |
Output is correct |
20 |
Correct |
6 ms |
1492 KB |
Output is correct |
21 |
Correct |
6 ms |
1444 KB |
Output is correct |
22 |
Correct |
2 ms |
956 KB |
Output is correct |
23 |
Correct |
6 ms |
1492 KB |
Output is correct |
24 |
Correct |
5 ms |
1460 KB |
Output is correct |
25 |
Correct |
4 ms |
1060 KB |
Output is correct |
26 |
Correct |
5 ms |
1108 KB |
Output is correct |
27 |
Correct |
6 ms |
1492 KB |
Output is correct |
28 |
Correct |
4 ms |
1108 KB |
Output is correct |
29 |
Correct |
3 ms |
980 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
1492 KB |
Output is correct |
17 |
Correct |
6 ms |
1492 KB |
Output is correct |
18 |
Correct |
2 ms |
852 KB |
Output is correct |
19 |
Correct |
3 ms |
1168 KB |
Output is correct |
20 |
Correct |
6 ms |
1492 KB |
Output is correct |
21 |
Correct |
6 ms |
1444 KB |
Output is correct |
22 |
Correct |
2 ms |
956 KB |
Output is correct |
23 |
Correct |
6 ms |
1492 KB |
Output is correct |
24 |
Correct |
5 ms |
1460 KB |
Output is correct |
25 |
Correct |
4 ms |
1060 KB |
Output is correct |
26 |
Correct |
5 ms |
1108 KB |
Output is correct |
27 |
Correct |
6 ms |
1492 KB |
Output is correct |
28 |
Correct |
4 ms |
1108 KB |
Output is correct |
29 |
Correct |
3 ms |
980 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
31 |
Correct |
97 ms |
20020 KB |
Output is correct |
32 |
Correct |
138 ms |
19964 KB |
Output is correct |
33 |
Correct |
18 ms |
9004 KB |
Output is correct |
34 |
Correct |
71 ms |
14408 KB |
Output is correct |
35 |
Correct |
143 ms |
20312 KB |
Output is correct |
36 |
Correct |
105 ms |
19712 KB |
Output is correct |
37 |
Correct |
26 ms |
9660 KB |
Output is correct |
38 |
Correct |
178 ms |
19728 KB |
Output is correct |
39 |
Correct |
120 ms |
20232 KB |
Output is correct |
40 |
Correct |
87 ms |
14932 KB |
Output is correct |
41 |
Correct |
104 ms |
19768 KB |
Output is correct |
42 |
Correct |
156 ms |
19772 KB |
Output is correct |
43 |
Correct |
86 ms |
14520 KB |
Output is correct |
44 |
Correct |
59 ms |
14404 KB |
Output is correct |
45 |
Correct |
24 ms |
9644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
1492 KB |
Output is correct |
17 |
Correct |
6 ms |
1492 KB |
Output is correct |
18 |
Correct |
2 ms |
852 KB |
Output is correct |
19 |
Correct |
3 ms |
1168 KB |
Output is correct |
20 |
Correct |
6 ms |
1492 KB |
Output is correct |
21 |
Correct |
6 ms |
1444 KB |
Output is correct |
22 |
Correct |
2 ms |
956 KB |
Output is correct |
23 |
Correct |
6 ms |
1492 KB |
Output is correct |
24 |
Correct |
5 ms |
1460 KB |
Output is correct |
25 |
Correct |
4 ms |
1060 KB |
Output is correct |
26 |
Correct |
5 ms |
1108 KB |
Output is correct |
27 |
Correct |
6 ms |
1492 KB |
Output is correct |
28 |
Correct |
4 ms |
1108 KB |
Output is correct |
29 |
Correct |
3 ms |
980 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
15 ms |
2664 KB |
Output is correct |
47 |
Correct |
16 ms |
2648 KB |
Output is correct |
48 |
Correct |
2 ms |
1108 KB |
Output is correct |
49 |
Correct |
10 ms |
1876 KB |
Output is correct |
50 |
Correct |
21 ms |
2704 KB |
Output is correct |
51 |
Correct |
4 ms |
1188 KB |
Output is correct |
52 |
Correct |
13 ms |
2088 KB |
Output is correct |
53 |
Correct |
20 ms |
2516 KB |
Output is correct |
54 |
Correct |
19 ms |
2644 KB |
Output is correct |
55 |
Correct |
7 ms |
1824 KB |
Output is correct |
56 |
Correct |
22 ms |
2664 KB |
Output is correct |
57 |
Correct |
20 ms |
2516 KB |
Output is correct |
58 |
Correct |
5 ms |
1492 KB |
Output is correct |
59 |
Correct |
16 ms |
2260 KB |
Output is correct |
60 |
Correct |
5 ms |
1472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
1492 KB |
Output is correct |
17 |
Correct |
6 ms |
1492 KB |
Output is correct |
18 |
Correct |
2 ms |
852 KB |
Output is correct |
19 |
Correct |
3 ms |
1168 KB |
Output is correct |
20 |
Correct |
6 ms |
1492 KB |
Output is correct |
21 |
Correct |
6 ms |
1444 KB |
Output is correct |
22 |
Correct |
2 ms |
956 KB |
Output is correct |
23 |
Correct |
6 ms |
1492 KB |
Output is correct |
24 |
Correct |
5 ms |
1460 KB |
Output is correct |
25 |
Correct |
4 ms |
1060 KB |
Output is correct |
26 |
Correct |
5 ms |
1108 KB |
Output is correct |
27 |
Correct |
6 ms |
1492 KB |
Output is correct |
28 |
Correct |
4 ms |
1108 KB |
Output is correct |
29 |
Correct |
3 ms |
980 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
31 |
Correct |
97 ms |
20020 KB |
Output is correct |
32 |
Correct |
138 ms |
19964 KB |
Output is correct |
33 |
Correct |
18 ms |
9004 KB |
Output is correct |
34 |
Correct |
71 ms |
14408 KB |
Output is correct |
35 |
Correct |
143 ms |
20312 KB |
Output is correct |
36 |
Correct |
105 ms |
19712 KB |
Output is correct |
37 |
Correct |
26 ms |
9660 KB |
Output is correct |
38 |
Correct |
178 ms |
19728 KB |
Output is correct |
39 |
Correct |
120 ms |
20232 KB |
Output is correct |
40 |
Correct |
87 ms |
14932 KB |
Output is correct |
41 |
Correct |
104 ms |
19768 KB |
Output is correct |
42 |
Correct |
156 ms |
19772 KB |
Output is correct |
43 |
Correct |
86 ms |
14520 KB |
Output is correct |
44 |
Correct |
59 ms |
14404 KB |
Output is correct |
45 |
Correct |
24 ms |
9644 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
0 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
15 ms |
2664 KB |
Output is correct |
62 |
Correct |
16 ms |
2648 KB |
Output is correct |
63 |
Correct |
2 ms |
1108 KB |
Output is correct |
64 |
Correct |
10 ms |
1876 KB |
Output is correct |
65 |
Correct |
21 ms |
2704 KB |
Output is correct |
66 |
Correct |
4 ms |
1188 KB |
Output is correct |
67 |
Correct |
13 ms |
2088 KB |
Output is correct |
68 |
Correct |
20 ms |
2516 KB |
Output is correct |
69 |
Correct |
19 ms |
2644 KB |
Output is correct |
70 |
Correct |
7 ms |
1824 KB |
Output is correct |
71 |
Correct |
22 ms |
2664 KB |
Output is correct |
72 |
Correct |
20 ms |
2516 KB |
Output is correct |
73 |
Correct |
5 ms |
1492 KB |
Output is correct |
74 |
Correct |
16 ms |
2260 KB |
Output is correct |
75 |
Correct |
5 ms |
1472 KB |
Output is correct |
76 |
Correct |
489 ms |
45424 KB |
Output is correct |
77 |
Correct |
537 ms |
45480 KB |
Output is correct |
78 |
Correct |
36 ms |
13492 KB |
Output is correct |
79 |
Correct |
289 ms |
29888 KB |
Output is correct |
80 |
Correct |
697 ms |
46072 KB |
Output is correct |
81 |
Correct |
77 ms |
19220 KB |
Output is correct |
82 |
Correct |
360 ms |
39540 KB |
Output is correct |
83 |
Correct |
600 ms |
45728 KB |
Output is correct |
84 |
Correct |
535 ms |
46660 KB |
Output is correct |
85 |
Correct |
159 ms |
29476 KB |
Output is correct |
86 |
Correct |
646 ms |
45868 KB |
Output is correct |
87 |
Correct |
625 ms |
45276 KB |
Output is correct |
88 |
Correct |
125 ms |
23436 KB |
Output is correct |
89 |
Correct |
477 ms |
40432 KB |
Output is correct |
90 |
Correct |
111 ms |
22184 KB |
Output is correct |