#include <bits/stdc++.h>
using namespace std;
#define arr array
#define int long long
#define pii pair<int, int>
#define fir first
#define sec second
#define vec vector
const int N = 1e5 + 5;
int n, k, q;
arr<int, N> a, b;
struct Sg {
struct Nd { int mx, nm, lzy; };
Nd mrg(Nd x, Nd y) {
if (x.mx < y.mx) swap(x, y);
return {x.mx, x.nm + ((y.mx == x.mx) ? y.nm : 0), 0};
}
arr<Nd, 4 * N> sg;
void bld(int u = 1, int lw = 1, int hg = n - k + 1) {
if (lw == hg) { sg[u] = {0, 1, 0}; return; }
int md = (lw + hg) / 2;
bld(2 * u, lw, md), bld(2 * u + 1, md + 1, hg);
sg[u] = mrg(sg[2 * u], sg[2 * u + 1]);
}
void prp(int u) {
for (int v : {2 * u, 2 * u + 1})
sg[v].mx += sg[u].lzy, sg[v].lzy += sg[u].lzy;
sg[u].lzy = 0;
}
void upd(int l, int r, int x, int u = 1, int lw = 1, int hg = n - k + 1) {
l = max(l, 1ll), r = min(r, n - k + 1);
if (r < lw || hg < l) return;
if (l <= lw && hg <= r) { sg[u].mx += x, sg[u].lzy += x; return; }
prp(u);
int md = (lw + hg) / 2;
upd(l, r, x, 2 * u, lw, md), upd(l, r, x, 2 * u + 1, md + 1, hg);
sg[u] = mrg(sg[2 * u], sg[2 * u + 1]);
}
} sg;
arr<int, N> ps;
arr<vec<vec<int>>, N> qrys; // {id, decrease id, increase id}
void prcmp() {
for (int i = 1; i <= n; i++)
ps[b[i]] = i;
vec<pii> und;
for (int j = 1; j <= q; j++) {
int i; cin >> i;
int lf_id = ps[a[i]], rg_id = ps[a[i + 1]];
swap(a[i], a[i + 1]), und.push_back({i, i + 1});
if (i - k + 1 >= 1) qrys[i - k + 1].push_back({j, lf_id, rg_id});
if (i + 1 <= n - k + 1) qrys[i + 1].push_back({j, rg_id, lf_id});
}
reverse(und.begin(), und.end());
for (pii x : und) swap(a[x.fir], a[x.sec]);
// for (int i = 1; i <= n; i++) {
// cout << i << ": " << endl;
// for (vec<int> x : qrys[i]) cout << x[0] << " " << x[1] << " " << x[2] << endl;
// }
}
map<int, int> cnt; // Value for Q = 0
arr<vec<vec<int>>, N> rsps;
void cmp() {
sg.bld();
for (int i = 1; i <= k; i++) {
int id = ps[a[i]];
sg.upd(id - k + 1, id, +1);
}
for (int i = 1; i <= n - k + 1; i++) {
if (i != 1) {
int lf_id = ps[a[i - 1]], rg_id = ps[a[i + k - 1]];
sg.upd(lf_id - k + 1, lf_id, -1);
sg.upd(rg_id - k + 1, rg_id, +1);
}
cnt[sg.sg[1].mx] += sg.sg[1].nm;
for (vec<int> x : qrys[i]) {
vec<int> rsp;
rsp.push_back(sg.sg[1].mx), rsp.push_back(sg.sg[1].nm);
sg.upd(x[1] - k + 1, x[1], -1);
sg.upd(x[2] - k + 1, x[2], +1);
rsp.push_back(sg.sg[1].mx), rsp.push_back(sg.sg[1].nm);
rsps[x[0]].push_back(rsp);
}
for (vec<int> x : qrys[i]) {
sg.upd(x[1] - k + 1, x[1], +1);
sg.upd(x[2] - k + 1, x[2], -1);
}
}
// for (int i = 1; i <= q; i++) {
// cout << i << ": " << endl;
// for (vec<int> x : rsps[i]) cout << x[0] << " " << x[1] << " " << x[2] << " " << x[3] << endl;
// }
}
void prnt() { cout << cnt.rbegin()->fir << " " << cnt.rbegin()->sec << endl; }
void pstcmp() {
prnt();
for (int i = 1; i <= q; i++) {
for (vec<int> x : rsps[i]) {
cnt[x[0]] -= x[1];
if (cnt[x[0]] == 0) cnt.erase(x[0]);
cnt[x[2]] += x[3];
}
prnt();
}
}
signed main() {
// freopen("a.in", "r", stdin);
cin >> n >> k >> q;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
prcmp(), cmp(), pstcmp();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |