# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
280109 | ec1117 | Arranging Shoes (IOI19_shoes) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* Description: range sum queries and point updates for $D$ dimensions
* Source: https://codeforces.com/blog/entry/64914
* Verification: SPOJ matsum
* Usage: \texttt{BIT<int,10,10>} gives 2D BIT
* Time: O((\log N)^D)
*/
#include "bits/stdc++.h"
#include "shoes.h"
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
#define pb push_back
#define mp make_pair
#define sz(v) ((int)(v).size())
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;
vector<pi> order[MAXN];
vector<pi> calc;
ll count_swaps(int s[]){
int size=sizeof(s)/sizeof(s[0]);
// int n = sz(s) / 2;
int n = size / 2;
ll ret=0;
// for(int i=0;i<sz(s);i++)
for(int i=0;i<size;i++)
order[abs(s[i])].push_back(mp(s[i], i));
for(int i=1;i<=MAXN;i++){
sort(order[i].begin(), order[i].end());
for(int j=0;j<order[i].size()/2;j++){
int l = order[i][j].second;
int r = order[i][j + sz(order[i])/2].second;
if(l>r) {
ret++;
swap(l,r);
}
calc.push_back(mp(l + 1, r + 1));
}
}
for(int i=1; i<=2*n; i++) bit.add(i, 1);
sort(calc.begin(),calc.end());
for(pi x:calc){
ret += bit.query(x.second) - bit.query(x.first) -1;
bit.add(x.second,-1);
bit.add(x.first,-1);
}
return ret;
}