Submission #1237114

#TimeUsernameProblemLanguageResultExecution timeMemory
1237114Sir_Ahmed_ImranArranging Shoes (IOI19_shoes)C++17
100 / 100
38 ms19784 KiB
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;
#define MAXN 200001
#define nl '\n'
#define ff first
#define ss second
#define ll long long
#define ld long double
#define terminator main
#define pll pair<ll,ll>
#define add insert
#define append push_back
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()

int a[MAXN];
int inv[MAXN];
int sum[MAXN];
vector<int> l[MAXN];
vector<int> r[MAXN];

int get(int p){
	int ans = 0;
	while(p){
		ans += sum[p];
		p -= p & -p;
	}
	return ans;
}

int get(int l, int r){
	return get(r) - get(l);
}

void add(int p, int x){
	while(p < MAXN){
		sum[p] += x;
		p += p & -p;
	}
}

ll count_swaps(vector<int> s){
	int n, p, q;
	ll ans = 0;
	n = s.size() / 2;
	for(int i = 0; i < 2 * n; i++){
		a[i + 1] = s[i];
		if(s[i] < 0)
			l[-s[i]].append(i + 1);
		else r[s[i]].append(i + 1);
	}
	for(int i = 1; i <= n; i++)
		for(int j = 0; j < l[i].size(); j++)
			inv[l[i][j]] = r[i][j], inv[r[i][j]] = l[i][j];
	for(int i = 1; i <= 2 * n; i++){
		if(inv[i] < i) continue;
		ans += inv[i] - i - (a[i] < 0) + get(i, inv[i]);
		add(inv[i], - 1);
		add(i, 1);
	}
	return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...