Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 2e5+5;
- const int size = (1<<24); // ~190mb
- long long a[size];
- int b[size];
- int n;
- inline long long h(int x, int y){ return (1ll*maxn*x+y)*7; }
- void h_inc(int x, int y){
- long long t = h(x, y);
- int k = t % size;
- while(a[k] != -1 && a[k] != t) k = (k+1) % size;
- a[k] = t, b[k]++;
- }
- int h_get(int x, int y){
- long long t = h(x, y);
- int k = t % size;
- while(a[k] != -1){
- if(a[k] == t) return b[k];
- k = (k+1) % size;
- }
- return 0;
- }
- int sum(int r1, int r2){
- int ans = 0;
- for(int i = r1; i >= 0; i = (i&(i+1))-1)
- for(int j = r2; j >= 0; j = (j&(j+1))-1)
- ans += h_get(i, j);
- return ans;
- }
- int sum(int l1, int r1, int l2, int r2){
- l2 = min(l2, n-1);
- r2 = min(r2, n-1);
- if(l1 > l2 || r1 > r2) return 0;
- return sum(l2, r2) - sum(l1-1, r2) - sum(l2, r1-1) + sum(l1-1, r1-1);
- }
- void add(int k1, int k2){
- for(int i = k1; i < n; i = (i|(i+1)))
- for(int j = k2; j < n; j = (j|(j+1)))
- h_inc(i, j);
- }
- int a_[maxn], b_[maxn], _a[maxn], _b[maxn];
- int main(){
- ios::sync_with_stdio(0);
- cin.tie(0);
- memset(a, -1, sizeof(a));
- freopen("friendcross.in", "r", stdin);
- freopen("friendcross.out", "w", stdout);
- int k;
- cin >> n >> k;
- for(int i = 0; i < n; i++){
- cin >> a_[i];
- a_[i]--;
- _a[a_[i]] = i;
- }
- for(int i = 0; i < n; i++){
- cin >> b_[i];
- b_[i]--;
- _b[b_[i]] = i;
- }
- long long ans = 0;
- for(int i = 0; i < n; i++){
- int t = a_[i];
- int x = _b[t];
- ans += sum(0, x+1, t-k-1, n-1) + sum(t+k+1, x+1, n-1, n-1);
- add(t, x);
- }
- cout << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement