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 ld double
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define pb emplace_back
#define X first
#define Y second
const int N = 2e5 + 5;
typedef pair<int,int> ii;
int t[N];
int upd(int p) {
for(; p > 0 ; p -= p & -p)
t[p]++;
return 1;
}
int get(int p) {
int ans = 0;
for(; p < N ; p += p & -p)
ans += t[p];
return ans;
}
queue<int> pos[N];
ll count_swaps(vector<int> S) {
queue<ii> quL;
int n = S.size() / 2;
for(int i = 1 ; i <= n + n ; ++i) {
int x = S[i - 1];
if (x < 0) quL.push(ii(-x,i));
if (x > 0) pos[x].push(i);
}
ll ans = 0;
for(int i = 1 ; i <= n + n ; ++i) {
int s = quL.front().X;
int p = quL.front().Y; quL.pop();
ans += p + get(p) - i++; upd(p); p = pos[s].front(); pos[s].pop();
ans += p + get(p) - i; upd(p);
}
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... |