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 sz(v) ((int)(v).size())
#define pb push_back
#define ff(j, a, b) for(int j=a;j<b;j++)
using namespace std;
typedef long long ll;
typedef pair<int, int> pint;
const int MAXN = 200005;
struct bit{
int tree[MAXN];
void add(int x, int v){
for(int i=x; i<MAXN; i+=i&-i) tree[i] += v;
}
int query(int x){
int ret = 0;
for(int i=x; i; i-=i&-i) ret += tree[i];
return ret;
}
}bit;
ll count_swaps(vector<int> s) {
int n = s.size() / 2;
ll ret = 0;
vector<pint> v;
vector<pint> ord[MAXN];
ff(i, 0, 2*n)
{
ord[abs(s[i])].pb(pint(s[i], i));
}
ff(i, 1, n+1)
{
sort(ord[i].begin(), ord[i].end());
for(int j=0; j<sz(ord[i])/2; j++){
int l = ord[i][j].second;
int r = ord[i][j + ord[i].size()/2].second;
if(l > r){
swap(l, r);
ret++;
}
v.pb(pint(l + 1, r + 1));
}
}
ff(i, 1, 2*n+1) bit.add(i, 1);
sort(v.begin(), v.end());
for(auto &i : v){
ret += bit.query(i.second - 1) - bit.query(i.first);
bit.add(i.first, -1);
bit.add(i.second, -1);
}
return ret;
}
# | 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... |