#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct SegTree {
struct Node {
int ls = -1, rs = -1, cmx = 0, mx = 0, tp = 0, cpr = -1;
Node(int lb, int rb) {
cmx = rb - lb;
}
};
vector<Node> t;
SegTree(int n) {
t.push_back(Node(0, n));
}
void apply(int i, int tp) {
t[i].mx += tp;
t[i].tp += tp;
}
int copy(int i) {
int ni = t.size();
t.push_back(t[i]);
t[ni].ls = -1;
t[ni].rs = -1;
t[ni].cpr = i;
if (t[t[ni].cpr].ls == -1) {
t[ni].cpr = t[t[ni].cpr].cpr;
}
return ni;
}
void push(int i, int l, int r) {
if (t[i].ls == -1) {
if (t[i].cpr != -1) {
int cpr = t[i].cpr;
t[i].ls = copy(t[cpr].ls);
t[i].rs = copy(t[cpr].rs);
} else {
int m = (l + r) / 2;
t[i].ls = t.size();
t.push_back(Node(l, m));
t[i].rs = t.size();
t.push_back(Node(m, r));
}
}
if (t[i].tp) {
apply(t[i].ls, t[i].tp);
apply(t[i].rs, t[i].tp);
t[i].tp = 0;
}
}
int upd(bool init, int i, int l, int r, int ql, int qr, int x) {
if (init) {
i = copy(i);
}
if (l >= qr || ql >= r) return i;
if (ql <= l && r <= qr) {
t[i].mx += x;
t[i].tp += x;
return i;
}
push(i, l, r);
int m = (l + r) / 2;
upd(false, t[i].ls, l, m, ql, qr, x);
upd(false, t[i].rs, m, r, ql, qr, x);
t[i].mx = max(t[t[i].ls].mx, t[t[i].rs].mx);
t[i].cmx = 0;
if (t[t[i].ls].mx == t[i].mx) {
t[i].cmx += t[t[i].ls].cmx;
}
if (t[t[i].rs].mx == t[i].mx) {
t[i].cmx += t[t[i].rs].cmx;
}
return i;
}
};
void solve() {
int n, k, m;
cin >> n >> k >> m;
vector<int> p(n), q(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
p[i]--;
}
vector<int> pos(n);
for (int i = 0; i < n; i++) {
cin >> q[i];
q[i]--;
pos[q[i]] = i;
}
SegTree t(n);
int vnow = 0;
for (int i = 0; i < k - 1; i++) {
vnow = t.upd(true, vnow, 0, n, max(0, pos[p[i]] - k + 1), min(pos[p[i]], n - k) + 1, 1);
}
map<ll, ll> mp;
vector<int> verses(n - k + 1);
for (int i = 0; i < n - k + 1; i++) {
vnow = t.upd(true, vnow, 0, n, max(0, pos[p[i + k - 1]] - k + 1), min(pos[p[i + k - 1]], n - k) + 1, 1);
verses[i] = vnow;
mp[t.t[vnow].mx] += t.t[vnow].cmx;
vnow = t.upd(true, vnow, 0, n, max(0, pos[p[i]] - k + 1), min(pos[p[i]], n - k) + 1, -1);
}
auto [ans, cans] = *mp.rbegin();
cout << ans << ' ' << cans << '\n';
for (int i = 0; i < m; i++) {
int j;
cin >> j;
j--;
int p1 = p[j], p2 = p[j + 1];
if (j - k + 1 >= 0) {
int s1 = j - k + 1;
vnow = verses[s1];
mp[t.t[vnow].mx] -= t.t[vnow].cmx;
if (mp[t.t[vnow].mx] == 0) {
mp.erase(t.t[vnow].mx);
}
vnow = t.upd(true, vnow, 0, n, max(0, pos[p1] - k + 1), min(pos[p1], n - k) + 1, -1);
vnow = t.upd(true, vnow, 0, n, max(0, pos[p2] - k + 1), min(pos[p2], n - k) + 1, 1);
mp[t.t[vnow].mx] += t.t[vnow].cmx;
verses[s1] = vnow;
}
if (j + 1 < n - k + 1) {
int s2 = j + 1;
vnow = verses[s2];
mp[t.t[vnow].mx] -= t.t[vnow].cmx;
if (mp[t.t[vnow].mx] == 0) {
mp.erase(t.t[vnow].mx);
}
vnow = t.upd(true, vnow, 0, n, max(0, pos[p1] - k + 1), min(pos[p1], n - k) + 1, 1);
vnow = t.upd(true, vnow, 0, n, max(0, pos[p2] - k + 1), min(pos[p2], n - k) + 1, -1);
mp[t.t[vnow].mx] += t.t[vnow].cmx;
verses[s2] = vnow;
}
auto [ans, cans] = *mp.rbegin();
cout << ans << ' ' << cans << '\n';
swap(p[j], p[j + 1]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
}
# | 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... |