#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ldb,ldb> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for(auto& a : x)
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 100005;
int n, k, q;
int A[mxN];
int B[mxN];
int poz[mxN];
int L[mxN];
int R[mxN];
int idx = 0;
int ls[200 * mxN], rs[200 * mxN], root[200 * mxN];
int mx[200 * mxN], sum[200 * mxN], cnt[200 * mxN];
void build(int& v, int tl, int tr){
v = ++idx;
if(tl == tr){
mx[v] = sum[v] = 0;
cnt[v] = 1;
return;
}
int mid = (tl + tr) / 2;
build(ls[v], tl, mid); build(rs[v], mid + 1, tr);
sum[v] = sum[ls[v]] + sum[rs[v]];
mx[v] = max(mx[ls[v]], mx[rs[v]] + sum[ls[v]]);
cnt[v] = (mx[v] == mx[ls[v]] ? cnt[ls[v]] : 0) + (mx[v] == mx[rs[v]] + sum[ls[v]] ? cnt[rs[v]] : 0);
}
void update(int& v, int rv, int tl, int tr, int pos, int val){
v = ++idx; ls[v] = ls[rv]; rs[v] = rs[rv]; mx[v] = mx[rv]; sum[v] = sum[rv]; cnt[v] = cnt[rv];
if(tl == tr){
mx[v] += val;
sum[v] += val;
return;
}
int mid = (tl + tr) / 2;
if(pos <= mid)update(ls[v], ls[rv], tl, mid, pos, val);
else update(rs[v], rs[rv], mid + 1, tr, pos, val);
sum[v] = sum[ls[v]] + sum[rs[v]];
mx[v] = max(mx[ls[v]], mx[rs[v]] + sum[ls[v]]);
cnt[v] = (mx[v] == mx[ls[v]] ? cnt[ls[v]] : 0) + (mx[v] == mx[rs[v]] + sum[ls[v]] ? cnt[rs[v]] : 0);
}
ll br = 0;
int najv = 0;
void calc(){
idx = 0;
build(root[k],1,n - k + 1);
ff(i,1,k){
int l = L[poz[A[i]]];
int r = R[poz[A[i]]];
update(root[k],root[k],1,n - k + 1,l,1);
if(r < n - k + 1)update(root[k],root[k],1,n - k + 1,r + 1,-1);
}
najv = mx[root[k]]; br = cnt[root[k]];
ff(i,k + 1,n){
int l1 = L[poz[A[i - k]]];
int r1 = R[poz[A[i - k]]];
update(root[i],root[i - 1],1,n - k + 1,l1,-1);
if(r1 < n - k + 1)update(root[i],root[i],1,n - k + 1,r1 + 1,1);
int l2 = L[poz[A[i]]];
int r2 = R[poz[A[i]]];
update(root[i],root[i],1,n - k + 1,l2,1);
if(r2 < n - k + 1)update(root[i],root[i],1,n - k + 1,r2 + 1,-1);
if(mx[root[i]] > najv){
najv = mx[root[i]];
br = cnt[root[i]];
}
else if(mx[root[i]] == najv)br += cnt[root[i]];
}
cout << najv << " " << br << '\n';
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k >> q;
ff(i,1,n)cin >> A[i];
ff(i,1,n)cin >> B[i], poz[B[i]] = i;
ff(i,1,n){
L[i] = max(1, i - k + 1);
R[i] = min(i, n - k + 1);
}
calc();
while(q--){
int t;
cin >> t;
if(t - k + 1 >= 1){
int x = t;
if(mx[root[x]] == najv)br -= cnt[root[x]];
int l1 = L[poz[A[t]]];
int r1 = R[poz[A[t]]];
update(root[x],root[x],1,n - k + 1,l1,-1);
if(r1 < n - k + 1)update(root[x],root[x],1,n - k + 1,r1 + 1,1);
int l2 = L[poz[A[t + 1]]];
int r2 = R[poz[A[t + 1]]];
update(root[x],root[x],1,n - k + 1,l2,1);
if(r2 < n - k + 1)update(root[x],root[x],1,n - k + 1,r2 + 1,-1);
if(mx[root[x]] > najv){
najv = mx[root[x]];
br = cnt[root[x]];
}
else if(mx[root[x]] == najv)br += cnt[root[x]];
}
if(t + k <= n){
int x = t + k;
if(mx[root[x]] == najv)br -= cnt[root[x]];
int l1 = L[poz[A[t + 1]]];
int r1 = R[poz[A[t + 1]]];
update(root[x],root[x],1,n - k + 1,l1,-1);
if(r1 < n - k + 1)update(root[x],root[x],1,n - k + 1,r1 + 1,1);
int l2 = L[poz[A[t]]];
int r2 = R[poz[A[t]]];
update(root[x],root[x],1,n - k + 1,l2,1);
if(r2 < n - k + 1)update(root[x],root[x],1,n - k + 1,r2 + 1,-1);
if(mx[root[x]] > najv){
najv = mx[root[x]];
br = cnt[root[x]];
}
else if(mx[root[x]] == najv)br += cnt[root[x]];
}
swap(A[t], A[t + 1]);
cout << najv << " " << br << '\n';
}
return 0;
}
/*
4 3 0
2 4 1 3
1 2 3 4
5 3 1
1 4 3 2 5
4 5 1 2 3
3
// probati bojenje sahovski
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10724 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
12664 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10724 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
12664 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
9 ms |
13144 KB |
Output is correct |
17 |
Correct |
8 ms |
14172 KB |
Output is correct |
18 |
Correct |
2 ms |
8792 KB |
Output is correct |
19 |
Correct |
5 ms |
13404 KB |
Output is correct |
20 |
Correct |
10 ms |
14172 KB |
Output is correct |
21 |
Correct |
7 ms |
13916 KB |
Output is correct |
22 |
Correct |
2 ms |
11100 KB |
Output is correct |
23 |
Correct |
9 ms |
14144 KB |
Output is correct |
24 |
Correct |
8 ms |
13148 KB |
Output is correct |
25 |
Correct |
6 ms |
12124 KB |
Output is correct |
26 |
Correct |
6 ms |
13404 KB |
Output is correct |
27 |
Correct |
8 ms |
15196 KB |
Output is correct |
28 |
Correct |
5 ms |
14936 KB |
Output is correct |
29 |
Correct |
5 ms |
14940 KB |
Output is correct |
30 |
Correct |
3 ms |
14940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10724 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
12664 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
9 ms |
13144 KB |
Output is correct |
17 |
Correct |
8 ms |
14172 KB |
Output is correct |
18 |
Correct |
2 ms |
8792 KB |
Output is correct |
19 |
Correct |
5 ms |
13404 KB |
Output is correct |
20 |
Correct |
10 ms |
14172 KB |
Output is correct |
21 |
Correct |
7 ms |
13916 KB |
Output is correct |
22 |
Correct |
2 ms |
11100 KB |
Output is correct |
23 |
Correct |
9 ms |
14144 KB |
Output is correct |
24 |
Correct |
8 ms |
13148 KB |
Output is correct |
25 |
Correct |
6 ms |
12124 KB |
Output is correct |
26 |
Correct |
6 ms |
13404 KB |
Output is correct |
27 |
Correct |
8 ms |
15196 KB |
Output is correct |
28 |
Correct |
5 ms |
14936 KB |
Output is correct |
29 |
Correct |
5 ms |
14940 KB |
Output is correct |
30 |
Correct |
3 ms |
14940 KB |
Output is correct |
31 |
Correct |
239 ms |
147800 KB |
Output is correct |
32 |
Correct |
244 ms |
146856 KB |
Output is correct |
33 |
Correct |
13 ms |
13148 KB |
Output is correct |
34 |
Correct |
146 ms |
82036 KB |
Output is correct |
35 |
Correct |
307 ms |
149584 KB |
Output is correct |
36 |
Correct |
189 ms |
139088 KB |
Output is correct |
37 |
Correct |
48 ms |
39204 KB |
Output is correct |
38 |
Correct |
278 ms |
142928 KB |
Output is correct |
39 |
Correct |
206 ms |
148540 KB |
Output is correct |
40 |
Correct |
188 ms |
94328 KB |
Output is correct |
41 |
Correct |
208 ms |
107320 KB |
Output is correct |
42 |
Correct |
303 ms |
134224 KB |
Output is correct |
43 |
Correct |
159 ms |
87124 KB |
Output is correct |
44 |
Correct |
122 ms |
72784 KB |
Output is correct |
45 |
Correct |
55 ms |
39248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10724 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
12664 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
1 ms |
12636 KB |
Output is correct |
17 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10724 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
12664 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
9 ms |
13144 KB |
Output is correct |
17 |
Correct |
8 ms |
14172 KB |
Output is correct |
18 |
Correct |
2 ms |
8792 KB |
Output is correct |
19 |
Correct |
5 ms |
13404 KB |
Output is correct |
20 |
Correct |
10 ms |
14172 KB |
Output is correct |
21 |
Correct |
7 ms |
13916 KB |
Output is correct |
22 |
Correct |
2 ms |
11100 KB |
Output is correct |
23 |
Correct |
9 ms |
14144 KB |
Output is correct |
24 |
Correct |
8 ms |
13148 KB |
Output is correct |
25 |
Correct |
6 ms |
12124 KB |
Output is correct |
26 |
Correct |
6 ms |
13404 KB |
Output is correct |
27 |
Correct |
8 ms |
15196 KB |
Output is correct |
28 |
Correct |
5 ms |
14936 KB |
Output is correct |
29 |
Correct |
5 ms |
14940 KB |
Output is correct |
30 |
Correct |
3 ms |
14940 KB |
Output is correct |
31 |
Correct |
1 ms |
12636 KB |
Output is correct |
32 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10724 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
12664 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
9 ms |
13144 KB |
Output is correct |
17 |
Correct |
8 ms |
14172 KB |
Output is correct |
18 |
Correct |
2 ms |
8792 KB |
Output is correct |
19 |
Correct |
5 ms |
13404 KB |
Output is correct |
20 |
Correct |
10 ms |
14172 KB |
Output is correct |
21 |
Correct |
7 ms |
13916 KB |
Output is correct |
22 |
Correct |
2 ms |
11100 KB |
Output is correct |
23 |
Correct |
9 ms |
14144 KB |
Output is correct |
24 |
Correct |
8 ms |
13148 KB |
Output is correct |
25 |
Correct |
6 ms |
12124 KB |
Output is correct |
26 |
Correct |
6 ms |
13404 KB |
Output is correct |
27 |
Correct |
8 ms |
15196 KB |
Output is correct |
28 |
Correct |
5 ms |
14936 KB |
Output is correct |
29 |
Correct |
5 ms |
14940 KB |
Output is correct |
30 |
Correct |
3 ms |
14940 KB |
Output is correct |
31 |
Correct |
239 ms |
147800 KB |
Output is correct |
32 |
Correct |
244 ms |
146856 KB |
Output is correct |
33 |
Correct |
13 ms |
13148 KB |
Output is correct |
34 |
Correct |
146 ms |
82036 KB |
Output is correct |
35 |
Correct |
307 ms |
149584 KB |
Output is correct |
36 |
Correct |
189 ms |
139088 KB |
Output is correct |
37 |
Correct |
48 ms |
39204 KB |
Output is correct |
38 |
Correct |
278 ms |
142928 KB |
Output is correct |
39 |
Correct |
206 ms |
148540 KB |
Output is correct |
40 |
Correct |
188 ms |
94328 KB |
Output is correct |
41 |
Correct |
208 ms |
107320 KB |
Output is correct |
42 |
Correct |
303 ms |
134224 KB |
Output is correct |
43 |
Correct |
159 ms |
87124 KB |
Output is correct |
44 |
Correct |
122 ms |
72784 KB |
Output is correct |
45 |
Correct |
55 ms |
39248 KB |
Output is correct |
46 |
Correct |
1 ms |
12636 KB |
Output is correct |
47 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
48 |
Halted |
0 ms |
0 KB |
- |