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 "shoes.h"
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vl vector<ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second;
using namespace std;
int tree[4 * 200100], lazy[4 * 200100];
void update(int node, int l, int r, int L, int R, int add){
if(lazy[node]){
tree[node] += (r - l + 1) * lazy[node];
if(l != r){
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
if(R < l || r < L) return;
if(L <= l && r <= R){
tree[node] += (r - l + 1) * add;
if(l != r){
lazy[node * 2 + 1] += add;
lazy[node * 2] += add;
}
return;
}
int mid = (l + r) / 2;
update(node * 2, l, mid, L, R, add);
update(node * 2 + 1, mid + 1, r, L, R, add);
tree[node] = tree[node * 2] + tree[node * 2 + 1];
}
int query(int node, int l, int r, int L, int R){
if(lazy[node] != 0){
tree[node] += (r - l + 1) * lazy[node];
if(l != r){
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
if(r < L || l > R) return 0;
if(L <= l && r <= R) return tree[node];
int mid = (l + r) / 2;
int left = query(node * 2, l, mid, L, R);
int right = query(node * 2 + 1, mid + 1, r, L, R);
return left + right;
}
multiset<int> l_pos[100010], r_pos[100010];
long long count_swaps(std::vector<int> s) {
ll ans = 0;
vi arr = s;
int n = s.size();
for(int i = 0; i < n; i++){
if(s[i] < 0){
l_pos[abs(s[i])].insert(i);
}
else
r_pos[s[i]].insert(i);
}
memset(tree, 0, sizeof tree);
memset(lazy, 0, sizeof lazy);
bool vis[n];
memset(vis, false, sizeof vis);
for(int i = 0; i < n; i++){
if(vis[i]) continue;
if(arr[i] < 0){
int cur_pos = i + query(1, 0, n-1, i, i);
int p_pos = *r_pos[abs(arr[i])].begin();
int get = query(1, 0, n-1, p_pos, p_pos) + p_pos;
if(cur_pos > get)
assert(false);
if(cur_pos >= n || get >= n)
assert(false);
// cout<<cur_pos<<" "<<get<<endl;
ans += (get - cur_pos - 1);
update(1, 0, n-1, i, p_pos-1, 1);
vis[p_pos] = true;
vis[i] = true;
r_pos[abs(arr[i])].erase(p_pos);
l_pos[abs(arr[i])].erase(i);
}
else{
int cur_pos = i + query(1, 0, n-1, i, i);
int p_pos = *l_pos[arr[i]].begin();
int get = query(1, 0, n-1, p_pos, p_pos) + p_pos;
if(cur_pos > get)
assert(false);
if(cur_pos >= n || get >= n)
assert(false);
// cout<<cur_pos<<" "<<get<<endl;
ans += (get - cur_pos);
// cout<<get - cur_pos<<" "<<get<<" "<<cur_pos<<endl;
update(1, 0, n-1, i, p_pos-1, 1);
vis[p_pos] = true;
vis[i] = true;
l_pos[arr[i]].erase(p_pos);
r_pos[arr[i]].erase(i);
}
}
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... |