#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 root[mxN], ls[200 * mxN], rs[200 * mxN], lenj[200 * mxN]; ll bor[200 * mxN][2];
void build(int& v, int tl, int tr){
v = ++idx;
lenj[v] = 0;
if(tl == tr){
bor[v][0] = 0;
bor[v][1] = 1;
return;
}
int mid = (tl + tr) / 2;
build(ls[v], tl, mid); build(rs[v], mid + 1, tr);
bor[v][0] = max(bor[ls[v]][0], bor[rs[v]][0]);
bor[v][1] = (bor[ls[v]][0] == bor[v][0] ? bor[ls[v]][1] : 0) +
(bor[rs[v]][0] == bor[v][0] ? bor[rs[v]][1] : 0);
}
void propagate(int v, int tl, int tr){
if(lenj[v] != 0){
bor[v][0] += lenj[v];
if(tl != tr){
lenj[ls[v]] += lenj[v];
lenj[rs[v]] += lenj[v];
}
lenj[v] = 0;
}
}
void napravi(int& v, int rv){
v = ++idx; ls[v] = ls[rv]; rs[v] = rs[rv];
bor[v][0] = bor[rv][0]; bor[v][1] = bor[rv][1]; lenj[v] = lenj[rv];
}
void lazyupd(int& v, int rv, int tl, int tr, int l, int r, int val){
propagate(rv, tl, tr);
napravi(v, rv);
if(tl > tr || tl > r || l > tr)return;
if(tl >= l && tr <= r){
lenj[v] += val;
if(tl != tr){
napravi(ls[v], ls[rv]);
napravi(rs[v], rs[rv]);
}
propagate(v, tl, tr);
// cout << "ide: " << tl << " " << tr << " " << bor[v][0] << " " << bor[v][1] << '\n';
return;
}
int mid = (tl + tr) / 2;
lazyupd(ls[v], ls[rv], tl, mid, l, r, val); lazyupd(rs[v], rs[rv], mid + 1, tr, l, r, val);
bor[v][0] = max(bor[ls[v]][0], bor[rs[v]][0]);
bor[v][1] = (bor[ls[v]][0] == bor[v][0] ? bor[ls[v]][1] : 0) +
(bor[rs[v]][0] == bor[v][0] ? bor[rs[v]][1] : 0);
// cout << "posle: " << tl << " " << tr << " " << bor[v][0] << " " << bor[v][1] << '\n';
}
void upd(int x, int a, int koef){
int l = L[poz[a]];
int r = R[poz[a]];
// cout << "upd: " << l << " " << r << " " << koef << '\n';
lazyupd(root[x],root[x],1,n - k + 1,l,r,koef);
// cout << "--------------" << '\n';
}
void calc(){
idx = 0;
build(root[1],1,n - k + 1);
ff(i,1,k){
int l = L[poz[A[i]]];
int r = R[poz[A[i]]];
lazyupd(root[1],root[1],1,n - k + 1,l,r,1);
}
int mx = bor[root[1]][0]; ll br = bor[root[1]][1];
ff(i,k + 1,n){
root[i - k + 1] = root[i - k];
upd(i - k + 1,A[i - k],-1);
upd(i - k + 1,A[i],1);
if(bor[root[i - k + 1]][0] > mx){
mx = bor[root[i - k + 1]][0];
br = bor[root[i - k + 1]][1];
}
else if(bor[root[i - k + 1]][0] == mx)br += bor[root[i - k + 1]][1];
}
cout << mx << " " << 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;
// int a = A[t];
// int b = A[t + 1];
// int x = t - k + 1;
// if(x >= 1){
// cout << "x: " << x << '\n';
// upd(x, a, -1);
// upd(x, b, 1);
// }
// int y = t + 1;
// if(y + k - 1 <= n){
// cout << "y: " << y << '\n';
// upd(y, b, -1);
// upd(y, a, 1);
// }
swap(A[t], A[t + 1]);
// int mx = 0; ll br = 0;
// ff(i,1,n - k + 1){
// cout << "mx & br: " << bor[root[i]][0] << " " << bor[root[i]][1] << '\n';
// if(bor[root[i]][0] > mx){
// mx = bor[root[i]][0];
// br = bor[root[i]][1];
// }
// else if(bor[root[i]][0] == mx)br += bor[root[i]][1];
// }
// cout << mx << " " << br << '\n';
calc();
}
return 0;
}
/*
5 3 1
1 4 3 2 5
4 5 1 2 3
3
4 3 0
2 4 1 3
1 2 3 4
// probati bojenje sahovski
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10716 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10720 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10716 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10720 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
6 ms |
21080 KB |
Output is correct |
17 |
Correct |
7 ms |
23132 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
6 ms |
19036 KB |
Output is correct |
20 |
Correct |
12 ms |
25320 KB |
Output is correct |
21 |
Correct |
9 ms |
25112 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
10 ms |
25316 KB |
Output is correct |
24 |
Correct |
8 ms |
25180 KB |
Output is correct |
25 |
Correct |
8 ms |
23132 KB |
Output is correct |
26 |
Correct |
9 ms |
23052 KB |
Output is correct |
27 |
Correct |
10 ms |
25176 KB |
Output is correct |
28 |
Correct |
8 ms |
21084 KB |
Output is correct |
29 |
Correct |
6 ms |
16988 KB |
Output is correct |
30 |
Correct |
4 ms |
12888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10716 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10720 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
6 ms |
21080 KB |
Output is correct |
17 |
Correct |
7 ms |
23132 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
6 ms |
19036 KB |
Output is correct |
20 |
Correct |
12 ms |
25320 KB |
Output is correct |
21 |
Correct |
9 ms |
25112 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
10 ms |
25316 KB |
Output is correct |
24 |
Correct |
8 ms |
25180 KB |
Output is correct |
25 |
Correct |
8 ms |
23132 KB |
Output is correct |
26 |
Correct |
9 ms |
23052 KB |
Output is correct |
27 |
Correct |
10 ms |
25176 KB |
Output is correct |
28 |
Correct |
8 ms |
21084 KB |
Output is correct |
29 |
Correct |
6 ms |
16988 KB |
Output is correct |
30 |
Correct |
4 ms |
12888 KB |
Output is correct |
31 |
Correct |
237 ms |
198884 KB |
Output is correct |
32 |
Correct |
183 ms |
208212 KB |
Output is correct |
33 |
Correct |
15 ms |
10840 KB |
Output is correct |
34 |
Correct |
186 ms |
191764 KB |
Output is correct |
35 |
Correct |
435 ms |
358164 KB |
Output is correct |
36 |
Correct |
257 ms |
402512 KB |
Output is correct |
37 |
Correct |
23 ms |
38740 KB |
Output is correct |
38 |
Correct |
345 ms |
341696 KB |
Output is correct |
39 |
Correct |
220 ms |
355904 KB |
Output is correct |
40 |
Correct |
270 ms |
252300 KB |
Output is correct |
41 |
Correct |
317 ms |
297812 KB |
Output is correct |
42 |
Correct |
453 ms |
394288 KB |
Output is correct |
43 |
Correct |
217 ms |
213072 KB |
Output is correct |
44 |
Correct |
142 ms |
149588 KB |
Output is correct |
45 |
Correct |
28 ms |
38736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10716 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10720 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
4 ms |
10588 KB |
Output is correct |
17 |
Correct |
5 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
4 ms |
10588 KB |
Output is correct |
20 |
Correct |
6 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
5 ms |
10792 KB |
Output is correct |
23 |
Correct |
5 ms |
10588 KB |
Output is correct |
24 |
Correct |
5 ms |
10588 KB |
Output is correct |
25 |
Correct |
4 ms |
10584 KB |
Output is correct |
26 |
Correct |
5 ms |
10588 KB |
Output is correct |
27 |
Correct |
4 ms |
10784 KB |
Output is correct |
28 |
Correct |
3 ms |
10844 KB |
Output is correct |
29 |
Correct |
5 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10716 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10720 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
6 ms |
21080 KB |
Output is correct |
17 |
Correct |
7 ms |
23132 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
6 ms |
19036 KB |
Output is correct |
20 |
Correct |
12 ms |
25320 KB |
Output is correct |
21 |
Correct |
9 ms |
25112 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
10 ms |
25316 KB |
Output is correct |
24 |
Correct |
8 ms |
25180 KB |
Output is correct |
25 |
Correct |
8 ms |
23132 KB |
Output is correct |
26 |
Correct |
9 ms |
23052 KB |
Output is correct |
27 |
Correct |
10 ms |
25176 KB |
Output is correct |
28 |
Correct |
8 ms |
21084 KB |
Output is correct |
29 |
Correct |
6 ms |
16988 KB |
Output is correct |
30 |
Correct |
4 ms |
12888 KB |
Output is correct |
31 |
Correct |
4 ms |
10588 KB |
Output is correct |
32 |
Correct |
5 ms |
10588 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
4 ms |
10588 KB |
Output is correct |
35 |
Correct |
6 ms |
10588 KB |
Output is correct |
36 |
Correct |
2 ms |
10588 KB |
Output is correct |
37 |
Correct |
5 ms |
10792 KB |
Output is correct |
38 |
Correct |
5 ms |
10588 KB |
Output is correct |
39 |
Correct |
5 ms |
10588 KB |
Output is correct |
40 |
Correct |
4 ms |
10584 KB |
Output is correct |
41 |
Correct |
5 ms |
10588 KB |
Output is correct |
42 |
Correct |
4 ms |
10784 KB |
Output is correct |
43 |
Correct |
3 ms |
10844 KB |
Output is correct |
44 |
Correct |
5 ms |
10588 KB |
Output is correct |
45 |
Correct |
2 ms |
10588 KB |
Output is correct |
46 |
Execution timed out |
3092 ms |
21216 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10716 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10720 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
6 ms |
21080 KB |
Output is correct |
17 |
Correct |
7 ms |
23132 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
6 ms |
19036 KB |
Output is correct |
20 |
Correct |
12 ms |
25320 KB |
Output is correct |
21 |
Correct |
9 ms |
25112 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
10 ms |
25316 KB |
Output is correct |
24 |
Correct |
8 ms |
25180 KB |
Output is correct |
25 |
Correct |
8 ms |
23132 KB |
Output is correct |
26 |
Correct |
9 ms |
23052 KB |
Output is correct |
27 |
Correct |
10 ms |
25176 KB |
Output is correct |
28 |
Correct |
8 ms |
21084 KB |
Output is correct |
29 |
Correct |
6 ms |
16988 KB |
Output is correct |
30 |
Correct |
4 ms |
12888 KB |
Output is correct |
31 |
Correct |
237 ms |
198884 KB |
Output is correct |
32 |
Correct |
183 ms |
208212 KB |
Output is correct |
33 |
Correct |
15 ms |
10840 KB |
Output is correct |
34 |
Correct |
186 ms |
191764 KB |
Output is correct |
35 |
Correct |
435 ms |
358164 KB |
Output is correct |
36 |
Correct |
257 ms |
402512 KB |
Output is correct |
37 |
Correct |
23 ms |
38740 KB |
Output is correct |
38 |
Correct |
345 ms |
341696 KB |
Output is correct |
39 |
Correct |
220 ms |
355904 KB |
Output is correct |
40 |
Correct |
270 ms |
252300 KB |
Output is correct |
41 |
Correct |
317 ms |
297812 KB |
Output is correct |
42 |
Correct |
453 ms |
394288 KB |
Output is correct |
43 |
Correct |
217 ms |
213072 KB |
Output is correct |
44 |
Correct |
142 ms |
149588 KB |
Output is correct |
45 |
Correct |
28 ms |
38736 KB |
Output is correct |
46 |
Correct |
4 ms |
10588 KB |
Output is correct |
47 |
Correct |
5 ms |
10588 KB |
Output is correct |
48 |
Correct |
1 ms |
6492 KB |
Output is correct |
49 |
Correct |
4 ms |
10588 KB |
Output is correct |
50 |
Correct |
6 ms |
10588 KB |
Output is correct |
51 |
Correct |
2 ms |
10588 KB |
Output is correct |
52 |
Correct |
5 ms |
10792 KB |
Output is correct |
53 |
Correct |
5 ms |
10588 KB |
Output is correct |
54 |
Correct |
5 ms |
10588 KB |
Output is correct |
55 |
Correct |
4 ms |
10584 KB |
Output is correct |
56 |
Correct |
5 ms |
10588 KB |
Output is correct |
57 |
Correct |
4 ms |
10784 KB |
Output is correct |
58 |
Correct |
3 ms |
10844 KB |
Output is correct |
59 |
Correct |
5 ms |
10588 KB |
Output is correct |
60 |
Correct |
2 ms |
10588 KB |
Output is correct |
61 |
Execution timed out |
3092 ms |
21216 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |