#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
pair <ll, ll> merge (pair <ll, ll> x, pair <ll, ll> y) {
if (x.first > y.first) return x;
if (x.first < y.first) return y;
return {x.first, x.second + y.second};
}
#define mid ((l + r) >> 1)
const int MAXN = 25'000'005;
int tl[MAXN], tr[MAXN], sum[MAXN], cnt;
pair <int, int> dp[MAXN];
int new_leaf (int x) {
cnt++; dp[cnt] = {0, 1};
return cnt;
}
int new_node (int l, int r) {
cnt++;
dp[cnt] = merge(dp[l], dp[r]);
tl[cnt] = l; tr[cnt] = r;
return cnt;
}
int build (int l, int r) {
if (l == r) {
return new_leaf(l);
} else {
return new_node(build(l, mid), build(mid + 1, r));
}
}
int update (int l, int r, int a, int b, int c, int node) {
if (l > b || r < a) return node;
if (l >= a && r <= b) {
cnt++; if (cnt >= MAXN) assert(0); tl[cnt] = tl[node]; tr[cnt] = tr[node];
sum[cnt] = sum[node]; dp[cnt] = dp[node];
sum[cnt] += c; dp[cnt].first += c;
return cnt;
}
int x = update(l, mid, a, b, c, tl[node]);
int y = update(mid + 1, r, a, b, c, tr[node]);
cnt++; if (cnt >= MAXN) assert(0);
tl[cnt] = x; tr[cnt] = y;
dp[cnt] = merge({dp[x].first + sum[node], dp[x].second}, {dp[y].first + sum[node], dp[y].second});
sum[cnt] = sum[node];
return cnt;
}
pair <ll, ll> get (int node) {
return dp[node];
}
int a[100002], n, k, q, p[100002];
int roots[100002];
struct SegmentTree2 {
pair <ll, ll> tree2[400001];
void build (int l, int r, int node) {
if (l == r) {
tree2[node] = {0, 1};
} else {
build(l, mid, 2 * node);
build(mid + 1, r, 2 * node + 1);
tree2[node] = merge(tree2[2 * node], tree2[2 * node + 1]);
}
}
void update (int l, int r, int a, pair <int, int> b, int node) {
if (l > a || r < a) return;
if (l == r) {
tree2[node] = b;
return;
}
update(l, mid, a, b, 2 * node);
update(mid + 1, r, a, b, 2 * node + 1);
tree2[node] = merge(tree2[2 * node], tree2[2 * node + 1]);
}
} cur;
void val () {
cout << cur.tree2[1].first << " " << cur.tree2[1].second << '\n';
}
void solve () {
cin >> n >> k >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
int x; cin >> x; p[x] = i;
}
for (int i = 1; i <= n; i++) {
a[i] = p[a[i]];
}
cur.build(1, n - k + 1, 1);
pair <ll, ll> dd = {-1, 0};
roots[1] = build(1, n);
for (int i = 1; i <= k; i++) {
roots[1] = update(1, n, max(1, a[i] - k + 1), min(a[i], n - k + 1), 1, roots[1]);
}
cur.update(1, n - k + 1, 1, get(roots[1]), 1);
for (int i = k + 1; i <= n; i++) {
roots[i - k + 1] = roots[i - k];
roots[i - k + 1] = update(1, n, max(1, a[i - k] - k + 1), min(a[i - k], n - k + 1), -1, roots[i - k + 1]);
roots[i - k + 1] = update(1, n, max(1, a[i] - k + 1), min(a[i], n - k + 1), 1, roots[i - k + 1]);
cur.update(1, n - k + 1, i - k + 1, get(roots[i - k + 1]), 1);
}
val();
while (q--) {
int t; cin >> t;
if (t + 1 <= n - k + 1) {
roots[t + 1] = update(1, n, max(1, a[t + 1] - k + 1), min(a[t + 1], n - k + 1), -1, roots[t + 1]);
roots[t + 1] = update(1, n, max(1, a[t] - k + 1), min(a[t], n - k + 1), 1, roots[t + 1]);
cur.update(1, n - k + 1, t + 1, get(roots[t + 1]), 1);
}
if (t - k + 1 >= 1) {
roots[t - k + 1] = update(1, n, max(1, a[t + 1] - k + 1), min(a[t + 1], n - k + 1), 1, roots[t - k + 1]);
roots[t - k + 1] = update(1, n, max(1, a[t] - k + 1), min(a[t], n - k + 1), -1, roots[t - k + 1]);
cur.update(1, n - k + 1, t - k + 1, get(roots[t - k + 1]), 1);
}
swap(a[t], a[t + 1]);
val();
}
}
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
int tc = 1; //cin >> tc;
while (tc--) solve();
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:88:27: warning: variable 'dd' set but not used [-Wunused-but-set-variable]
88 | pair <ll, ll> dd = {-1, 0};
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
3420 KB |
Output is correct |
17 |
Correct |
4 ms |
3548 KB |
Output is correct |
18 |
Correct |
2 ms |
1884 KB |
Output is correct |
19 |
Correct |
4 ms |
4188 KB |
Output is correct |
20 |
Correct |
6 ms |
5468 KB |
Output is correct |
21 |
Correct |
5 ms |
5856 KB |
Output is correct |
22 |
Correct |
2 ms |
2272 KB |
Output is correct |
23 |
Correct |
6 ms |
5336 KB |
Output is correct |
24 |
Correct |
4 ms |
5400 KB |
Output is correct |
25 |
Correct |
4 ms |
4700 KB |
Output is correct |
26 |
Correct |
5 ms |
5212 KB |
Output is correct |
27 |
Correct |
6 ms |
5724 KB |
Output is correct |
28 |
Correct |
4 ms |
4444 KB |
Output is correct |
29 |
Correct |
3 ms |
3676 KB |
Output is correct |
30 |
Correct |
2 ms |
1504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
3420 KB |
Output is correct |
17 |
Correct |
4 ms |
3548 KB |
Output is correct |
18 |
Correct |
2 ms |
1884 KB |
Output is correct |
19 |
Correct |
4 ms |
4188 KB |
Output is correct |
20 |
Correct |
6 ms |
5468 KB |
Output is correct |
21 |
Correct |
5 ms |
5856 KB |
Output is correct |
22 |
Correct |
2 ms |
2272 KB |
Output is correct |
23 |
Correct |
6 ms |
5336 KB |
Output is correct |
24 |
Correct |
4 ms |
5400 KB |
Output is correct |
25 |
Correct |
4 ms |
4700 KB |
Output is correct |
26 |
Correct |
5 ms |
5212 KB |
Output is correct |
27 |
Correct |
6 ms |
5724 KB |
Output is correct |
28 |
Correct |
4 ms |
4444 KB |
Output is correct |
29 |
Correct |
3 ms |
3676 KB |
Output is correct |
30 |
Correct |
2 ms |
1504 KB |
Output is correct |
31 |
Correct |
117 ms |
77396 KB |
Output is correct |
32 |
Correct |
166 ms |
81748 KB |
Output is correct |
33 |
Correct |
56 ms |
39476 KB |
Output is correct |
34 |
Correct |
147 ms |
99412 KB |
Output is correct |
35 |
Correct |
215 ms |
135764 KB |
Output is correct |
36 |
Correct |
164 ms |
152912 KB |
Output is correct |
37 |
Correct |
51 ms |
57400 KB |
Output is correct |
38 |
Correct |
217 ms |
129320 KB |
Output is correct |
39 |
Correct |
138 ms |
134908 KB |
Output is correct |
40 |
Correct |
173 ms |
118044 KB |
Output is correct |
41 |
Correct |
206 ms |
132424 KB |
Output is correct |
42 |
Correct |
257 ms |
152192 KB |
Output is correct |
43 |
Correct |
216 ms |
109140 KB |
Output is correct |
44 |
Correct |
127 ms |
88144 KB |
Output is correct |
45 |
Correct |
56 ms |
60892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
856 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
3420 KB |
Output is correct |
17 |
Correct |
4 ms |
3548 KB |
Output is correct |
18 |
Correct |
2 ms |
1884 KB |
Output is correct |
19 |
Correct |
4 ms |
4188 KB |
Output is correct |
20 |
Correct |
6 ms |
5468 KB |
Output is correct |
21 |
Correct |
5 ms |
5856 KB |
Output is correct |
22 |
Correct |
2 ms |
2272 KB |
Output is correct |
23 |
Correct |
6 ms |
5336 KB |
Output is correct |
24 |
Correct |
4 ms |
5400 KB |
Output is correct |
25 |
Correct |
4 ms |
4700 KB |
Output is correct |
26 |
Correct |
5 ms |
5212 KB |
Output is correct |
27 |
Correct |
6 ms |
5724 KB |
Output is correct |
28 |
Correct |
4 ms |
4444 KB |
Output is correct |
29 |
Correct |
3 ms |
3676 KB |
Output is correct |
30 |
Correct |
2 ms |
1504 KB |
Output is correct |
31 |
Correct |
0 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
0 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
856 KB |
Output is correct |
45 |
Correct |
0 ms |
344 KB |
Output is correct |
46 |
Correct |
10 ms |
8540 KB |
Output is correct |
47 |
Correct |
15 ms |
9356 KB |
Output is correct |
48 |
Correct |
3 ms |
2088 KB |
Output is correct |
49 |
Correct |
12 ms |
8540 KB |
Output is correct |
50 |
Correct |
18 ms |
14428 KB |
Output is correct |
51 |
Correct |
4 ms |
3420 KB |
Output is correct |
52 |
Correct |
19 ms |
12892 KB |
Output is correct |
53 |
Correct |
19 ms |
13864 KB |
Output is correct |
54 |
Correct |
25 ms |
14172 KB |
Output is correct |
55 |
Correct |
8 ms |
8028 KB |
Output is correct |
56 |
Correct |
22 ms |
14684 KB |
Output is correct |
57 |
Correct |
20 ms |
14292 KB |
Output is correct |
58 |
Correct |
6 ms |
4444 KB |
Output is correct |
59 |
Correct |
21 ms |
13468 KB |
Output is correct |
60 |
Correct |
5 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
3420 KB |
Output is correct |
17 |
Correct |
4 ms |
3548 KB |
Output is correct |
18 |
Correct |
2 ms |
1884 KB |
Output is correct |
19 |
Correct |
4 ms |
4188 KB |
Output is correct |
20 |
Correct |
6 ms |
5468 KB |
Output is correct |
21 |
Correct |
5 ms |
5856 KB |
Output is correct |
22 |
Correct |
2 ms |
2272 KB |
Output is correct |
23 |
Correct |
6 ms |
5336 KB |
Output is correct |
24 |
Correct |
4 ms |
5400 KB |
Output is correct |
25 |
Correct |
4 ms |
4700 KB |
Output is correct |
26 |
Correct |
5 ms |
5212 KB |
Output is correct |
27 |
Correct |
6 ms |
5724 KB |
Output is correct |
28 |
Correct |
4 ms |
4444 KB |
Output is correct |
29 |
Correct |
3 ms |
3676 KB |
Output is correct |
30 |
Correct |
2 ms |
1504 KB |
Output is correct |
31 |
Correct |
117 ms |
77396 KB |
Output is correct |
32 |
Correct |
166 ms |
81748 KB |
Output is correct |
33 |
Correct |
56 ms |
39476 KB |
Output is correct |
34 |
Correct |
147 ms |
99412 KB |
Output is correct |
35 |
Correct |
215 ms |
135764 KB |
Output is correct |
36 |
Correct |
164 ms |
152912 KB |
Output is correct |
37 |
Correct |
51 ms |
57400 KB |
Output is correct |
38 |
Correct |
217 ms |
129320 KB |
Output is correct |
39 |
Correct |
138 ms |
134908 KB |
Output is correct |
40 |
Correct |
173 ms |
118044 KB |
Output is correct |
41 |
Correct |
206 ms |
132424 KB |
Output is correct |
42 |
Correct |
257 ms |
152192 KB |
Output is correct |
43 |
Correct |
216 ms |
109140 KB |
Output is correct |
44 |
Correct |
127 ms |
88144 KB |
Output is correct |
45 |
Correct |
56 ms |
60892 KB |
Output is correct |
46 |
Correct |
0 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
600 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
604 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
604 KB |
Output is correct |
53 |
Correct |
0 ms |
604 KB |
Output is correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
1 ms |
600 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
856 KB |
Output is correct |
60 |
Correct |
0 ms |
344 KB |
Output is correct |
61 |
Correct |
10 ms |
8540 KB |
Output is correct |
62 |
Correct |
15 ms |
9356 KB |
Output is correct |
63 |
Correct |
3 ms |
2088 KB |
Output is correct |
64 |
Correct |
12 ms |
8540 KB |
Output is correct |
65 |
Correct |
18 ms |
14428 KB |
Output is correct |
66 |
Correct |
4 ms |
3420 KB |
Output is correct |
67 |
Correct |
19 ms |
12892 KB |
Output is correct |
68 |
Correct |
19 ms |
13864 KB |
Output is correct |
69 |
Correct |
25 ms |
14172 KB |
Output is correct |
70 |
Correct |
8 ms |
8028 KB |
Output is correct |
71 |
Correct |
22 ms |
14684 KB |
Output is correct |
72 |
Correct |
20 ms |
14292 KB |
Output is correct |
73 |
Correct |
6 ms |
4444 KB |
Output is correct |
74 |
Correct |
21 ms |
13468 KB |
Output is correct |
75 |
Correct |
5 ms |
4956 KB |
Output is correct |
76 |
Correct |
573 ms |
214184 KB |
Output is correct |
77 |
Correct |
620 ms |
231248 KB |
Output is correct |
78 |
Correct |
52 ms |
40664 KB |
Output is correct |
79 |
Correct |
530 ms |
219920 KB |
Output is correct |
80 |
Correct |
1060 ms |
387264 KB |
Output is correct |
81 |
Correct |
111 ms |
90960 KB |
Output is correct |
82 |
Correct |
459 ms |
335184 KB |
Output is correct |
83 |
Correct |
1016 ms |
371284 KB |
Output is correct |
84 |
Correct |
694 ms |
375148 KB |
Output is correct |
85 |
Correct |
218 ms |
222804 KB |
Output is correct |
86 |
Correct |
1159 ms |
428572 KB |
Output is correct |
87 |
Correct |
1136 ms |
418132 KB |
Output is correct |
88 |
Correct |
205 ms |
129608 KB |
Output is correct |
89 |
Correct |
913 ms |
372144 KB |
Output is correct |
90 |
Correct |
190 ms |
117900 KB |
Output is correct |