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;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}
#include "shoes.h"
struct Fen {
vector<ll> tree;
void init(int n){
tree.assign(n + 1 , 0LL);
}
void set(int i , ll val , int n){
i++;
while(i <= n){
tree[i] += val;
i += (i & (-i));
}
}
ll sum(int i){
i++;
ll ans = 0;
while(i > 0){
ans += tree[i];
i -= (i & (-i));
}
return ans;
}
void build(vector<int>& a){
int n = a.size();
for(int i = 0; i < n; i++){
set(i , a[i] , n);
}
}
};
ll count_swaps(vector<int> arr){
int n = arr.size();
vector<int> v[n + 1] , v1[n + 1];
int pos1[n] , pos[n];
for(int i = 0; i < n; i++){
if(arr[i] < 0){
v1[-arr[i]].pb(i);
pos1[i] = v1[-arr[i]].size()-1;
}
else {
v[arr[i]].pb(i);
pos[i] = v[arr[i]].size() - 1;
}
}
Fen ft;
ft.init(n + 3);
ll ans = 0;
ll cur = 0;
for(int i = 0; i < n; i++){
if(arr[i] < 0 && v[-arr[i]][pos1[i]] > i){
int p = pos1[i];
ans += ((ft.sum(n) - ft.sum(i)) + i) - cur;
ft.set(i,1LL , n + 2);
cur++;
ans += (ft.sum(n) - ft.sum(v[-arr[i]][p])) + v[-arr[i]][p] - cur;
ft.set(v[-arr[i]][p],1LL , n + 2);
cur++;
}
else if(arr[i] > 0 && v1[arr[i]][pos[i]] > i){
int p = v1[arr[i]][pos[i]];
ans += ((ft.sum(n) - ft.sum(p)) + p) - cur;
ft.set(p,1LL , n + 2);
cur++;
ans += (ft.sum(n) - ft.sum(i)) + i - cur;
ft.set(i , 1LL , n + 2);
cur++;
}
}
return ans;
}
# | 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... |