This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Node{
int mx;
ll cnt;
Node(){}
Node(int _mx, ll _cnt): mx(_mx), cnt(_cnt) {}
Node operator + (const Node &N) const{
if (mx > N.mx) return *this;
if (mx < N.mx) return N;
return Node(mx, cnt + N.cnt);
}
};
struct Seg1{
Node tree[800800];
int lazy[800800];
void init(int i, int l, int r){
tree[i] = Node(0, r-l+1);
lazy[i] = 0;
if (l==r) return;
int m = (l+r)>>1;
init(i<<1, l, m); init(i<<1|1, m+1, r);
}
void propagate(int i, int l, int r){
tree[i].mx += lazy[i];
if (l!=r){
lazy[i<<1] += lazy[i];
lazy[i<<1|1] += lazy[i];
}
lazy[i] = 0;
}
void update(int i, int l, int r, int s, int e, int x){
propagate(i, l, r);
if (r<s || e<l) return;
if (s<=l && r<=e){
lazy[i] += x;
propagate(i, l, r);
return;
}
int m = (l+r)>>1;
update(i<<1, l, m, s, e, x); update(i<<1|1, m+1, r, s, e, x);
tree[i] = tree[i<<1] + tree[i<<1|1];
}
Node query(int i, int l, int r, int s, int e){
propagate(i, l, r);
if (r<s || e<l) return Node(0, 0);
if (s<=l && r<=e) return tree[i];
int m = (l+r)>>1;
return query(i<<1, l, m, s, e) + query(i<<1|1, m+1, r, s, e);
}
}tree1;
struct Seg2{
Node tree[800800];
Node lazy[800800];
void init(int i, int l, int r){
tree[i] = Node(0, 0);
lazy[i] = Node(0, 0);
if (l==r) return;
int m = (l+r)>>1;
init(i<<1, l, m); init(i<<1|1, m+1, r);
}
void propagate(int i, int l, int r){
if (l!=r){
lazy[i<<1] = lazy[i<<1] + lazy[i];
lazy[i<<1|1] = lazy[i<<1|1] + lazy[i];
}
else{
tree[i] = tree[i] + lazy[i];
}
lazy[i] = Node(0, 0);
}
void update(int i, int l, int r, int s, int e, Node x){
propagate(i, l, r);
if (r<s || e<l) return;
if (s<=l && r<=e){
lazy[i] = lazy[i] + x;
propagate(i, l, r);
return;
}
int m = (l+r)>>1;
update(i<<1, l, m, s, e, x); update(i<<1|1, m+1, r, s, e, x);
}
Node query(int i, int l, int r, int p){
propagate(i, l, r);
if (r<p || p<l) return Node(0, 0);
if (l==r) return tree[i];
int m = (l+r)>>1;
return query(i<<1, l, m, p) + query(i<<1|1, m+1, r, p);
}
}tree2;
int a[100100], b[100100], X[100100], Y[100100];
vector<array<int, 3>> E[200200];
vector<array<int, 4>> F[200200];
void print(Node x){
printf("%d %lld\n", x.mx, x.cnt);
}
int main(){
int n, k, q;
scanf("%d %d %d", &n, &k, &q);
for (int i=1;i<=n;i++){
scanf("%d", a+i);
X[a[i]] = i;
}
for (int i=1;i<=n;i++){
scanf("%d", b+i);
Y[b[i]] = i;
}
for (int i=1;i<=n;i++){
E[X[i]].push_back({Y[i], Y[i]+k-1, 1});
E[X[i]+k].push_back({Y[i], Y[i]+k-1, -1});
}
for (int i=1;i<=q;i++){
int x;
scanf("%d", &x);
swap(a[x], a[x+1]);
X[a[x]] = x;
F[x].push_back({i, Y[a[x]], Y[a[x]]+k-1, 1});
F[x+k].push_back({i, Y[a[x]], Y[a[x]]+k-1, -1});
X[a[x+1]] = x+1;
F[x].push_back({i, Y[a[x+1]], Y[a[x+1]]+k-1, -1});
F[x+k].push_back({i, Y[a[x+1]], Y[a[x+1]]+k-1, 1});
}
tree1.init(1, 1, n+k-1);
tree2.init(1, 0, q);
for (int i=1;i<=n;i++){
for (auto &[l, r, x]:E[i]) tree1.update(1, 1, n+k-1, l, r, x);
if (i<k) continue;
if (F[i].empty()){
tree2.update(1, 0, q, 0, q, tree1.query(1, 1, n+k-1, k, n));
continue;
}
tree2.update(1, 0, q, 0, F[i][0][0]-1, tree1.query(1, 1, n+k-1, k, n));
for (int j=0;j<(int)F[i].size();j++){
tree1.update(1, 1, n+k-1, F[i][j][1], F[i][j][2], F[i][j][3]);
if (j+1<(int)F[i].size() && F[i][j][0]==F[i][j+1][0]) continue;
tree2.update(1, 0, q, F[i][j][0], (j+1<(int)F[i].size())?(F[i][j+1][0]-1):q, tree1.query(1, 1, n+k-1, k, n));
}
for (int j=0;j<(int)F[i].size();j++){
tree1.update(1, 1, n+k-1, F[i][j][1], F[i][j][2], -F[i][j][3]);
}
}
for (int i=0;i<=q;i++) print(tree2.query(1, 0, q, i));
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:127:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | scanf("%d %d %d", &n, &k, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:130:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
130 | scanf("%d", a+i);
| ~~~~~^~~~~~~~~~~
Main.cpp:134:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | scanf("%d", b+i);
| ~~~~~^~~~~~~~~~~
Main.cpp:145:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
145 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
# | 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... |