#include <bits/stdc++.h>
#define mi(x, y) (x = min(x, y))
#define ma(x, y) (x = max(x, y))
using namespace std;
const int SZ = (int)1e7, NS = (int)1e5 + 4;
struct Data{
int l, r, mx, lazy;
long long cnt;
Data(){
l = r = mx = lazy = cnt = 0;
}
};
Data tr[SZ];
int tn;
int n, k, q;
int yl[NS], yr[NS];
int a[NS], b[NS];
int rt[NS];
long long cntsum[NS];
void build(int x, int s, int e){
if(s == e){
tr[x].cnt = 1;
return;
}
int m = s + e >> 1;
tr[x].l = ++tn;
build(tr[x].l, s, m);
tr[x].r = ++tn;
build(tr[x].r, m + 1, e);
tr[x].cnt = e - s + 1;
}
void push(int pre, int now, int s, int e, int ps, int pe, int val){
// cout << "PUSH " << pre << ' ' << now << ' ' << s << ' ' << e << ' ' << ps << ' ' << pe << ' ' << val << endl;
if(ps <= s && e <= pe){
tr[now].mx += val;
tr[now].lazy += val;
return;
}
int m = s + e >> 1;
if(tr[now].lazy || ps <= m){
tr[now].l = ++tn;
tr[tn] = tr[tr[pre].l];
tr[tn].mx += tr[now].lazy;
tr[tn].lazy += tr[now].lazy;
}
if(tr[now].lazy || pe > m){
tr[now].r = ++tn;
tr[tn] = tr[tr[pre].r];
tr[tn].mx += tr[now].lazy;
tr[tn].lazy += tr[now].lazy;
}
tr[now].lazy = 0;
if(ps <= m){
push(tr[pre].l, tr[now].l, s, m, ps, pe, val);
}
if(pe > m){
push(tr[pre].r, tr[now].r, m + 1, e, ps, pe, val);
}
tr[now].mx = max(tr[tr[now].l].mx, tr[tr[now].r].mx);
tr[now].cnt = (tr[now].mx == tr[tr[now].l].mx ? tr[tr[now].l].cnt : 0) + (tr[now].mx == tr[tr[now].r].mx ? tr[tr[now].r].cnt : 0);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k >> q;
for(int i = 0; i < n; ++i){
cin >> a[i];
--a[i];
}
for(int i = 0; i < n; ++i){
cin >> b[i];
--b[i];
yl[b[i]] = max(0, i - k + 1);
yr[b[i]] = min(i, n - k);
}
vector<vector<vector<int>>> add(n - k + 2);
for(int i = 0; i < n; ++i){
add[max(0, i - k + 1)].push_back({yl[a[i]], yr[a[i]], 1});
add[min(i, n - k) + 1].push_back({yl[a[i]], yr[a[i]], -1});
}
++tn;
build(1, 0, n - k);
int pre = 1;
for(int i = 0; i <= n - k; ++i){
for(auto&j:add[i]){
// cout << i << ' ' << j[0] << ' ' << j[1] << ' ' << j[2] << endl;
rt[i] = ++tn;
tr[tn] = tr[pre];
push(pre, rt[i], 0, n - k, j[0], j[1], j[2]);
// cout << tr[rt[i]].mx << ' ' << tr[rt[i]].cnt << endl;
pre = rt[i];
}
}
multiset<int> st;
for(int i = 0; i <= n - k; ++i){
st.insert(tr[rt[i]].mx);
cntsum[tr[rt[i]].mx] += tr[rt[i]].cnt;
}
auto output = [&](){
int ans1 = *(--st.end());
cout << ans1 << ' ' << cntsum[ans1] << '\n';
};
output();
for(int i = 0; i < q; ++i){
int x;
cin >> x;
--x;
int lp = max(0, x - k + 1);
int rp = min(x, n - k) + 1;
st.erase(st.find(tr[rt[lp]].mx));
cntsum[tr[rt[lp]].mx] -= tr[rt[lp]].cnt;
int lst = rt[lp];
tr[++tn] = tr[rt[lp]];
push(lst, rt[lp] = tn, 0, n - k, yl[a[x]], yr[a[x]], -1);
lst = rt[lp];
tr[++tn] = tr[rt[lp]];
push(lst, rt[lp] = tn, 0, n - k, yl[a[x + 1]], yr[a[x + 1]], 1);
st.insert(tr[rt[lp]].mx);
cntsum[tr[rt[lp]].mx] += tr[rt[lp]].cnt;
if(rp <= n - k){
st.erase(st.find(tr[rt[rp]].mx));
cntsum[tr[rt[rp]].mx] -= tr[rt[rp]].cnt;
lst = rt[rp];
tr[++tn] = tr[rt[rp]];
push(lst, rt[rp] = tn, 0, n - k, yl[a[x]], yr[a[x]], 1);
lst = rt[rp];
tr[++tn] = tr[rt[rp]];
push(lst, rt[rp] = tn, 0, n - k, yl[a[x + 1]], yr[a[x + 1]], -1);
st.insert(tr[rt[rp]].mx);
cntsum[tr[rt[rp]].mx] += tr[rt[rp]].cnt;
}
swap(a[x], a[x + 1]);
output();
}
return 0;
}
Compilation message
Main.cpp: In function 'void build(int, int, int)':
Main.cpp:27:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
27 | int m = s + e >> 1;
| ~~^~~
Main.cpp: In function 'void push(int, int, int, int, int, int, int)':
Main.cpp:44:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | int m = s + e >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
235132 KB |
Output is correct |
2 |
Correct |
90 ms |
235160 KB |
Output is correct |
3 |
Correct |
90 ms |
235212 KB |
Output is correct |
4 |
Correct |
91 ms |
235080 KB |
Output is correct |
5 |
Correct |
103 ms |
235120 KB |
Output is correct |
6 |
Correct |
97 ms |
235080 KB |
Output is correct |
7 |
Correct |
95 ms |
235116 KB |
Output is correct |
8 |
Correct |
90 ms |
235172 KB |
Output is correct |
9 |
Correct |
90 ms |
235096 KB |
Output is correct |
10 |
Correct |
89 ms |
235084 KB |
Output is correct |
11 |
Correct |
97 ms |
235064 KB |
Output is correct |
12 |
Correct |
92 ms |
235064 KB |
Output is correct |
13 |
Correct |
93 ms |
235100 KB |
Output is correct |
14 |
Correct |
90 ms |
235132 KB |
Output is correct |
15 |
Correct |
91 ms |
235072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
235132 KB |
Output is correct |
2 |
Correct |
90 ms |
235160 KB |
Output is correct |
3 |
Correct |
90 ms |
235212 KB |
Output is correct |
4 |
Correct |
91 ms |
235080 KB |
Output is correct |
5 |
Correct |
103 ms |
235120 KB |
Output is correct |
6 |
Correct |
97 ms |
235080 KB |
Output is correct |
7 |
Correct |
95 ms |
235116 KB |
Output is correct |
8 |
Correct |
90 ms |
235172 KB |
Output is correct |
9 |
Correct |
90 ms |
235096 KB |
Output is correct |
10 |
Correct |
89 ms |
235084 KB |
Output is correct |
11 |
Correct |
97 ms |
235064 KB |
Output is correct |
12 |
Correct |
92 ms |
235064 KB |
Output is correct |
13 |
Correct |
93 ms |
235100 KB |
Output is correct |
14 |
Correct |
90 ms |
235132 KB |
Output is correct |
15 |
Correct |
91 ms |
235072 KB |
Output is correct |
16 |
Correct |
99 ms |
236196 KB |
Output is correct |
17 |
Correct |
95 ms |
236204 KB |
Output is correct |
18 |
Correct |
92 ms |
235868 KB |
Output is correct |
19 |
Correct |
96 ms |
235928 KB |
Output is correct |
20 |
Correct |
97 ms |
236236 KB |
Output is correct |
21 |
Correct |
96 ms |
236224 KB |
Output is correct |
22 |
Correct |
92 ms |
235980 KB |
Output is correct |
23 |
Correct |
98 ms |
236128 KB |
Output is correct |
24 |
Correct |
96 ms |
236124 KB |
Output is correct |
25 |
Correct |
99 ms |
236036 KB |
Output is correct |
26 |
Correct |
102 ms |
236092 KB |
Output is correct |
27 |
Correct |
99 ms |
236104 KB |
Output is correct |
28 |
Correct |
95 ms |
235976 KB |
Output is correct |
29 |
Correct |
95 ms |
235996 KB |
Output is correct |
30 |
Correct |
94 ms |
235952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
235132 KB |
Output is correct |
2 |
Correct |
90 ms |
235160 KB |
Output is correct |
3 |
Correct |
90 ms |
235212 KB |
Output is correct |
4 |
Correct |
91 ms |
235080 KB |
Output is correct |
5 |
Correct |
103 ms |
235120 KB |
Output is correct |
6 |
Correct |
97 ms |
235080 KB |
Output is correct |
7 |
Correct |
95 ms |
235116 KB |
Output is correct |
8 |
Correct |
90 ms |
235172 KB |
Output is correct |
9 |
Correct |
90 ms |
235096 KB |
Output is correct |
10 |
Correct |
89 ms |
235084 KB |
Output is correct |
11 |
Correct |
97 ms |
235064 KB |
Output is correct |
12 |
Correct |
92 ms |
235064 KB |
Output is correct |
13 |
Correct |
93 ms |
235100 KB |
Output is correct |
14 |
Correct |
90 ms |
235132 KB |
Output is correct |
15 |
Correct |
91 ms |
235072 KB |
Output is correct |
16 |
Correct |
99 ms |
236196 KB |
Output is correct |
17 |
Correct |
95 ms |
236204 KB |
Output is correct |
18 |
Correct |
92 ms |
235868 KB |
Output is correct |
19 |
Correct |
96 ms |
235928 KB |
Output is correct |
20 |
Correct |
97 ms |
236236 KB |
Output is correct |
21 |
Correct |
96 ms |
236224 KB |
Output is correct |
22 |
Correct |
92 ms |
235980 KB |
Output is correct |
23 |
Correct |
98 ms |
236128 KB |
Output is correct |
24 |
Correct |
96 ms |
236124 KB |
Output is correct |
25 |
Correct |
99 ms |
236036 KB |
Output is correct |
26 |
Correct |
102 ms |
236092 KB |
Output is correct |
27 |
Correct |
99 ms |
236104 KB |
Output is correct |
28 |
Correct |
95 ms |
235976 KB |
Output is correct |
29 |
Correct |
95 ms |
235996 KB |
Output is correct |
30 |
Correct |
94 ms |
235952 KB |
Output is correct |
31 |
Correct |
236 ms |
257184 KB |
Output is correct |
32 |
Correct |
232 ms |
256916 KB |
Output is correct |
33 |
Correct |
112 ms |
248344 KB |
Output is correct |
34 |
Correct |
192 ms |
252864 KB |
Output is correct |
35 |
Correct |
296 ms |
257188 KB |
Output is correct |
36 |
Correct |
234 ms |
256204 KB |
Output is correct |
37 |
Correct |
127 ms |
249128 KB |
Output is correct |
38 |
Correct |
292 ms |
256668 KB |
Output is correct |
39 |
Correct |
231 ms |
257372 KB |
Output is correct |
40 |
Correct |
312 ms |
253216 KB |
Output is correct |
41 |
Correct |
292 ms |
254824 KB |
Output is correct |
42 |
Correct |
316 ms |
256160 KB |
Output is correct |
43 |
Correct |
206 ms |
253036 KB |
Output is correct |
44 |
Correct |
183 ms |
251808 KB |
Output is correct |
45 |
Correct |
119 ms |
248908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
235132 KB |
Output is correct |
2 |
Correct |
90 ms |
235160 KB |
Output is correct |
3 |
Correct |
90 ms |
235212 KB |
Output is correct |
4 |
Correct |
91 ms |
235080 KB |
Output is correct |
5 |
Correct |
103 ms |
235120 KB |
Output is correct |
6 |
Correct |
97 ms |
235080 KB |
Output is correct |
7 |
Correct |
95 ms |
235116 KB |
Output is correct |
8 |
Correct |
90 ms |
235172 KB |
Output is correct |
9 |
Correct |
90 ms |
235096 KB |
Output is correct |
10 |
Correct |
89 ms |
235084 KB |
Output is correct |
11 |
Correct |
97 ms |
235064 KB |
Output is correct |
12 |
Correct |
92 ms |
235064 KB |
Output is correct |
13 |
Correct |
93 ms |
235100 KB |
Output is correct |
14 |
Correct |
90 ms |
235132 KB |
Output is correct |
15 |
Correct |
91 ms |
235072 KB |
Output is correct |
16 |
Correct |
94 ms |
235080 KB |
Output is correct |
17 |
Incorrect |
89 ms |
235128 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
235132 KB |
Output is correct |
2 |
Correct |
90 ms |
235160 KB |
Output is correct |
3 |
Correct |
90 ms |
235212 KB |
Output is correct |
4 |
Correct |
91 ms |
235080 KB |
Output is correct |
5 |
Correct |
103 ms |
235120 KB |
Output is correct |
6 |
Correct |
97 ms |
235080 KB |
Output is correct |
7 |
Correct |
95 ms |
235116 KB |
Output is correct |
8 |
Correct |
90 ms |
235172 KB |
Output is correct |
9 |
Correct |
90 ms |
235096 KB |
Output is correct |
10 |
Correct |
89 ms |
235084 KB |
Output is correct |
11 |
Correct |
97 ms |
235064 KB |
Output is correct |
12 |
Correct |
92 ms |
235064 KB |
Output is correct |
13 |
Correct |
93 ms |
235100 KB |
Output is correct |
14 |
Correct |
90 ms |
235132 KB |
Output is correct |
15 |
Correct |
91 ms |
235072 KB |
Output is correct |
16 |
Correct |
99 ms |
236196 KB |
Output is correct |
17 |
Correct |
95 ms |
236204 KB |
Output is correct |
18 |
Correct |
92 ms |
235868 KB |
Output is correct |
19 |
Correct |
96 ms |
235928 KB |
Output is correct |
20 |
Correct |
97 ms |
236236 KB |
Output is correct |
21 |
Correct |
96 ms |
236224 KB |
Output is correct |
22 |
Correct |
92 ms |
235980 KB |
Output is correct |
23 |
Correct |
98 ms |
236128 KB |
Output is correct |
24 |
Correct |
96 ms |
236124 KB |
Output is correct |
25 |
Correct |
99 ms |
236036 KB |
Output is correct |
26 |
Correct |
102 ms |
236092 KB |
Output is correct |
27 |
Correct |
99 ms |
236104 KB |
Output is correct |
28 |
Correct |
95 ms |
235976 KB |
Output is correct |
29 |
Correct |
95 ms |
235996 KB |
Output is correct |
30 |
Correct |
94 ms |
235952 KB |
Output is correct |
31 |
Correct |
94 ms |
235080 KB |
Output is correct |
32 |
Incorrect |
89 ms |
235128 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
235132 KB |
Output is correct |
2 |
Correct |
90 ms |
235160 KB |
Output is correct |
3 |
Correct |
90 ms |
235212 KB |
Output is correct |
4 |
Correct |
91 ms |
235080 KB |
Output is correct |
5 |
Correct |
103 ms |
235120 KB |
Output is correct |
6 |
Correct |
97 ms |
235080 KB |
Output is correct |
7 |
Correct |
95 ms |
235116 KB |
Output is correct |
8 |
Correct |
90 ms |
235172 KB |
Output is correct |
9 |
Correct |
90 ms |
235096 KB |
Output is correct |
10 |
Correct |
89 ms |
235084 KB |
Output is correct |
11 |
Correct |
97 ms |
235064 KB |
Output is correct |
12 |
Correct |
92 ms |
235064 KB |
Output is correct |
13 |
Correct |
93 ms |
235100 KB |
Output is correct |
14 |
Correct |
90 ms |
235132 KB |
Output is correct |
15 |
Correct |
91 ms |
235072 KB |
Output is correct |
16 |
Correct |
99 ms |
236196 KB |
Output is correct |
17 |
Correct |
95 ms |
236204 KB |
Output is correct |
18 |
Correct |
92 ms |
235868 KB |
Output is correct |
19 |
Correct |
96 ms |
235928 KB |
Output is correct |
20 |
Correct |
97 ms |
236236 KB |
Output is correct |
21 |
Correct |
96 ms |
236224 KB |
Output is correct |
22 |
Correct |
92 ms |
235980 KB |
Output is correct |
23 |
Correct |
98 ms |
236128 KB |
Output is correct |
24 |
Correct |
96 ms |
236124 KB |
Output is correct |
25 |
Correct |
99 ms |
236036 KB |
Output is correct |
26 |
Correct |
102 ms |
236092 KB |
Output is correct |
27 |
Correct |
99 ms |
236104 KB |
Output is correct |
28 |
Correct |
95 ms |
235976 KB |
Output is correct |
29 |
Correct |
95 ms |
235996 KB |
Output is correct |
30 |
Correct |
94 ms |
235952 KB |
Output is correct |
31 |
Correct |
236 ms |
257184 KB |
Output is correct |
32 |
Correct |
232 ms |
256916 KB |
Output is correct |
33 |
Correct |
112 ms |
248344 KB |
Output is correct |
34 |
Correct |
192 ms |
252864 KB |
Output is correct |
35 |
Correct |
296 ms |
257188 KB |
Output is correct |
36 |
Correct |
234 ms |
256204 KB |
Output is correct |
37 |
Correct |
127 ms |
249128 KB |
Output is correct |
38 |
Correct |
292 ms |
256668 KB |
Output is correct |
39 |
Correct |
231 ms |
257372 KB |
Output is correct |
40 |
Correct |
312 ms |
253216 KB |
Output is correct |
41 |
Correct |
292 ms |
254824 KB |
Output is correct |
42 |
Correct |
316 ms |
256160 KB |
Output is correct |
43 |
Correct |
206 ms |
253036 KB |
Output is correct |
44 |
Correct |
183 ms |
251808 KB |
Output is correct |
45 |
Correct |
119 ms |
248908 KB |
Output is correct |
46 |
Correct |
94 ms |
235080 KB |
Output is correct |
47 |
Incorrect |
89 ms |
235128 KB |
Output isn't correct |
48 |
Halted |
0 ms |
0 KB |
- |